SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


IF Exists and a clause questions


IF Exists and a clause questions

Author
Message
dejava
dejava
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 68
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
Andras Belokosztolszki
Andras Belokosztolszki
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5395 Visits: 1585
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
antonio.collins
antonio.collins
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2196 Visits: 921
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.
Andras Belokosztolszki
Andras Belokosztolszki
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5395 Visits: 1585
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
antonio.collins
antonio.collins
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2196 Visits: 921

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.
Christian Buettner-167247
Christian Buettner-167247
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5255 Visits: 3889
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
Andras Belokosztolszki
Andras Belokosztolszki
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5395 Visits: 1585
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 Sad. 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
Christian Buettner-167247
Christian Buettner-167247
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5255 Visits: 3889
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
matt stockham
matt stockham
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2588 Visits: 3178
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search