Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

NOT operator and Index Use Expand / Collapse
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: Monday, November 28, 2016 1:42 PM
Points: 850, Visits: 1,785
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:

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:

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



Group: General Forum Members
Last Login: Saturday, December 3, 2016 5:18 AM
Points: 45,619, Visits: 44,147
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

Post #1375266
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse