I've run into a curiosity. While creating a script to correlate some data, I was trying to use MIN() and MAX() on a identity column based on values in a datetime column. Using MAX(), I got the results back in six seconds (00:00:06). Using MIN(), I got the results back in 8 minutes 43 seconds (00:08:43). Using the Estimated Execution Plan, I found this:
The MAX() query first does a Clustered Index SEEK (98%), then a TOP (2%), then a Stream Aggregate (0%).
The MIN() query first does a Clustered Index SCAN (100%), followed by Parallelism/Gather Stream (0%), then a TOP (2%), then a Stream Aggregate (0%).
The queries are:
The RowID column is a clustered index and the dt_column is a non-clustered index.
I can make the second query faster by changing it to a MAX() and using 2006-09-06 10:59:59 (acceptable for the need), but that doesn't explain why one uses a scan and parallelism/gather stream and the other uses a seek and no parallelism.
Any thoughts on this?
(I can't change the schema and that's not the point of this, I'm more interested in why the difference execution between the two.)
-SQLBill
Posting advice: FAQ481-4875
The MAX() query first does a Clustered Index SEEK (98%), then a TOP (2%), then a Stream Aggregate (0%).
The MIN() query first does a Clustered Index SCAN (100%), followed by Parallelism/Gather Stream (0%), then a TOP (2%), then a Stream Aggregate (0%).
The queries are:
Code:
SELECT MAX(RowID)
FROM mytable
WHERE dt_column = '2006-09-06 11:00'
SELECT MIN(RowID)
FROM mytable
WHERE dt_column = '2006-09-06 11:00'
The RowID column is a clustered index and the dt_column is a non-clustered index.
I can make the second query faster by changing it to a MAX() and using 2006-09-06 10:59:59 (acceptable for the need), but that doesn't explain why one uses a scan and parallelism/gather stream and the other uses a seek and no parallelism.
Any thoughts on this?
(I can't change the schema and that's not the point of this, I'm more interested in why the difference execution between the two.)
-SQLBill
Posting advice: FAQ481-4875