Aside from the numbers of rows that may be in a table, would one of these sample queries be more costly than the other?
SELECT * FROM dbo.Accounts WHERE AccountID IN (4,6,7,9,10)
SELECT * FROM dbo.Accounts WHERE AccountID NOT IN (4,6,7,9,10)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generally speaking a
NOT INwill be more expensive although it is certainly possible to construct scenarios where the opposite is true.First, assuming that
AccountIdis the Primary Key for theAccountstable.IN (4,6,7,9,10)will require 5 index seeks which means logical IO is 5 * the depth of the index (each seek needs to navigate from the root down through the intermediate pages and to exactly one leaf page).NOT IN (4,6,7,9,10)will require a full scan and a filter (pushable non sargable predicate means it is pushed into the scan rather than as a separate operator) which means logical IO will equal the number of pages in the leaf node of the index + the number of non leaf levels.To see this
Returns
Looking at the pathologically different case where all of the rows meet the
INclause and thus none of them theNOT INReturns
(The index is deper as the uniquifier has been added to the clustered index key)
The
INis still implemented as 5 equality seeks but this time the number of leaf pages read on each seek is considerably more than 1. The leaf pages are arranged in a linked list and SQL Server carries on navigating along this until it encounters a row not matching the seek.The
NOT INis now implemented as 2 range seeksWith the residual predicate of
So it can be seen that even in this extreme case the best SQL Server can do is to skip looking at the leaf pages for just one of the
NOT INvalues. Somewhat surprisingly even when I skewed the distribution such that theAccountID=7records were 6 times more prevalent than theAccountID=4ones it still gave the same plan, and did not rewrite it as range seeks either side of 7, similarly when reducing the number ofAccountID=4records to 1 the plan reverted to a clustered index scan so it seems limited to considering this transformation only for the first value in the index.Addition
In the first half of my answer the numbers add up exactly as they would be expected to from my description and the index depth.
In the second part my answer didn’t explain exactly why an index with 3 levels and 358 leaf pages should cause quite the exact number of logical reads that it does, for the very good reason that I wasn’t quite sure myself! However I’ve filled in the missing bit of knowledge now.
First this query (SQL Server 2008+ syntax only)
Gives these results
Adding up
NumPagesgives a total of 362 reflecting the fact that some leaf pages contain 2 differentAccountIdvalues. These pages will be visited twice by the seeks.Gives
So,
For the
INversion:the seek for
=4visits 1 root page, 1 intermediate page and 72 leaf pages (74)the seek for
=6visits 1 root page, 1 intermediate page and 72 leaf pages (74)the seek for
=7visits 1 root page, 1 intermediate page and 73 leaf pages (75)the seek for
=9visits 1 root page, 1 intermediate page and 72 leaf pages (74)the seek for
=10visits 1 root page, 1 intermediate page and 73 leaf pages (75)Total: (372) (vs 378 reported in the IO stats)
And, for the
NOT INversion:the seek for
<4visits 1 root page, 1 intermediate page and 1 leaf pages (3)the seek for
>4visits 1 root page, 1 intermediate page and 287 leaf pages (289)Total: (292) (vs 295 reported in the IO stats)
So what about the unaccounted for IOs?
It turns out that these are related to the
read-aheadmechanism. It is possible to (on a development instance) use a trace flag to disable this mechanism and verify that the logical reads are now reported as expected from the description above. This is discussed further in the comments to this blog post.