<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="function.mysqlnd_qc_get_core_stats" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>mysqlnd_qc_get_core_stats</refname>
  <refpurpose>Statistics collected by the core of the query cache</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>array</type>
   <methodname>mysqlnd_qc_get_core_stats</methodname>
   <void />
  </methodsynopsis>
  <para>
   Returns an array of statistics collected by the core of the cache plugin.
   The same data fields will be reported for any storage handler because the
   data is collected by the core.
  </para>
  <para>
   The
   <literal>PHP</literal> configuration setting
   <literal>mysqlnd_qc.collect_statistics</literal>
   controls the collection of statistics. The collection of statistics
   is disabled by default for performance reasons.
   Disabling the collection of statistics will also disable the collection
   of time related statistics.
  </para>
  <para>
   The
   <literal>PHP</literal> configuration setting
   <literal>mysqlnd_qc.collect_time_statistics</literal> controls the
   collection of time related statistics.
  </para>
  <para>
   The scope of the core statistics is the
   <literal>PHP</literal> process.
   Depending on your deployment model a
   <literal>PHP</literal> process may handle one or multiple requests.
  </para>
  <para>
   Statistics are aggregated for all cache entries. It is not possible
   to tell how much queries originating from
   <literal>mysqli</literal>,
   <literal>PDO_MySQL</literal> or
   <literal>mysql</literal> API calls have
   contributed to the aggregated data values.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  &no.function.parameters;
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
      Array of core statistics
  </para>
  <informaltable>
   <tgroup cols="3">
    <colspec colwidth="10%"/>
    <colspec colwidth="70%"/>
    <colspec colwidth="20%"/>
    <thead>
     <row>
      <entry>Statistic</entry>
      <entry>Description</entry>
      <entry>Version</entry>
     </row>
    </thead>
    <tbody>
     <row>
      <entry>
       <literal>cache_hit</literal>
      </entry>
      <entry>
       Statement is considered cacheable and cached data has been reused.
       Statement is considered cacheable and a cache miss happened but
       the statement got cached by someone else while we process it and thus
       we can fetch the result from the refreshed cache.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>cache_miss</literal>
      </entry>
      <entry>
       Statement is considered cacheable...
       <itemizedlist>
        <listitem>
         <para>
          ... and has been added to the cache
         </para>
        </listitem>
        <listitem>
         <para>
          ... but the
          <literal>PHP</literal>
          configuration directive setting of
          <literal>mysqlnd_qc.cache_no_table = 1</literal> has prevented caching.
         </para>
        </listitem>
        <listitem>
         <para>
           ... but an unbuffered result set is requested.
         </para>
        </listitem>
        <listitem>
         <para>
          ... but a buffered result set was empty.
         </para>
        </listitem>
       </itemizedlist>
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>cache_put</literal>
      </entry>
      <entry>
       Statement is considered cacheable and has been added to the cache.
       Take care when calculating derived statistics. Storage handler
       with a storage life time beyond process scope may report
       <literal>cache_put = 0</literal> together with
       <literal>cache_hit &gt; 0</literal>, if another process has filled
       the cache. You may want to use
       <literal>num_entries</literal> from
       <function>mysqlnd_qc_get_cache_info</function> if the handler
       supports it (
       <literal>default</literal>,
       <literal>APC</literal>).
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_should_cache</literal>
      </entry>
      <entry>
       Statement is considered cacheable based on query string analysis.
       The statement may or may not be added to the cache. See also
       <literal>cache_put</literal>.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_should_not_cache</literal>
      </entry>
      <entry>
       Statement is considered not cacheable based on query string analysis.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_not_cached</literal>
      </entry>
      <entry>
       Statement is considered not cacheable or it is
       considered cachable but the storage handler has not returned a
       hash key for it.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_could_cache</literal>
      </entry>
      <entry>
       Statement is considered cacheable...
       <itemizedlist>
        <listitem>
         <para>
          ... and statement has been run without errors
         </para>
        </listitem>
        <listitem>
         <para>
          ... and meta data shows at least one column in the result set
         </para>
        </listitem>
       </itemizedlist>
       The statement may or may not be in the cache already.
       It may or may not be added to the cache later on.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_found_in_cache</literal>
      </entry>
      <entry>
       Statement is considered cacheable and we have found it in the cache
       but we have not replayed the cached data yet and we have not send
       the result set to the client yet. This is not considered
       a cache hit because the client might not fetch the result or
       the cached data may be faulty.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_uncached_other</literal>
      </entry>
      <entry>
       Statement is considered cacheable and it may or may not be in
       the cache already but either replaying cached data has failed,
       no result set is available or some other error has happened.
      </entry>
     </row>
     <row>
      <entry>
       <literal>query_uncached_no_table</literal>
      </entry>
      <entry>
       Statement has not been cached because the result set has at least
       one column which has no table name in its meta data. An example of
       such a query is
       <literal>SELECT SLEEP(1)</literal>. To cache those
       statements you have to change default value of the PHP configuration directive
       <literal>mysqlnd_qc.cache_no_table</literal> and set
       <literal>mysqlnd_qc.cache_no_table = 1</literal>. Often is is not
       desired to cache such statements.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_uncached_use_result</literal>
      </entry>
      <entry>
       Statement would have been cached if a buffered result set
       had been used. The situation is also consiered as a cache miss and
       <literal>cache_miss</literal> will be incremented as well.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_aggr_run_time_cache_hit</literal>
      </entry>
      <entry>
       Aggregated run time (ms) of all cached queries.
       Cached queries are those which have incremented
       <literal>cache_hit</literal>.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_aggr_run_time_cache_put</literal>
      </entry>
      <entry>
       Aggregated run time (ms) of all uncached queries that
       have been put into the cache. See also
       <literal>cache_put</literal>.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_aggr_run_time_total</literal>
      </entry>
      <entry>
       Aggregated run time (ms) of all uncached and cached queries
       that have been inspected and executed by the query cache.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_aggr_store_time_cache_hit</literal>
      </entry>
      <entry>
       Aggregated store time (ms) of all cached queries.
       Cached queries are those which have incremented
       <literal>cache_hit</literal>.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_aggr_store_time_cache_put</literal>
      </entry>
      <entry>
       Aggregated store time (
       <literal>ms</literal>) of all uncached queries that
       have been put into the cache. See also
       <literal>cache_put</literal>.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>query_aggr_store_time_total</literal>
      </entry>
      <entry>
       Aggregated store time (ms) of all uncached and cached queries
       that have been inspected and executed by the query cache.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>receive_bytes_recorded</literal>
      </entry>
      <entry>
       Recorded incoming network traffic (
       <literal>bytes</literal>) send from MySQL to PHP.
       The traffic may or may not have been added to the cache. The
       traffic is the total for all queries regardless if cached or not.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>receive_bytes_replayed</literal>
      </entry>
      <entry>
       Network traffic replayed during cache. This is the total amount of
       incoming traffic saved because of the usage of the query cache plugin.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>send_bytes_recorded</literal>
      </entry>
      <entry>
       Recorded outgoing network traffic (
       <literal>bytes</literal>) send from MySQL to PHP.
       The traffic may or may not have been added to the cache. The
       traffic is the total for all queries regardless if cached or not.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>send_bytes_replayed</literal>
      </entry>
      <entry>
       Network traffic replayed during cache. This is the total amount of
       outgoing traffic saved because of the usage of the query cache plugin.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>slam_stale_refresh</literal>
      </entry>
      <entry>
       Number of cache misses which triggered serving stale data until
       the client causing the cache miss has refreshed the cache entry.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
     <row>
      <entry>
       <literal>slam_stale_hit</literal>
      </entry>
      <entry>
       Number of cache hits while a stale cache entry gets refreshed.
      </entry>
      <entry>Since 1.0.0.</entry>
     </row>
    </tbody>
   </tgroup>
  </informaltable>
 </refsect1>


 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <programlisting role="php">
<![CDATA[
<?php
/* Enable collection of statistics - default: disabled */
ini_set("mysqlnd_qc.collect_statistics", 1);

/* Enable collection of all timing related statistics -
default: enabled but overruled by mysqlnd_qc.collect_statistics = 0 */
ini_set("mysqlnd_qc.collect_time_statistics", 1);

/* Populate the cache, e.g. using mysqli */
$mysqli = new mysqli('host', 'user', 'password', 'schema');

/* Cache miss and cache put */
$mysqli->query("/*qc=on*/SELECT id FROM test");
/* Cache hit */
$mysqli->query("/*qc=on*/SELECT id FROM test");

/* Display core statstics */
var_dump(mysqlnd_qc_get_core_stats());
?>
]]>
   </programlisting>
   &examples.outputs;
   <screen>
<![CDATA[
array(26) {
  ["cache_hit"]=>
  string(1) "1"
  ["cache_miss"]=>
  string(1) "1"
  ["cache_put"]=>
  string(1) "1"
  ["query_should_cache"]=>
  string(1) "2"
  ["query_should_not_cache"]=>
  string(1) "0"
  ["query_not_cached"]=>
  string(1) "0"
  ["query_could_cache"]=>
  string(1) "2"
  ["query_found_in_cache"]=>
  string(1) "1"
  ["query_uncached_other"]=>
  string(1) "0"
  ["query_uncached_no_table"]=>
  string(1) "0"
  ["query_uncached_no_result"]=>
  string(1) "0"
  ["query_uncached_use_result"]=>
  string(1) "0"
  ["query_aggr_run_time_cache_hit"]=>
  string(1) "4"
  ["query_aggr_run_time_cache_put"]=>
  string(3) "395"
  ["query_aggr_run_time_total"]=>
  string(3) "399"
  ["query_aggr_store_time_cache_hit"]=>
  string(1) "2"
  ["query_aggr_store_time_cache_put"]=>
  string(1) "8"
  ["query_aggr_store_time_total"]=>
  string(2) "10"
  ["receive_bytes_recorded"]=>
  string(2) "65"
  ["receive_bytes_replayed"]=>
  string(2) "65"
  ["send_bytes_recorded"]=>
  string(2) "29"
  ["send_bytes_replayed"]=>
  string(2) "29"
  ["slam_stale_refresh"]=>
  string(1) "0"
  ["slam_stale_hit"]=>
  string(1) "0"
  ["request_counter"]=>
  int(1)
  ["process_hash"]=>
  int(3547549858)
}

]]>
   </screen>

  </example>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member>
     <link linkend="mysqlnd-qc.configuration">Runtime configuration</link>
    </member>
    <member>
     <link linkend="ini.mysqlnd-qc.collect-statistics">mysqlnd_qc.collect_statistics</link>
    </member>
    <member>
     <link linkend="ini.mysqlnd-qc.time-statistics">mysqlnd_qc.time_statistics</link>
    </member>
   </simplelist>
  </para>
 </refsect1>

</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->