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


Join Operations – Nested Loops


Join Operations – Nested Loops

Author
Message
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65873 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Solomon Rutzky
Solomon Rutzky
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3302 Visits: 3029
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/
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65873 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Lempster
Lempster
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5053 Visits: 1657
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
InvisibleCat
InvisibleCat
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 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
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65873 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Bharat Panthee
Bharat Panthee
Mr or Mrs. 500
Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)Mr or Mrs. 500 (584 reputation)

Group: General Forum Members
Points: 584 Visits: 162
Very nice article, Thanks !
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65873 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6357 Visits: 25530
Interesting stuff, thanks!

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

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




nopeqwerty123
nopeqwerty123
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 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!
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