Home Forums SQL Server 7,2000 T-SQL Fastest Way To Execute A Query Where We Know There Are No Results? RE: Fastest Way To Execute A Query Where We Know There Are No Results?

  • Depends. The where clause you provided would be much faster, but that would require dynamic sql. If one of the two constants is a variable (which I expect is what you meant and were simply using shorthand) then it will still perform an index SCAN which means it will walk the entire child index. The difference can be seen by looking at the execution plan in Query Analyzer with the following SQL.

    declare @test-2 int

    set @test-2 = 0

    SELECT * FROM syscolumns WHERE @test-2 = 1

    SELECT * FROM syscolumns WHERE 0 = 1

    If you are doing individual selects on the child tables (vs JOINs) an option would be to query the bits and the use IF statements to either perform the select on the child table or to perform a dummy select if the placeholder was required. If I didn't explain that clearly don't worry because I don't like the option.

    A better option, which would support joins, would be to identify a where clause against the first column in an index for the child and use a value that would be first in the index sort, but never actually exist.

    SELECT * FROM syscolumns WHERE case when @test-2 = 0 then -1 else 1 end = id

    The above will perform an index SEEK and immediately acknoledge that the key can't be found. In your case, rather than a constant (I used 1 above) you would provide the variable containing the actual key value being looked up, of course. If multiple key fields need to be tested you can use a case statement for each of the fields individually.