<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="nested-loop-joins">
      </a>
      10.2.1.7 Nested-Loop Join Algorithms
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045229583488">
  </a>
  <a class="indexterm" name="idm46045229582432">
  </a>
  <a class="indexterm" name="idm46045229580944">
  </a>
  <a class="indexterm" name="idm46045229579856">
  </a>
  <p>
   MySQL executes joins between tables using a nested-loop
          algorithm or variations on it.
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <a class="xref" href="nested-loop-joins.html#nested-loop-join-algorithm" title="Nested-Loop Join Algorithm">
       Nested-Loop Join Algorithm
      </a>
     </p>
    </li>
   </ul>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="nested-loop-join-algorithm">
       </a>
       Nested-Loop Join Algorithm
      </h5>
     </div>
    </div>
   </div>
   <p>
    A simple nested-loop join (NLJ) algorithm reads rows from
            the first table in a loop one at a time, passing each row to
            a nested loop that processes the next table in the join.
            This process is repeated as many times as there remain
            tables to be joined.
   </p>
   <p>
    Assume that a join between three tables
    <code class="literal">
     t1
    </code>
    ,
    <code class="literal">
     t2
    </code>
    , and
    <code class="literal">
     t3
    </code>
    is to be executed using the following
            join types:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-none"><div class="docs-select-all right" id="sa19208388"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-none">Table   Join Type
t1      range
t2      ref
t3      ALL</code></pre>
   </div>
   <p>
    If a simple NLJ algorithm is used, the join is processed
            like this:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-clike"><div class="docs-select-all right" id="sa64464486"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-clike"><span class="token keyword">for</span> each row <span class="token keyword">in</span> t1 matching range <span class="token punctuation">{</span>
  <span class="token keyword">for</span> each row <span class="token keyword">in</span> t2 matching reference key <span class="token punctuation">{</span>
    <span class="token keyword">for</span> each row <span class="token keyword">in</span> t3 <span class="token punctuation">{</span>
      <span class="token keyword">if</span> row satisfies join conditions<span class="token punctuation">,</span> send to client
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
   </div>
   <p>
    Because the NLJ algorithm passes rows one at a time from
            outer loops to inner loops, it typically reads tables
            processed in the inner loops many times.
   </p>
  </div>
 </div>
 <br/>
</div>
