Author |
Topic |
LOOKUP_BI
Constraint Violating Yak Guru
295 Posts |
Posted - 2010-12-09 : 13:38:07
|
SELECT DISTINCT A.StatusFROM dbo.Test1 ALEFT JOIN dbo.Test2 BON A.Status= B.StatusWHERE B.Status IS NULL AND A.Status IS NOT NULLNumber of rows in dbo.Test1 A = 150 000Number of rows in dbo.Test2 B = 4500There are clusterd indexes on both table on ID columnThis query is taking 35 minutes.How can I make it fasterI have tested the following1) Included column A.Status & B.Status on the clustered index -Time taken 34 minutesThe query execution plan shows that the LEFT JOIN operation cost is 60% and the filter on B.Status IS NULL is 18%.These two task has the highest % |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2010-12-09 : 14:14:45
|
Put a non clustered index on Status on both tables. For this query in isolation, you don't need any other columns included, but you may want to consider including columns that other queries might need that join/filter on status.-Chad |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-12-09 : 14:22:47
|
select a.Status from Test1 awhere not exists(select * from Test2 b where b.Status=a.Status)group by a.Status No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
LOOKUP_BI
Constraint Violating Yak Guru
295 Posts |
Posted - 2010-12-09 : 14:50:41
|
I tried this and it executed really fast,apprecaite if someone can verify if this logic is the same.SELECT DISTINCT A.Status INTO #TEMP FROM dbo.Test1 A where A.Status IS NOT NULLSELECT C.Status from #TEMP CLEFT JOIN dbo.Test2 BON C.Status= B.StatusWHERE B.Status IS NULL |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-12-09 : 15:01:12
|
Yes it is the same. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-12-09 : 15:58:59
|
Did you try webfred's query? Saves meesing around with a temporary table and I think will be faster.Or possibly this:select DISTINCT a.Status from Test1 awhere not exists(select * from Test2 b where b.Status=a.Status)group by a.Status |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-12-09 : 16:00:10
|
"1) Included column A.Status & B.Status on the clustered index -Time taken 34 minutes"Having the [Status] in the clustered index won't help (unless it is the only column in the clustered index, which is a fairly moot point as it is not unique). Putting it in a separate index probably would help. |
 |
|
LOOKUP_BI
Constraint Violating Yak Guru
295 Posts |
Posted - 2010-12-09 : 16:38:31
|
I did try webfred's query but it did not improve on the duration.I also did try the below, but no improvementsselect DISTINCT a.Status from Test1 awhere not exists(select * from Test2 b where b.Status=a.Status)Im going to create a nonclusterd index on Status column and try again. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-12-09 : 16:49:41
|
"Im going to create a nonclusterd index on Status column and try again."Well that should help (or I'll have to eat my hat ... again ...!) as it will cover the query |
 |
|
TimSman
Posting Yak Master
127 Posts |
Posted - 2010-12-09 : 17:04:59
|
What about:SELECT DISTINCT A.StatusFROM dbo.Test1 AS AEXCEPTSELECT DISTINCT B.StatusFROM dbo.Test2 AS b |
 |
|
LOOKUP_BI
Constraint Violating Yak Guru
295 Posts |
Posted - 2010-12-13 : 13:17:08
|
Im going to create a nonclusterd index on Status column and try again.Yippie this helped me out, thanks guys |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-12-13 : 13:38:56
|
How selective is status? |
 |
|
|