January 3, 2011 at 11:25 pm
Comments posted to this topic are about the item Join Operations – Nested Loops[/url]
Thanks to those who helped review this for me. Their suggestions and insight were very helpful
Gail Shaw
Wayne Sheffield
Chris Morris
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 4, 2011 at 1:27 am
Good, thorough article Jason. Thanks!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 4, 2011 at 7:13 am
Thanks for the article, very good read.
January 4, 2011 at 8:20 am
Nice job, Jason. Good explanation, and looking forward to reading about the other join types.
January 4, 2011 at 8:38 am
Thanks Wayne, Steve and zlthomps.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 4, 2011 at 10:01 am
Well thought-out and illustrated Jason, thanks! 😎
So should we always check queries returning just a few rows to see if a forced Nested Loop would help?
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
January 4, 2011 at 10:11 am
mtillman-921105 (1/4/2011)
Well thought-out and illustrated Jason, thanks! 😎So should we always check queries returning just a few rows to see if a forced Nested Loop would help?
I would say proceed very cautiously. The DB Engine does an excellent job of determining which join operation to use. I would certainly say to verify indexes first and then check your query. If after that, you still see a performance issue - go ahead and try the hint but be careful about it.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 4, 2011 at 10:25 am
'Makes perfect sense Jason, thanks again.
- Mark
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
January 4, 2011 at 10:27 am
mtillman-921105 (1/4/2011)
'Makes perfect sense Jason, thanks again.- Mark
You're welcome.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 4, 2011 at 1:50 pm
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...
SQL# — https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
Sql Quantum Lift — https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap — https://SqlQuantumLeap.com/ ( blog )
Info sites — Collations • Module Signing • SQLCLR
January 4, 2011 at 2:06 pm
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[/url]
Learn Extended Events
January 4, 2011 at 2:11 pm
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# — https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
Sql Quantum Lift — https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap — https://SqlQuantumLeap.com/ ( blog )
Info sites — Collations • Module Signing • SQLCLR
January 4, 2011 at 2:18 pm
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[/url]
Learn Extended Events
January 7, 2011 at 4:57 am
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
January 7, 2011 at 6:45 am
Excellent article Jason. Well done, it explains clearly and concisely how best to use the nested loop, and when it should be used.
Nic
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply