IF Exists and a clause questions

  • Hi all,

    I am new here, and glad that we have a good community like this. I have 2 simple questions:

    First Question:

    which one is faster ?

    IF Exists(select * from Table1) ....

    or

    IF Exists(select TOP 1 * from Table1 WHERE ID=x) ....

    The real question is, does SQL engine know that it only needs to find 1 record to answer the EXISTS function ? Or it needs to make a set first before answering EXISTS function ?

    Second Question:

    similar with fist question.

    SELECT * FROM Table1 WHERE (@S=1 AND CONTAINS(column1,'key1')) OR (@S=2 AND CONTAINS(column2,'key1')) OR (@S=3 AND CONTAINS(column3,'key1'))

    Does SQL engine will search (CONTAINS) only in 1 column depending on the value of @S ? Or it will search on on all columns, then coming back to see the boolean of the clauses ?

    As we know that in old VB they do not have ANDAALSO, ORELSE which make the compiler/interpreter execute other clauses to see the results. What is the case in SQL query?

    regards

    cha

  • For the first question the answer is that using top 1 is often resulting in a faster execution. (in a simple test case using top 1 resulted in fewer logical reads)

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • nugroho (1/22/2008)


    Hi all,

    Second Question:

    SELECT * FROM Table1 WHERE (@S=1 AND CONTAINS(column1,'key1')) OR (@S=2 AND CONTAINS(column2,'key1')) OR (@S=3 AND CONTAINS(column3,'key1'))

    Does SQL engine will search (CONTAINS) only in 1 column depending on the value of @S ? Or it will search on on all columns, then coming back to see the boolean of the clauses ?

    that should work well since the comparison to @S should be resolved without even considering any rows. so only the desired full text search should take run.

  • antonio.collins (1/23/2008)


    nugroho (1/22/2008)


    Hi all,

    Second Question:

    SELECT * FROM Table1 WHERE (@S=1 AND CONTAINS(column1,'key1')) OR (@S=2 AND CONTAINS(column2,'key1')) OR (@S=3 AND CONTAINS(column3,'key1'))

    Does SQL engine will search (CONTAINS) only in 1 column depending on the value of @S ? Or it will search on on all columns, then coming back to see the boolean of the clauses ?

    that should work well since the comparison to @S should be resolved without even considering any rows. so only the desired full text search should take run.

    In T-SQL there is no guarantee about the order of how OR and AND statements are evaluated. Of course precedence rules are observed, and the result will also stay correct, but the optimizer may change the order arbitrarily when it is allowed.

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • In T-SQL there is no guarantee about the order of how OR and AND statements are evaluated. Of course precedence rules are observed, and the result will also stay correct, but the optimizer may change the order arbitrarily when it is allowed.

    it is true that evaluation order within an expression cannot be predicted, but you can pretty much depend on the scalar comparison to a literal value (@s = 1) being performed first. take the longest running query you have and add "and 1=2" to its where clause and it will fly.

  • Hello Andras,

    could you please provide an example where TOP 1 speeds up the query?

    I was always under the assumption that SQL Server would automatically TOP the query when EXISTS is used.

    Thanks!

    Best Regards,

    Chris Büttner

  • Christian Buettner (1/23/2008)


    Hello Andras,

    could you please provide an example where TOP 1 speeds up the query?

    I was always under the assumption that SQL Server would automatically TOP the query when EXISTS is used.

    Thanks!

    Hmmm, I've just spent 10 minutest trying to reproduce this, and no luck :(. It was one of the smaller system tables I've checked before, and the difference was 1 logical read with TOP, 2 logical reads without. But I'm starting to doubt this now myself, having tried to reproduce this with tables ranging from 2 to 8 pages. Maybe to condition I used was more complex? With larger tables, as expected, the number of reads is the same.

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • Hello Andras,

    I did some more tests with queries like the following:

    SELECT 1 WHERE EXISTS(SELECT * FROM sys.objects )

    SELECT 1 WHERE EXISTS(SELECT TOP 1 * FROM sys.objects )

    It seems to make no difference, regardless of the table size.

    If I remember correctly, it was one of Itzik's books where I read about this behaviour but I cannot tell for sure.

    Best Regards,

    Chris Büttner

  • Regarding the second item, the optimizer is smart enough to know what to look for so that it won't check unnecessary items. I believe that you may get a different plan if you use 2 UNION statements instead of the OR conditions.

Viewing 9 posts - 1 through 8 (of 8 total)

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