snippetsqlMinor
SQL Server index / performance help needed (index scan and a sort taking 40 minutes)
Viewed 0 times
neededminutesscansqlandhelpsortperformanceserverindex
Problem
I finally have the logic figured out for a query, now I need to speed it up... dramatically, if possible (it runs for 40 minutes +/-). It's on two tables, one with just a couple hundred rows (
My basic logic is: I'm returning a row for each combination of symbol, trade date, and period values, creating a "slow v fast" pair of periods. So in the original table, there is a symbol, a trade date, a period (5,10,20,...) and the actual value for that combination. I want to compare the values for all combinations of periods (except period p1 = period p2). I'm numbering the records to be used for further processing. Hope that makes sense.
I've tried various indexes and combinations of the fields involved in the joins and predicates. In the estimated execution plan, it looks like a sort is the most expensive operation. The only index on
Here's the execution plan:
```
|--Compute Scalar(DEFINE:([Expr1007]=[Market].[dbo].[tblDailySMA].[Value] as [t1].[Value]-[Market].[dbo].[tblDailySMA].[Value] as [t2].[Value], [Expr1008]=CASE WHEN [Market].[dbo].[tblD
tblTradingDays) and the other with over a million (tblDailySMA). It returns 48 million rows.My basic logic is: I'm returning a row for each combination of symbol, trade date, and period values, creating a "slow v fast" pair of periods. So in the original table, there is a symbol, a trade date, a period (5,10,20,...) and the actual value for that combination. I want to compare the values for all combinations of periods (except period p1 = period p2). I'm numbering the records to be used for further processing. Hope that makes sense.
I've tried various indexes and combinations of the fields involved in the joins and predicates. In the estimated execution plan, it looks like a sort is the most expensive operation. The only index on
tblDailySMA that appears in the plan, despite my adding others is nonclustered, unique, on: Symbol, TradeDate, Period, and includes Value. It is scanned. The query is below, hopefully someone can help me out. Thanks in advance..SELECT
ROW_NUMBER() OVER
(
ORDER BY t1.Symbol, t1.Period, t2.Period, t.TradingDate DESC
) RowNum,
t1.Symbol, t.TradingDate, t1.Period, t2.Period, t1.Value FastValue,
t2.Value SlowValue, (t1.Value - t2.Value) SlowFastDiff,
ChgSign = CASE WHEN t1.Value = t2.Value THEN 1
WHEN t1.Value IS NULL OR t2.Value IS NULL THEN NULL
END
FROM
tblTradingDays t
RIGHT JOIN
tblDailySMA t1 ON t.TradingDate = t1.TradeDate
INNER JOIN
tblDailySMA t2 ON t1.Symbol = t2.Symbol AND t1.TradeDate = t2.TradeDate
WHERE
t1.Period < t2.PeriodHere's the execution plan:
```
|--Compute Scalar(DEFINE:([Expr1007]=[Market].[dbo].[tblDailySMA].[Value] as [t1].[Value]-[Market].[dbo].[tblDailySMA].[Value] as [t2].[Value], [Expr1008]=CASE WHEN [Market].[dbo].[tblD
Solution
When I have this kind of performance problems in a big query I split it into small queries with temporary tables. For me it is a solution and performance ratio may be 10 to 1 or more.
First step:
Secon step:
And so on. One benefit of this system is that you can improve queries step by step.
First step:
with t1 as (
SELECT
t1.Symbol,
t1.Period,
t.TradingDate,
t1.Value as FastValue
FROM tblDailySMA t1
LEFT JOIN tblTradingDays t
ON t.TradingDate = t1.TradeDate
)
select *
into #t1
from t1;
--I include period into idex to avoid table access on next query
create index t1_idx on ( Symbol, TradeDate, Period )Secon step:
with t2 as (
SELECT
t1.Symbol,
t1.Period as period_t1,
t.TradingDate,
t1.Value as FastValue
t2.Period as period_t2,
t2.Value as SlowValue,
t2.TradeDate
FROM #t1 as t1
INNER JOIN tblDailySMA t2
ON t1.Symbol = t2.Symbol AND t1.TradeDate = t2.TradeDate
WHERE t1.Period < t2.Period
)
select
*
into #t2
from t2;
--Here create indexes for t2
--Here next and final queryAnd so on. One benefit of this system is that you can improve queries step by step.
Code Snippets
with t1 as (
SELECT
t1.Symbol,
t1.Period,
t.TradingDate,
t1.Value as FastValue
FROM tblDailySMA t1
LEFT JOIN tblTradingDays t
ON t.TradingDate = t1.TradeDate
)
select *
into #t1
from t1;
--I include period into idex to avoid table access on next query
create index t1_idx on ( Symbol, TradeDate, Period )with t2 as (
SELECT
t1.Symbol,
t1.Period as period_t1,
t.TradingDate,
t1.Value as FastValue
t2.Period as period_t2,
t2.Value as SlowValue,
t2.TradeDate
FROM #t1 as t1
INNER JOIN tblDailySMA t2
ON t1.Symbol = t2.Symbol AND t1.TradeDate = t2.TradeDate
WHERE t1.Period < t2.Period
)
select
*
into #t2
from t2;
--Here create indexes for t2
--Here next and final queryContext
StackExchange Database Administrators Q#23473, answer score: 3
Revisions (0)
No revisions yet.