NOT operator and Index Use

  • 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?

  • 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

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply