<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="explain-for-connection">
      </a>
      10.8.4 Obtaining Execution Plan Information for a Named Connection
     </h3>
    </div>
   </div>
  </div>
  <p>
   To obtain the execution plan for an explainable statement
        executing in a named connection, use this statement:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa45157127"><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">EXPLAIN</span> <span class="token punctuation">[</span><span class="token keyword"><em class="replaceable">options</em></span><span class="token punctuation">]</span> <span class="token keyword">FOR</span> <span class="token keyword">CONNECTION</span> <em class="replaceable">connection_id</em><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   <a class="link" href="explain-for-connection.html" title="10.8.4 Obtaining Execution Plan Information for a Named Connection">
    <code class="literal">
     EXPLAIN FOR CONNECTION
    </code>
   </a>
   returns
        the
   <a class="link" href="explain.html" title="15.8.2 EXPLAIN Statement">
    <code class="literal">
     EXPLAIN
    </code>
   </a>
   information that is
        currently being used to execute a query in a given connection.
        Because of changes to data (and supporting statistics) it may
        produce a different result from running
   <a class="link" href="explain.html" title="15.8.2 EXPLAIN Statement">
    <code class="literal">
     EXPLAIN
    </code>
   </a>
   on the equivalent query
        text. This difference in behavior can be useful in diagnosing
        more transient performance problems. For example, if you are
        running a statement in one session that is taking a long time to
        complete, using
   <a class="link" href="explain-for-connection.html" title="10.8.4 Obtaining Execution Plan Information for a Named Connection">
    <code class="literal">
     EXPLAIN FOR
        CONNECTION
    </code>
   </a>
   in another session may yield useful
        information about the cause of the delay.
  </p>
  <p>
   <em class="replaceable">
    <code>
     connection_id
    </code>
   </em>
   is the connection
        identifier, as obtained from the
   <code class="literal">
    INFORMATION_SCHEMA
   </code>
   <a class="link" href="information-schema-processlist-table.html" title="28.3.23 The INFORMATION_SCHEMA PROCESSLIST Table">
    <code class="literal">
     PROCESSLIST
    </code>
   </a>
   table or the
   <a class="link" href="show-processlist.html" title="15.7.7.31 SHOW PROCESSLIST Statement">
    <code class="literal">
     SHOW PROCESSLIST
    </code>
   </a>
   statement. If
        you have the
   <a class="link" href="privileges-provided.html#priv_process">
    <code class="literal">
     PROCESS
    </code>
   </a>
   privilege,
        you can specify the identifier for any connection. Otherwise,
        you can specify the identifier only for your own connections. In
        all cases, you must have sufficient privileges to explain the
        query on the specified connection.
  </p>
  <p>
   If the named connection is not executing a statement, the result
        is empty. Otherwise,
   <code class="literal">
    EXPLAIN FOR CONNECTION
   </code>
   applies only if the statement being executed in the named
        connection is explainable. This includes
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   ,
   <a class="link" href="delete.html" title="15.2.2 DELETE Statement">
    <code class="literal">
     DELETE
    </code>
   </a>
   ,
   <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
    <code class="literal">
     INSERT
    </code>
   </a>
   ,
   <a class="link" href="replace.html" title="15.2.12 REPLACE Statement">
    <code class="literal">
     REPLACE
    </code>
   </a>
   , and
   <a class="link" href="update.html" title="15.2.17 UPDATE Statement">
    <code class="literal">
     UPDATE
    </code>
   </a>
   . (However,
   <code class="literal">
    EXPLAIN FOR CONNECTION
   </code>
   does not work for
        prepared statements, even prepared statements of those types.)
  </p>
  <p>
   If the named connection is executing an explainable statement,
        the output is what you would obtain by using
   <code class="literal">
    EXPLAIN
   </code>
   on the statement itself.
  </p>
  <p>
   If the named connection is executing a statement that is not
        explainable, an error occurs. For example, you cannot name the
        connection identifier for your current session because
   <code class="literal">
    EXPLAIN
   </code>
   is not explainable:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa24059134"><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">SELECT</span> <span class="token function">CONNECTION_ID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<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>
<span class="token output"><span class="token punctuation">|</span> CONNECTION_ID() <span class="token punctuation">|</span></span>
<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>
<span class="token output"><span class="token punctuation">|</span>             373 <span class="token punctuation">|</span></span>
<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>
<span class="token output">1 row in set (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">EXPLAIN</span> <span class="token keyword">FOR</span> <span class="token keyword">CONNECTION</span> <span class="token number">373</span><span class="token punctuation">;</span>
<span class="token output">ERROR 1889 (HY000)<span class="token punctuation">:</span> EXPLAIN FOR CONNECTION command is supported
only for SELECT/UPDATE/INSERT/DELETE/REPLACE</span></code></pre>
  </div>
  <p>
   The
   <code class="literal">
    Com_explain_other
   </code>
   status variable
        indicates the number of
   <a class="link" href="explain.html" title="15.8.2 EXPLAIN Statement">
    <code class="literal">
     EXPLAIN FOR
        CONNECTION
    </code>
   </a>
   statements executed.
  </p>
 </div>
 <br/>
</div>
