SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


NOT operator and Index Use


NOT operator and Index Use

Author
Message
kramaswamy
kramaswamy
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1529 Visits: 1803
Hi all,

Very basic question. I was reading the MCTS 70-433 training kit book from Microsoft, and fairly early on, they make a claim which I don't really fully believe.

In it, they claim that the use of the NOT operator makes indexes unusable for the query optimiser. However, I'm sure that I've regularly used NOT and had no problem with the optimiser using indexes. IE:


SELECT ID
FROM Table
WHERE ID NOT IN (1, 2, 3, 4, 5)



I have a feeling that the statement was just a generalized claim, since the following query clearly would not use an index seek:


SELECT ID
FROM Table
WHERE NOT (ID = 5 AND Name = 'Andrew')



Since, because of the NOT operator and the way boolean logic works, that query translates into an OR (WHERE ID <> 5 OR Name <> 'Andrew').


So, is the claim in the book incorrect?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88501 Visits: 45284
Yes, the claim in the book is incorrect and is a common myth.

p.s. Your second will be able to use indexes, but probably for a scan operation, not a seek (or for a ranged seek)

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search