<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="row-constructor-optimization">
      </a>
      10.2.1.22 Row Constructor Expression Optimization
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045228469632">
  </a>
  <a class="indexterm" name="idm46045228468144">
  </a>
  <p>
   Row constructors permit simultaneous comparisons of multiple
          values. For example, these two statements are semantically
          equivalent:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa26905863"><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-sql"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1 <span class="token keyword">WHERE</span> <span class="token punctuation">(</span>column1<span class="token punctuation">,</span>column2<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1 <span class="token keyword">WHERE</span> column1 <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">AND</span> column2 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   In addition, the optimizer handles both expressions the same
          way.
  </p>
  <p>
   The optimizer is less likely to use available indexes if the
          row constructor columns do not cover the prefix of an index.
          Consider the following table, which has a primary key on
   <code class="literal">
    (c1, c2, c3)
   </code>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa70338388"><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-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1 <span class="token punctuation">(</span>
  c1 <span class="token datatype">INT</span><span class="token punctuation">,</span> c2 <span class="token datatype">INT</span><span class="token punctuation">,</span> c3 <span class="token datatype">INT</span><span class="token punctuation">,</span> c4 <span class="token datatype">CHAR</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span><span class="token punctuation">(</span>c1<span class="token punctuation">,</span>c2<span class="token punctuation">,</span>c3<span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   In this query, the
   <code class="literal">
    WHERE
   </code>
   clause uses all
          columns in the index. However, the row constructor itself does
          not cover an index prefix, with the result that the optimizer
          uses only
   <code class="literal">
    c1
   </code>
   (
   <code class="literal">
    key_len=4
   </code>
   ,
          the size of
   <code class="literal">
    c1
   </code>
   ):
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa16919016"><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-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">EXPLAIN</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1
       <span class="token keyword">WHERE</span> c1<span class="token operator">=</span><span class="token number">1</span> <span class="token operator">AND</span> <span class="token punctuation">(</span>c2<span class="token punctuation">,</span>c3<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>\G
<span class="token output"><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span> 1. row <span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span>
           id<span class="token punctuation">:</span> 1
  select_type<span class="token punctuation">:</span> SIMPLE
        table<span class="token punctuation">:</span> t1
   partitions<span class="token punctuation">:</span> NULL
         type<span class="token punctuation">:</span> ref
possible_keys<span class="token punctuation">:</span> PRIMARY
          key<span class="token punctuation">:</span> PRIMARY
      key_len<span class="token punctuation">:</span> 4
          ref<span class="token punctuation">:</span> const
         rows<span class="token punctuation">:</span> 3
     filtered<span class="token punctuation">:</span> 100.00
        Extra<span class="token punctuation">:</span> Using where</span></code></pre>
  </div>
  <p>
   In such cases, rewriting the row constructor expression using
          an equivalent nonconstructor expression may result in more
          complete index use. For the given query, the row constructor
          and equivalent nonconstructor expressions are:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa93053526"><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-sql"><span class="token punctuation">(</span>c2<span class="token punctuation">,</span>c3<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
c2 <span class="token operator">&gt;</span> <span class="token number">1</span> <span class="token operator">OR</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>c2 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">AND</span> <span class="token punctuation">(</span>c3 <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span></code></pre>
  </div>
  <p>
   Rewriting the query to use the nonconstructor expression
          results in the optimizer using all three columns in the index
          (
   <code class="literal">
    key_len=12
   </code>
   ):
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa62719529"><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-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">EXPLAIN</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1
       <span class="token keyword">WHERE</span> c1 <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">AND</span> <span class="token punctuation">(</span>c2 <span class="token operator">&gt;</span> <span class="token number">1</span> <span class="token operator">OR</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>c2 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">AND</span> <span class="token punctuation">(</span>c3 <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>\G
<span class="token output"><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span> 1. row <span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span><span class="token punctuation">*</span>
           id<span class="token punctuation">:</span> 1
  select_type<span class="token punctuation">:</span> SIMPLE
        table<span class="token punctuation">:</span> t1
   partitions<span class="token punctuation">:</span> NULL
         type<span class="token punctuation">:</span> range
possible_keys<span class="token punctuation">:</span> PRIMARY
          key<span class="token punctuation">:</span> PRIMARY
      key_len<span class="token punctuation">:</span> 12
          ref<span class="token punctuation">:</span> NULL
         rows<span class="token punctuation">:</span> 3
     filtered<span class="token punctuation">:</span> 100.00
        Extra<span class="token punctuation">:</span> Using where</span></code></pre>
  </div>
  <p>
   Thus, for better results, avoid mixing row constructors with
   <a class="link" href="logical-operators.html#operator_and">
    <code class="literal">
     AND
    </code>
   </a>
   /
   <a class="link" href="logical-operators.html#operator_or">
    <code class="literal">
     OR
    </code>
   </a>
   expressions. Use one or the other.
  </p>
  <p>
   Under certain conditions, the optimizer can apply the range
          access method to
   <a class="link" href="comparison-operators.html#operator_in">
    <code class="literal">
     IN()
    </code>
   </a>
   expressions
          that have row constructor arguments. See
   <a class="xref" href="range-optimization.html#row-constructor-range-optimization" title="Range Optimization of Row Constructor Expressions">
    Range Optimization of Row Constructor Expressions
   </a>
   .
  </p>
 </div>
 <br/>
</div>
