Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

IF Exists and a clause questions Expand / Collapse
Author
Message
Posted Tuesday, January 22, 2008 8:23 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, August 23, 2014 2:20 PM
Points: 30, Visits: 49
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
Post #445892
Posted Tuesday, January 22, 2008 9:27 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 26, 2012 5:26 AM
Points: 1,367, Visits: 1,585
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
Post #445943
Posted Wednesday, January 23, 2008 11:20 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 8:41 AM
Points: 438, Visits: 908
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.
Post #446558
Posted Wednesday, January 23, 2008 11:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 26, 2012 5:26 AM
Points: 1,367, Visits: 1,585
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
Post #446569
Posted Wednesday, January 23, 2008 12:03 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 8:41 AM
Points: 438, Visits: 908

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.
Post #446582
Posted Wednesday, January 23, 2008 12:28 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:05 AM
Points: 2,842, Visits: 3,875
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
Post #446591
Posted Thursday, January 24, 2008 1:46 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 26, 2012 5:26 AM
Points: 1,367, Visits: 1,585
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
Post #446780
Posted Wednesday, January 30, 2008 12:46 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:05 AM
Points: 2,842, Visits: 3,875
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
Post #449627
Posted Wednesday, January 30, 2008 11:02 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 6:34 PM
Points: 750, Visits: 3,159
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.
Post #449814
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse