Zend_Db_Profiler.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.profiler" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <title>Zend_Db_Profiler</title>
  5. <sect2 id="zend.db.profiler.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Db_Profiler</classname> can be enabled to allow profiling of
  9. queries. Profiles include the queries processed by the adapter as
  10. well as elapsed time to run the queries, allowing inspection of the
  11. queries that have been performed without needing to add extra
  12. debugging code to classes. Advanced usage also allows the
  13. developer to filter which queries are profiled.
  14. </para>
  15. <para>
  16. Enable the profiler by either passing a directive to the adapter
  17. constructor, or by asking the adapter to enable it later.
  18. </para>
  19. <programlisting role="php"><![CDATA[
  20. $params = array(
  21. 'host' => '127.0.0.1',
  22. 'username' => 'webuser',
  23. 'password' => 'xxxxxxxx',
  24. 'dbname' => 'test'
  25. 'profiler' => true // turn on profiler
  26. // set to false to disable (disabled by default)
  27. );
  28. $db = Zend_Db::factory('PDO_MYSQL', $params);
  29. // turn off profiler:
  30. $db->getProfiler()->setEnabled(false);
  31. // turn on profiler:
  32. $db->getProfiler()->setEnabled(true);
  33. ]]></programlisting>
  34. <para>
  35. The value of the '<code>profiler</code>' option is flexible. It is interpreted differently depending on its
  36. type. Most often, you should use a simple boolean value, but other types enable you to customize the
  37. profiler behavior.
  38. </para>
  39. <para>
  40. A boolean argument sets the profiler to enabled if it is a <code>true</code> value, or disabled if
  41. <code>false</code>. The profiler class is the adapter's default profiler class,
  42. <classname>Zend_Db_Profiler</classname>.
  43. <programlisting role="php"><![CDATA[
  44. $params['profiler'] = true;
  45. $db = Zend_Db::factory('PDO_MYSQL', $params);
  46. ]]></programlisting>
  47. </para>
  48. <para>
  49. An instance of a profiler object makes the adapter use that object. The object type must be
  50. <classname>Zend_Db_Profiler</classname> or a subclass thereof. Enabling the profiler is done separately.
  51. <programlisting role="php"><![CDATA[
  52. $profiler = MyProject_Db_Profiler();
  53. $profiler->setEnabled(true);
  54. $params['profiler'] = $profiler;
  55. $db = Zend_Db::factory('PDO_MYSQL', $params);
  56. ]]></programlisting>
  57. </para>
  58. <para>
  59. The argument can be an associative array containing any or all of the keys '<code>enabled</code>',
  60. '<code>instance</code>', and '<code>class</code>'. The '<code>enabled</code>' and '<code>instance</code>'
  61. keys correspond to the boolean and instance types documented above. The '<code>class</code>' key is used
  62. to name a class to use for a custom profiler. The class must be <classname>Zend_Db_Profiler</classname> or a
  63. subclass. The class is instantiated with no constructor arguments. The '<code>class</code>' option is
  64. ignored when the '<code>instance</code>' option is supplied.
  65. <programlisting role="php"><![CDATA[
  66. $params['profiler'] = array(
  67. 'enabled' => true,
  68. 'class' => 'MyProject_Db_Profiler'
  69. );
  70. $db = Zend_Db::factory('PDO_MYSQL', $params);
  71. ]]></programlisting>
  72. </para>
  73. <para>
  74. Finally, the argument can be an object of type <classname>Zend_Config</classname> containing properties, which are treated as the array keys described above. For example, a file "config.ini" might contain the following data:
  75. <programlisting role="ini"><![CDATA[
  76. [main]
  77. db.profiler.class = "MyProject_Db_Profiler"
  78. db.profiler.enabled = true
  79. ]]></programlisting>
  80. This configuration can be applied by the following PHP code:
  81. <programlisting role="php"><![CDATA[
  82. $config = new Zend_Config_Ini('config.ini', 'main');
  83. $params['profiler'] = $config->db->profiler;
  84. $db = Zend_Db::factory('PDO_MYSQL', $params);
  85. ]]></programlisting>
  86. The '<code>instance</code>' property may be used as in the following:
  87. <programlisting role="php"><![CDATA[
  88. $profiler = new MyProject_Db_Profiler();
  89. $profiler->setEnabled(true);
  90. $configData = array(
  91. 'instance' => $profiler
  92. );
  93. $config = new Zend_Config($configData);
  94. $params['profiler'] = $config;
  95. $db = Zend_Db::factory('PDO_MYSQL', $params);
  96. ]]></programlisting>
  97. </para>
  98. </sect2>
  99. <sect2 id="zend.db.profiler.using">
  100. <title>Using the Profiler</title>
  101. <para>
  102. At any point, grab the profiler using the adapter's
  103. <code>getProfiler()</code> method:
  104. </para>
  105. <programlisting role="php"><![CDATA[
  106. $profiler = $db->getProfiler();
  107. ]]></programlisting>
  108. <para>
  109. This returns a <classname>Zend_Db_Profiler</classname> object instance. With
  110. that instance, the developer can examine your queries using a
  111. variety of methods:
  112. </para>
  113. <itemizedlist>
  114. <listitem>
  115. <para>
  116. <code>getTotalNumQueries()</code> returns the total number
  117. of queries that have been profiled.
  118. </para>
  119. </listitem>
  120. <listitem>
  121. <para>
  122. <code>getTotalElapsedSecs()</code> returns the total
  123. number of seconds elapsed for all profiled queries.
  124. </para>
  125. </listitem>
  126. <listitem>
  127. <para>
  128. <code>getQueryProfiles()</code> returns an array of all
  129. query profiles.
  130. </para>
  131. </listitem>
  132. <listitem>
  133. <para>
  134. <code>getLastQueryProfile()</code> returns the last (most
  135. recent) query profile, regardless of whether or not the query
  136. has finished (if it hasn't, the end time will be null)
  137. </para>
  138. </listitem>
  139. <listitem>
  140. <para>
  141. <code>clear()</code> clears any past query profiles
  142. from the stack.
  143. </para>
  144. </listitem>
  145. </itemizedlist>
  146. <para>
  147. The return value of <code>getLastQueryProfile()</code> and the
  148. individual elements of <code>getQueryProfiles()</code> are
  149. <classname>Zend_Db_Profiler_Query</classname> objects, which provide the
  150. ability to inspect the individual queries themselves:
  151. </para>
  152. <itemizedlist>
  153. <listitem>
  154. <para>
  155. <code>getQuery()</code> returns the SQL text of the query.
  156. The SQL text of a prepared statement with parameters is the
  157. text at the time the query was prepared, so it contains
  158. parameter placeholders, not the values used when the
  159. statement is executed.
  160. </para>
  161. </listitem>
  162. <listitem>
  163. <para>
  164. <code>getQueryParams()</code> returns an array of
  165. parameter values used when executing a prepared query.
  166. This includes both bound parameters and arguments to the
  167. statement's <code>execute()</code> method. The keys of
  168. the array are the positional (1-based) or named (string)
  169. parameter indices.
  170. </para>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. <code>getElapsedSecs()</code> returns the number of
  175. seconds the query ran.
  176. </para>
  177. </listitem>
  178. </itemizedlist>
  179. <para>
  180. The information <classname>Zend_Db_Profiler</classname> provides is useful for
  181. profiling bottlenecks in applications, and for debugging queries
  182. that have been run. For instance, to see the exact query that was
  183. last run:
  184. </para>
  185. <programlisting role="php"><![CDATA[
  186. $query = $profiler->getLastQueryProfile();
  187. echo $query->getQuery();
  188. ]]></programlisting>
  189. <para>
  190. Perhaps a page is generating slowly; use the profiler to determine
  191. first the total number of seconds of all queries, and then step
  192. through the queries to find the one that ran longest:
  193. </para>
  194. <programlisting role="php"><![CDATA[
  195. $totalTime = $profiler->getTotalElapsedSecs();
  196. $queryCount = $profiler->getTotalNumQueries();
  197. $longestTime = 0;
  198. $longestQuery = null;
  199. foreach ($profiler->getQueryProfiles() as $query) {
  200. if ($query->getElapsedSecs() > $longestTime) {
  201. $longestTime = $query->getElapsedSecs();
  202. $longestQuery = $query->getQuery();
  203. }
  204. }
  205. echo 'Executed ' . $queryCount . ' queries in ' . $totalTime .
  206. ' seconds' . "\n";
  207. echo 'Average query length: ' . $totalTime / $queryCount .
  208. ' seconds' . "\n";
  209. echo 'Queries per second: ' . $queryCount / $totalTime . "\n";
  210. echo 'Longest query length: ' . $longestTime . "\n";
  211. echo "Longest query: \n" . $longestQuery . "\n";
  212. ]]></programlisting>
  213. </sect2>
  214. <sect2 id="zend.db.profiler.advanced">
  215. <title>Advanced Profiler Usage</title>
  216. <para>
  217. In addition to query inspection, the profiler also allows the
  218. developer to filter which queries get profiled. The following
  219. methods operate on a <classname>Zend_Db_Profiler</classname> instance:
  220. </para>
  221. <sect3 id="zend.db.profiler.advanced.filtertime">
  222. <title>Filter by query elapsed time</title>
  223. <para>
  224. <code>setFilterElapsedSecs()</code> allows the developer to set
  225. a minimum query time before a query is profiled. To remove the
  226. filter, pass the method a null value.
  227. </para>
  228. <programlisting role="php"><![CDATA[
  229. // Only profile queries that take at least 5 seconds:
  230. $profiler->setFilterElapsedSecs(5);
  231. // Profile all queries regardless of length:
  232. $profiler->setFilterElapsedSecs(null);
  233. ]]></programlisting>
  234. </sect3>
  235. <sect3 id="zend.db.profiler.advanced.filtertype">
  236. <title>Filter by query type</title>
  237. <para>
  238. <code>setFilterQueryType()</code> allows the developer to set
  239. which types of queries should be profiled; to profile multiple
  240. types, logical OR them. Query types are defined as the following
  241. <classname>Zend_Db_Profiler</classname> constants:
  242. </para>
  243. <itemizedlist>
  244. <listitem>
  245. <para>
  246. <classname>Zend_Db_Profiler::CONNECT</classname>: connection
  247. operations, or selecting a database.
  248. </para>
  249. </listitem>
  250. <listitem>
  251. <para>
  252. <classname>Zend_Db_Profiler::QUERY</classname>: general database
  253. queries that do not match other types.
  254. </para>
  255. </listitem>
  256. <listitem>
  257. <para>
  258. <classname>Zend_Db_Profiler::INSERT</classname>: any query that
  259. adds new data to the database, generally SQL INSERT.
  260. </para>
  261. </listitem>
  262. <listitem>
  263. <para>
  264. <classname>Zend_Db_Profiler::UPDATE</classname>: any query that
  265. updates existing data, usually SQL UPDATE.
  266. </para>
  267. </listitem>
  268. <listitem>
  269. <para>
  270. <classname>Zend_Db_Profiler::DELETE</classname>: any query that
  271. deletes existing data, usually SQL DELETE.
  272. </para>
  273. </listitem>
  274. <listitem>
  275. <para>
  276. <classname>Zend_Db_Profiler::SELECT</classname>: any query that
  277. retrieves existing data, usually SQL SELECT.
  278. </para>
  279. </listitem>
  280. <listitem>
  281. <para>
  282. <classname>Zend_Db_Profiler::TRANSACTION</classname>: any
  283. transactional operation, such as start transaction, commit,
  284. or rollback.
  285. </para>
  286. </listitem>
  287. </itemizedlist>
  288. <para>
  289. As with <code>setFilterElapsedSecs()</code>, you can remove any
  290. existing filters by passing <code>null</code> as the sole
  291. argument.
  292. </para>
  293. <programlisting role="php"><![CDATA[
  294. // profile only SELECT queries
  295. $profiler->setFilterQueryType(Zend_Db_Profiler::SELECT);
  296. // profile SELECT, INSERT, and UPDATE queries
  297. $profiler->setFilterQueryType(Zend_Db_Profiler::SELECT |
  298. Zend_Db_Profiler::INSERT |
  299. Zend_Db_Profiler::UPDATE);
  300. // profile DELETE queries
  301. $profiler->setFilterQueryType(Zend_Db_Profiler::DELETE);
  302. // Remove all filters
  303. $profiler->setFilterQueryType(null);
  304. ]]></programlisting>
  305. </sect3>
  306. <sect3 id="zend.db.profiler.advanced.getbytype">
  307. <title>Retrieve profiles by query type</title>
  308. <para>
  309. Using <code>setFilterQueryType()</code> can cut down on the
  310. profiles generated. However, sometimes it can be more useful to
  311. keep all profiles, but view only those you need at a given
  312. moment. Another feature of <code>getQueryProfiles()</code> is
  313. that it can do this filtering on-the-fly, by passing a query
  314. type (or logical combination of query types) as its first
  315. argument; see <xref linkend="zend.db.profiler.advanced.filtertype" />
  316. for a list of the query type constants.
  317. </para>
  318. <programlisting role="php"><![CDATA[
  319. // Retrieve only SELECT query profiles
  320. $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT);
  321. // Retrieve only SELECT, INSERT, and UPDATE query profiles
  322. $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT |
  323. Zend_Db_Profiler::INSERT |
  324. Zend_Db_Profiler::UPDATE);
  325. // Retrieve DELETE query profiles
  326. $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::DELETE);
  327. ]]></programlisting>
  328. </sect3>
  329. </sect2>
  330. <sect2 id="zend.db.profiler.profilers">
  331. <title>Specialized Profilers</title>
  332. <para>
  333. A Specialized Profiler is an object that inherits from
  334. <classname>Zend_Db_Profiler</classname>. Specialized Profilers treat
  335. profiling information in specific ways.
  336. </para>
  337. <xi:include href="Zend_Db_Profiler-Firebug.xml" />
  338. </sect2>
  339. </sect1>
  340. <!--
  341. vim:se ts=4 sw=4 et:
  342. -->