|
I have this query but it is taking foreever to run. Any suggestions on if it can be changed to run faster? BTW tablaA has nearly 4 million records and tableB 40,000. I am thinking theres is nothing I can do but figured couldn;t hurt to ask. This is a simple example that resembles it:
(comments are locked)
|
|
I think indexing this could be a bear. You've got two different filter criteria, one of them a rather wide IN clause (which results in OR statements, which frequently cause scans) and the other a BETWEEN on a text field, again, possibly problematic. Then, on top of it, we get an aggregation against another column with a DISTINCT. Yikes. First thing I'd do is pivot the IN clause to a temp table and then use a JOIN. You're more likely to get a good execution plan that way. After that... I'd need to see the execution plan to understand how SQL Server is dealing with all the details. Look up Jeff Moden and Tally Table to find great articles on how to do the pivot.
(comments are locked)
|
|
You can also try this ? You do not need to count ID field because as per your query it will be always 1. +1, though I agree with Fatherjack about indexing and joining rather than using subquery.
Oct 20 '11 at 01:08 AM
Magnus Ahlkvist
I'm not crazy about the use of NOLOCK. You do know that it can lead to missing or extra rows depending on how page splits occur as you're reading?
Oct 20 '11 at 03:34 AM
Grant Fritchey ♦♦
I know about the advantage and disadvantage of using NOLOCK, but it's depends on requirement. so for every case you can not say that NOLOCK is not helpful.
Oct 20 '11 at 05:12 AM
Amardeep
Magnus, in mine suggested query i used exists condition and sometime it is very fast instead of inner join also.
Oct 20 '11 at 05:14 AM
Amardeep
(comments are locked)
|
|
Agreeing with what all the others have said, I'd like to be a little more specific on the indexing. I'm guessing tablea.ID is the primary key, so that should already be indexed, probably clustered index. If not, create an index on that column. Apart from that, you should have indexes on colA, colB and colC in both tables. For this specific query, you'd probably do fine with one index containing all three columns, but if you want that or not should also be a consideration with the other queries running against the tables. tablea.ProdID should definitely be indexed. To find out which indexes you have, you could either expand Tables->tablea->Indexes in Management Studio, or run a query to find out:
(comments are locked)
|
|
I would suggest trying a temp table, as @sqlchamp has shown but I would index it and join on it, not use a nested select CREATE TABLE #Prod ( ProdID INT ) You will also need to look at the execution plan, consider data types and indexing. Your +1. What other construct would you use to replace the BETWEEN clause?
Oct 20 '11 at 01:09 AM
Magnus Ahlkvist
Heh! I'd try >= and <= depending on data type - is it int/char/varchar is it indexed ... ... ... so many possibilities.!
Oct 20 '11 at 01:21 AM
Fatherjack ♦♦
Is >= and <= performing better than BETWEEN? If I'd implement a BETWEEN clause, I'd do it using >= and <=...
Oct 20 '11 at 01:36 AM
Magnus Ahlkvist
(comments are locked)
|


What indexes are in place on those tables? What's the range of data within the ID field? What's the distribution of that data? What are you trying to achieve?
Trying to count the number of ids that use each product. I am not sure on what indexes are currrently in place.
Oh, and should the a1.YearMonth field be between 200810 and 200909 rather than 200810 and 20909?
If you are grouping by a1.ID then are you sure you need to select count(distinct a1.ID)? Also, there might be a type in the part of the predicate reading
and (a1.YearMonth between 200810 and 20909)
The 20909 looks strange, maybe you meant it to be 200909 seeking to get one year worth of data.
Since it looks like you are only getting back 20 rows from your query, it should not take long time at all unless there are missing indexes.
@sqlnewb : you deleted your own question again, this time it had comments. If you realised something was wrong, please just comment and do not delete