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 ««123»»

Join Operations – Nested Loops Expand / Collapse
Author
Message
Posted Tuesday, January 4, 2011 2:06 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:21 PM
Points: 21,228, Visits: 14,933
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1042690
Posted Tuesday, January 4, 2011 2:11 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 3:30 PM
Points: 352, Visits: 1,868
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/
Post #1042693
Posted Tuesday, January 4, 2011 2:18 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:21 PM
Points: 21,228, Visits: 14,933
Solomon Rutzky (1/4/2011)
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.


You're welcome.

My apologies if it was misleading.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1042699
Posted Friday, January 7, 2011 4:57 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:58 AM
Points: 2,036, Visits: 1,377
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
Post #1044302
Posted Friday, January 7, 2011 6:45 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, October 11, 2013 8:39 AM
Points: 58, Visits: 606
Excellent article Jason. Well done, it explains clearly and concisely how best to use the nested loop, and when it should be used.

Nic
Post #1044365
Posted Friday, January 7, 2011 8:21 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:21 PM
Points: 21,228, Visits: 14,933
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1044450
Posted Saturday, January 8, 2011 10:39 PM
SSC-Addicted

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

Group: General Forum Members
Last Login: Wednesday, August 14, 2013 3:48 PM
Points: 444, Visits: 162
Very nice article, Thanks !
Post #1044940
Posted Monday, January 10, 2011 7:19 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:21 PM
Points: 21,228, Visits: 14,933
Bharat Panthee (1/8/2011)
Very nice article, Thanks !


Thank you Bharat.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1045286
Posted Friday, February 1, 2013 8:44 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 6:13 AM
Points: 1,694, Visits: 19,550
Interesting stuff, thanks!

____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1414712
Posted Saturday, February 2, 2013 8:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 4, 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!
Post #1414977
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse