<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="searching-on-two-keys">
      </a>
      5.6.7 Searching on Two Keys
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045324624288">
  </a>
  <a class="indexterm" name="idm46045324623248">
  </a>
  <a class="indexterm" name="idm46045324622176">
  </a>
  <a class="indexterm" name="idm46045324620688">
  </a>
  <p>
   An
   <a class="link" href="logical-operators.html#operator_or">
    <code class="literal">
     OR
    </code>
   </a>
   using a single key is well
        optimized, as is the handling of
   <a class="link" href="logical-operators.html#operator_and">
    <code class="literal">
     AND
    </code>
   </a>
   .
  </p>
  <p>
   The one tricky case is that of searching on two different keys
        combined with
   <a class="link" href="logical-operators.html#operator_or">
    <code class="literal">
     OR
    </code>
   </a>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa52073969"><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> field1_index<span class="token punctuation">,</span> field2_index <span class="token keyword">FROM</span> test_table
<span class="token keyword">WHERE</span> field1_index <span class="token operator">=</span> <span class="token string">'1'</span> <span class="token operator">OR</span>  field2_index <span class="token operator">=</span> <span class="token string">'1'</span></code></pre>
  </div>
  <p>
   This case is optimized. See
   <a class="xref" href="index-merge-optimization.html" title="10.2.1.3 Index Merge Optimization">
    Section 10.2.1.3, “Index Merge Optimization”
   </a>
   .
  </p>
  <p>
   You can also solve the problem efficiently by using a
   <a class="link" href="union.html" title="15.2.18 UNION Clause">
    <code class="literal">
     UNION
    </code>
   </a>
   that combines the output of
        two separate
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   statements.
        See
   <a class="xref" href="union.html" title="15.2.18 UNION Clause">
    Section 15.2.18, “UNION Clause”
   </a>
   .
  </p>
  <p>
   Each
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   searches only one key
        and can be optimized:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa65784381"><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> field1_index<span class="token punctuation">,</span> field2_index
    <span class="token keyword">FROM</span> test_table <span class="token keyword">WHERE</span> field1_index <span class="token operator">=</span> <span class="token string">'1'</span>
<span class="token keyword">UNION</span>
<span class="token keyword">SELECT</span> field1_index<span class="token punctuation">,</span> field2_index
    <span class="token keyword">FROM</span> test_table <span class="token keyword">WHERE</span> field2_index <span class="token operator">=</span> <span class="token string">'1'</span><span class="token punctuation">;</span></code></pre>
  </div>
 </div>
 <br/>
</div>
