|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 27, 2010 1:31 PM
Points: 132,
Visits: 101
|
|
| Comments posted to this topic are about the item Query cost
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Thursday, January 05, 2012 2:05 AM
Points: 488,
Visits: 335
|
|
I had chosen - "both are equally cost-effective" simply because I felt that the correlated subquery still needs to check for the existence of the equality condition for which the passes through the student and teacher tables will have to be made for every outer row. Whereas the simple subquery will have to be evaluated only once and then the IN operator would kick in. Somehow - without having any rigourous fundamentals backing my theory - I felt the queries would perform equally well and so I chose the third option.
Even though I knew that the question was testing the usage of EXISTS - I wasn't convinced that the 2nd query would perform appreciably better than the first query.
Anyway - I stand corrected.
I had not confirmed the execution plan in SSMS. Now that Cirque has confirmed what I felt intuitively I will go ahead and look at it.
Saurabh Dwivedy ___________________________________________________________
My Blog: http://tinyurl.com/dwivedys
For better, quicker answers, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537
Be Happy!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 1,103,
Visits: 1,199
|
|
I answered OK, but... The first variant with IN can be more effective, it allways depend on data. Compare two queries without any knowledge about structure of tables, indexes, possible data distributions...
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 2,659,
Visits: 720
|
|
This used to be true, and I have often used this knowledge to optimise slow-running queries. But since SQL 2005 it no longer matters from a performance point of view. It's still good practice to use EXISTS, though, as it expresses the intent more clearly.
Just because you're right doesn't mean everybody else is wrong.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:05 AM
Points: 1,103,
Visits: 1,199
|
|
CirquedeSQLeil (1/27/2010)
honza.mf (1/27/2010) I answered OK, but... The first variant with IN can be more effective, it allways depend on data. Compare two queries without any knowledge about structure of tables, indexes, possible data distributions...This is a very valid point - the answer really is an It Depends kind of answer.
Thanks.
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 1:51 AM
Points: 1,972,
Visits: 1,821
|
|
Volumes of data returned from tables may vary (test or production database), so a well performing query may slow down or give an error.
FROM BOL: Including an extremely large number of values (many thousands) in an IN clause can consume resources and return errors 8623 or 8632. To work around this problem, store the items in the IN list in a table.
Any null values returned by subquery or expression that are compared to test_expression using IN or NOT IN return UNKNOWN. Using null values in together with IN or NOT IN can produce unexpected results.
I prefer EXISTS instead of IN + subquery because of performance.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 9:52 AM
Points: 1,356,
Visits: 4,761
|
|
I recently optimised a query and tried exactly these two constructs. The query plan for both was identical. So the correct answer is either "they are equal", or (more likely I suspect) "it depends on the data".
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 3:58 AM
Points: 3,191,
Visits: 4,149
|
|
Rune Bivrin (1/27/2010) This used to be true, and I have often used this knowledge to optimise slow-running queries. But since SQL 2005 it no longer matters from a performance point of view. I checked this on SQL Server 2000 (SP4, 8.00.2039) and it produced fully identical execution plans. Seems like the database engine 2000 is smart enough Maybe this was one of the improvements in Service Pack 4.
Carlo Romagnano (1/27/2010)
FROM BOL: Including an extremely large number of values (many thousands) in an IN clause can consume resources and return errors 8623 or 8632. This may happen only when a list of values is used in the IN statement. The author used a subquery, so it's O.K.
|
|
|
|