Join Operations – Nested Loops

  • 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

  • Good, thorough article Jason. Thanks!

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Thanks for the article, very good read.

  • Nice job, Jason. Good explanation, and looking forward to reading about the other join types.

  • 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

  • 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

  • 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

  • 'Makes perfect sense Jason, thanks again.

    - Mark

    The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking

  • 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

  • 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 Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • 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

  • 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 Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • 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

  • 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

  • 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