NULL toggles off an index

  • Hi folks!

    I'm experiencing a trouble with nulls. I have a table which has index over a column which could contain nulls.

    When I use a statement like this one:

    select * from table where TheColumn =

    index is used. But when I use another kind of statement:

    select * from table where TheColumn is not null

    the index is NOT used.

    It might be a workaround for the problem is to use some constant value to determine nulls, but is not my case 'cause I can't do this for now. But I'd like index to be used in the latest case.

    Is there a way to get it worked?

  • Hi,

    Try any one of this. First one should work.

    1. select * from table with (index(myidxname)) where thecolumn is not null

    or

    2.

    declare @thecolvalue integer

    set @thecolvalue=null

    select * from table where thecolumn=@thecolvalue

    [font="Verdana"]Thanks
    Chandra Mohan[/font]

  • isn't the table scan and not using the index due to the SARGable condition?(Search ARGument)

    column = @value can be used to search as boolean match to the index

    column is not null does not resolve as able to boolean match an index, so a scan must be used.

    AFAIK, any time you have a NOT in the where statemnt, a scan will be used to check all values, right?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • NULL values are not your friend when it comes to indexes. You will get a scan. Even forcing the use of the index through the hint will result in a scan, and it will probably include a key lookup with the scan.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • hi Grant

    please see the below loink's problem

    http://www.sqlservercentral.com/Forums/FindPost761384.aspx

    thanks

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

Viewing 5 posts - 1 through 4 (of 4 total)

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