Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

NOT operator and Index Use Expand / Collapse
Author
Message
Posted Sunday, October 21, 2012 5:39 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 8:44 AM
Points: 819, Visits: 1,654
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?
Post #1375219
Posted Monday, October 22, 2012 12:44 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:15 AM
Points: 39,975, Visits: 36,334
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 2008, MVP
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

Post #1375266
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse