Zend_Db_Profiler.xml 16 KB

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