这是我之前发布的帖子的延续:RowNumber() and Partition By performance help wanted
我的查询需要显着的性能提升。根据前一篇文章中的建议,我在查询中删除了除了一个cte之外的所有内容,并实现了一个临时表,其中包含索引。它仍然非常缓慢...目前40分钟并计数,尚未返回数据。一些背景信息:所有数据来自的一个表有大约500万行。它上面有几个索引,包括一个唯一的非聚簇,它包含Symbol,Period和TradeDate列,其中包含Value列。我有两个完全相同的但是首先是Period,然后是TradeDate。表上还有一个唯一的聚簇索引。什么可以加快这个速度?临时表上有不同的索引?对不起半重复的帖子..我在这里停滞不前。任何帮助都会很大。
create table ##smaComp
(
RowNum bigint,
Rank bigint,
TradeDate Date,
Symbol Char(6),
FastPer int,
FastVal Decimal(9,4),
SlowPer int,
SlowVal Decimal(9,4),
FastMinusSlow Decimal(9,4)
)
;with sma as
(
select t.TradeDate, t.Symbol, t.Period FastPer, t.Value FastVal, t2.Period SlowPer,
t2.Value SlowVal, (t.Value-t2.Value) FastMinusSlow
from tblDailySMA t join tblDailySMA as t2 on t.Symbol = t2.Symbol
and t.TradeDate = t2.TradeDate and t2.Period > t.Period
)
insert into ##smaComp
(
RowNum, Rank, TradeDate, Symbol, FastPer, FastVal, SlowPer, SlowVal, FastMinusSlow
)
select ROW_NUMBER() OVER (PARTITION BY sma.Symbol, sma.FastPer, sma.SlowPer
ORDER BY sma.TradeDate) as RowNum, DENSE_RANK() OVER (ORDER BY sma.Symbol, sma.FastPer,
sma.SlowPer) as Rank, sma.TradeDate, sma.Symbol, sma.FastPer, sma.FastVal,
sma.SlowPer, sma.SlowVal, sma.FastMinusSlow
from sma
CREATE UNIQUE NONCLUSTERED INDEX [IX_tblDailySMAClustered] ON ##smaComp
(RowNum, Rank)
INCLUDE (Symbol, TradeDate, FastPer, SlowPer, FastVal, SlowVal, FastMinusSlow)
select t.TradeDate as PriorDate, t.FastPer, t.FastVal, t.SlowPer, t.SlowVal,
t.FastMinusSlow, t2.TradeDate as LatestDate, t2.FastPer, t2.FastVal, t2.SlowPer,
t2.SlowVal, t2.FastMinusSlow, (t2.FastMinusSlow * t2.FastMinusSlow) as Comparison
from ##smaComp t join ##smaComp t2
on t.Rank = t2.Rank and t.RowNum = (t2.RowNum - 1)
执行计划,如要求:
StmtText
create table ##smaComps ( RowNum bigint, Rank bigint, TradeDate Date, Symbol Char(6), FastPer int, FastVal Decimal(9,4), SlowPer int, SlowVal Decimal(9,4), FastMinusSlow Decimal(9,4) )
;with sma as ( select t.TradeDate, t.Symbol, t.Period FastPer, t.Value FastVal, t2.Period SlowPer, t2.Value SlowVal, (t.Value-t2.Value) FastMinusSlow from tblDailySMA t join tblDailySMA as t2 on t.Symbol = t2.Symbol and t.TradeDate = t2.TradeDate and t2.Period > t.Period ) insert into ##smaComps ( RowNum, Rank, TradeDate, Symbol, FastPer, FastVal, SlowPer, SlowVal, FastMinusSlow ) select ROW_NUMBER() OVER (PARTITION BY sma.Symbol, sma.FastPer, sma.SlowPer ORDER BY sma.TradeDate) as RowNum, DENSE_RANK() OVER (ORDER BY sma.Symbol, sma.FastPer, sma.SlowPer) as Rank, sma.TradeDate, sma.Symbol, sma.FastPer, sma.FastVal, sma.SlowPer, sma.SlowVal, sma.FastMinusSlow from sma
StmtText
|--Table Insert(OBJECT:([tempdb].[dbo].[##smaComps]), SET:([tempdb].[dbo].[##smaComps].[RowNum] = [Expr1009],[tempdb].[dbo].[##smaComps].[Rank] = [Expr1010],[tempdb].[dbo].[##smaComps].[TradeDate] = [Market].[dbo].[tblDailySMA].[TradeDate] as [t].[TradeDate],[tempdb].[dbo].[##smaComps].[Symbol] = [Expr1011],[tempdb].[dbo].[##smaComps].[FastPer] = [Market].[dbo].[tblDailySMA].[Period] as [t].[Period],[tempdb].[dbo].[##smaComps].[FastVal] = [Expr1012],[tempdb].[dbo].[##smaComps].[SlowPer] = [Market].[dbo].[tblDailySMA].[Period] as [t2].[Period],[tempdb].[dbo].[##smaComps].[SlowVal] = [Expr1013],[tempdb].[dbo].[##smaComps].[FastMinusSlow] = [Expr1014]))
|--Compute Scalar(DEFINE:([Expr1014]=CONVERT_IMPLICIT(decimal(9,4),[Expr1008],0)))
|--Top(ROWCOUNT est 0)
|--Compute Scalar(DEFINE:([Expr1011]=CONVERT_IMPLICIT(char(6),[Market].[dbo].[tblDailySMA].[Symbol] as [t].[Symbol],0), [Expr1012]=CONVERT_IMPLICIT(decimal(9,4),[Market].[dbo].[tblDailySMA].[Value] as [t].[Value],0), [Expr1013]=CONVERT_IMPLICIT(decimal(9,4),[Market].[dbo].[tblDailySMA].[Value] as [t2].[Value],0)))
|--Sequence Project(DEFINE:([Expr1010]=dense_rank))
|--Segment
|--Segment
|--Sequence Project(DEFINE:([Expr1009]=row_number))
|--Segment
|--Compute Scalar(DEFINE:([Expr1008]=[Market].[dbo].[tblDailySMA].[Value] as [t].[Value]-[Market].[dbo].[tblDailySMA].[Value] as [t2].[Value]))
|--Parallelism(Gather Streams, ORDER BY:([t].[Symbol] ASC, [t].[Period] ASC, [t2].[Period] ASC, [t].[TradeDate] ASC))
|--Sort(ORDER BY:([t].[Symbol] ASC, [t].[Period] ASC, [t2].[Period] ASC, [t].[TradeDate] ASC))
|--Merge Join(Inner Join, MANY-TO-MANY MERGE:([t].[TradeDate], [t].[Symbol])=([t2].[TradeDate], [t2].[Symbol]), RESIDUAL:([Market].[dbo].[tblDailySMA].[Symbol] as [t].[Symbol]=[Market].[dbo].[tblDailySMA].[Symbol] as [t2].[Symbol] AND [Market].[dbo].[tblDailySMA].[TradeDate] as [t].[TradeDate]=[Market].[dbo].[tblDailySMA].[TradeDate] as [t2].[TradeDate] AND [Market].[dbo].[tblDailySMA].[Period] as [t2].[Period]>[Market].[dbo].[tblDailySMA].[Period] as [t].[Period]))
|--Parallelism(Repartition Streams, Hash Partitioning, PARTITION COLUMNS:([t].[TradeDate], [t].[Symbol]), ORDER BY:([t].[TradeDate] ASC, [t].[Symbol] ASC))
| |--Index Scan(OBJECT:([Market].[dbo].[tblDailySMA].[IX_tblDailySMA_TrDateNonClust] AS [t]), ORDERED FORWARD)
|--Parallelism(Repartition Streams, Hash Partitioning, PARTITION COLUMNS:([t2].[TradeDate], [t2].[Symbol]), ORDER BY:([t2].[TradeDate] ASC, [t2].[Symbol] ASC))
|--Index Scan(OBJECT:([Market].[dbo].[tblDailySMA].[IX_tblDailySMA_TrDateNonClust] AS [t2]), ORDERED FORWARD)
StmtText
CREATE UNIQUE NONCLUSTERED INDEX [IX_tblDailySMAClustered] ON ##smaComps (RowNum, Rank) INCLUDE (Symbol, TradeDate, FastPer, SlowPer, FastVal, SlowVal, FastMinusSlow)
select t.TradeDate as PriorDate, t.FastPer, t.FastVal, t.SlowPer, t.SlowVal, t.FastMinusSlow, t2.TradeDate as LatestDate, t2.FastPer, t2.FastVal, t2.SlowPer, t2.SlowVal, t2.FastMinusSlow, (t2.FastMinusSlow * t2.FastMinusSlow) as Comparison from ##smaComps t join ##smaComps t2 on t.Rank = t2.Rank and t.RowNum = (t2.RowNum - 1)
StmtText
|--Hash Match(Inner Join, HASH:([t].[Rank], [t].[RowNum])=([t2].[Rank], [Expr1007]), RESIDUAL:([tempdb].[dbo].[##smaComps].[Rank] as [t].[Rank]=[tempdb].[dbo].[##smaComps].[Rank] as [t2].[Rank] AND [tempdb].[dbo].[##smaComps].[RowNum] as [t].[RowNum]=[Expr1007]))
|--Table Scan(OBJECT:([tempdb].[dbo].[##smaComps] AS [t]))
|--Compute Scalar(DEFINE:([Expr1006]=[tempdb].[dbo].[##smaComps].[FastMinusSlow] as [t2].[FastMinusSlow]*[tempdb].[dbo].[##smaComps].[FastMinusSlow] as [t2].[FastMinusSlow], [Expr1007]=[tempdb].[dbo].[##smaComps].[RowNum] as [t2].[RowNum]-(1)))
|--Table Scan(OBJECT:([tempdb].[dbo].[##smaComps] AS [t2]))
答案 0 :(得分:1)
查询计划不包含任何特别错误的内容(如嵌套循环爆炸)。
我认为原因是你在所有“时期”交叉加入。我的猜测是,您的数据每个TradeDate和Symbol包含许多(100s?)句点。这意味着SQL Server必须处理二次数据量。谓词t2.Period > t.Period
过滤了大约一半的行,但另一半仍然存在。
因此原则上数据量非常高。不确定这是否可以优化。您需要所有数据还是只需要一个子集?如果您需要所有这些,我认为无法做任何事情。
您可以通过将查询限制为一个TradeDate和Symbol并查看行计数来测试此假设。