株式市場の移動平均値の表があり、1 日のうちに 2 つの値を比較し、その値を前日の同じ計算と比較しようとしています。私のSQLは以下のとおりです...結果セットを定義する最後のselectステートメントをコメントアウトし、結果セットとして表示された最後のcteを実行すると、約15分でデータが返されます。長いですが、一晩挿入 sproc として実行されるため扱いやすいです。示されているように実行すると、結果が表示されるまでに 40 分かかります。何かアイデアはありますか? おそらくBTWを追加すると、多少遅くなり、爆破するようになりROW_NUMBER() OVER (PARTITION BY)
ます.このパフォーマンスの問題では現在不可能なロジックをまだ処理しています. 前もって感謝します..
編集:以下に提案するようにパーティションを修正しました。
with initialSmas as
(
select TradeDate, Symbol, Period, Value
from tblDailySMA
),
smaComparisonsByPer as
(
select i.TradeDate, i.Symbol, i.Period FastPer, i.Value FastVal,
i2.Period SlowPer, i2.Value SlowVal, (i.Value-i2.Value) FastMinusSlow
from initialSmas i join initialSmas as i2 on i.Symbol = i2.Symbol
and i.TradeDate = i2.TradeDate and i2.Period > i.Period
),
smaComparisonsByPerPartitioned as
(
select ROW_NUMBER() OVER (PARTITION BY sma.Symbol, sma.FastPer, sma.SlowPer
ORDER BY sma.TradeDate) as RowNum, sma.TradeDate, sma.Symbol, sma.FastPer,
sma.FastVal, sma.SlowPer, sma.SlowVal, sma.FastMinusSlow
from smaComparisonsByPer sma
)
select scp.TradeDate as LatestDate, scp.FastPer, scp.FastVal, scp.SlowPer, scp.SlowVal,
scp.FastMinusSlow, scp2.TradeDate as LatestDate, scp2.FastPer, scp2.FastVal, scp2.SlowPer,
scp2.SlowVal, scp2.FastMinusSlow, (scp.FastMinusSlow * scp2.FastMinusSlow) as Comparison
from smaComparisonsByPerPartitioned scp join smaComparisonsByPerPartitioned scp2
on scp.Symbol = scp2.Symbol and scp.RowNum = (scp2.RowNum - 1)