|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
Solomon Rutzky (1/4/2011) Hey Jason. Nice article and one question. In your two main examples the difference is the WHERE condition that constrains the results to 10 rows as opposed to the full 10,000 in the table. Is it fair to compare the query times (and make implications on the differences of the JOIN types) given that they are different queries? One is asked to get 10 rows and the other query gets all 10,000 so naturally they would not take the same amount of time, right? Maybe that is not the point you were trying to get across to begin with, but my initial thought as to the speed increase wasn't that it was due to the different JOIN type but instead to only pulling 10 rows. I wonder if there is a way to show two queries that pull the same amount of rows but are written differently so as to force the different JOIN types (Merge vs Nested Loop).
Thanks and take care, Solomon...
True it is a bit unfair to illustrate it that way (I alluded to that unfairness as well). An important part of that comparison is to show how the query optimizer changes the join operator when fewer records are required. Since an indexed nested loops works better with fewer records, the optimizer will choose that. That was really the main point.
Jason AKA CirqueDeSQLeil I have given a name to my pain... MCM SQL Server 2008
SQL RNNR
Posting Performance Based Questions - Gail Shaw Posting Data Etiquette - Jeff Moden Hidden RBAR - Jeff Moden VLFs and the Tran Log - Kimberly Tripp
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: 2 days ago @ 11:12 AM
Points: 285,
Visits: 1,377
|
|
CirquedeSQLeil (1/4/2011) True it is a bit unfair to illustrate it that way (I alluded to that unfairness as well). An important part of that comparison is to show how the query optimizer changes the join operator when fewer records are required. Since an indexed nested loops works better with fewer records, the optimizer will choose that. That was really the main point.
Got it. Thanks.
SQL# - http://www.SQLsharp.com/
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Today @ 1:50 AM
Points: 1,449,
Visits: 756
|
|
| Jason, thanks for the article. Just one point: after forcing the optimizer to use a Nested Loop you state, By trying to force the optimizer to use a Nested Loops where the query didn't really warrant it, we did not improve the query and it could be argued that we caused more work to be performed.
Yet you've improved the query time (compared to when no query hint was used) by nearly 50%. Of course the logical reads have gone through the roof and that may or may not be a problem depending on the amount of memory and CPU on the box in question, but if it's just query execution time you're interested in, I would argue that you have improved it.
I definitley agree that in the vast majority of cases one should leave the optimizer to pick the 'best' plan (we should really say 'optimal' as it may take way too long to actually find the 'best' plan) and use query hints with extreme caution.
Thanks Lempster
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Today @ 7:31 AM
Points: 58,
Visits: 540
|
|
Excellent article Jason. Well done, it explains clearly and concisely how best to use the nested loop, and when it should be used.
Nic
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
Lempster (1/7/2011)
Jason, thanks for the article. Just one point: after forcing the optimizer to use a Nested Loop you state, By trying to force the optimizer to use a Nested Loops where the query didn't really warrant it, we did not improve the query and it could be argued that we caused more work to be performed.
Yet you've improved the query time (compared to when no query hint was used) by nearly 50%. Of course the logical reads have gone through the roof and that may or may not be a problem depending on the amount of memory and CPU on the box in question, but if it's just query execution time you're interested in, I would argue that you have improved it. I definitley agree that in the vast majority of cases one should leave the optimizer to pick the 'best' plan (we should really say 'optimal' as it may take way too long to actually find the 'best' plan) and use query hints with extreme caution. Thanks Lempster
Good points. It was due to the increased logical reads that one may argue that more work is being done. But yes, based on execution time, you are correct.
Thanks for the comments.
Jason AKA CirqueDeSQLeil I have given a name to my pain... MCM SQL Server 2008
SQL RNNR
Posting Performance Based Questions - Gail Shaw Posting Data Etiquette - Jeff Moden Hidden RBAR - Jeff Moden VLFs and the Tran Log - Kimberly Tripp
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, May 28, 2012 11:28 AM
Points: 444,
Visits: 157
|
|
| Very nice article, Thanks !
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Today @ 1:17 PM
Points: 1,499,
Visits: 18,159
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, February 04, 2013 6:33 PM
Points: 6,
Visits: 8
|
|
Hi there.
I appreciate you taking the time to go in detail regarding the hows and whys of optimization in this scenario. While i cannot attest to your example at the moment, i will try in the future.
Thanks!
|
|
|
|