2
0

performance-database.xml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="performance.database">
  4. <title>Zend_Db Performance</title>
  5. <para>
  6. <classname>Zend_Db</classname> is a database abstraction layer, and is intended to
  7. provide a common API for SQL operations. <classname>Zend_Db_Table</classname> is a
  8. Table Data Gateway, intended to abstract common table-level database
  9. operations. Due to their abstract nature and the "magic" they do under
  10. the hood to perform their operations, they can sometimes introduce
  11. performance overhead.
  12. </para>
  13. <sect2 id="performance.database.tableMetadata">
  14. <title>How can I reduce overhead introduced by Zend_Db_Table for
  15. retrieving table metadata?</title>
  16. <para>
  17. In order to keep usage as simple as possible, and also to support
  18. constantly changing schemas during development,
  19. <classname>Zend_Db_Table</classname> does some magic under the hood: on
  20. first use, it fetches the table schema and stores it within object
  21. members. This operation is typically expensive, regardless of the
  22. database -- which can contribute to bottlenecks in production.
  23. </para>
  24. <para>
  25. Fortunately, there are techniques for improving the situation.
  26. </para>
  27. <sect3 id="performance.database.tableMetadata.cache">
  28. <title>Use the metadata cache</title>
  29. <para>
  30. <classname>Zend_Db_Table</classname> can optionally utilize
  31. <classname>Zend_Cache</classname> to cache table metadata. This is
  32. typically faster to access and less expensive than fetching the
  33. metadata from the database itself.
  34. </para>
  35. <para>
  36. The <link linkend="zend.db.table.metadata.caching"><classname>Zend_Db_Table
  37. </classname> documentation includes information on metadata caching</link>.
  38. </para>
  39. </sect3>
  40. <sect3 id="performance.database.tableMetadata.hardcoding">
  41. <title>Hardcode your metadata in the table definition</title>
  42. <para>
  43. As of 1.7.0, <classname>Zend_Db_Table</classname> also provides <link
  44. linkend="zend.db.table.metadata.caching.hardcoding">support
  45. for hardcoding metadata in the table definition</link>. This is
  46. an advanced use case, and should only be used when you know the
  47. table schema is unlikely to change, or that you're able to keep
  48. the definitions up-to-date.
  49. </para>
  50. </sect3>
  51. </sect2>
  52. <sect2 id="performance.database.select">
  53. <title>SQL generated with Zend_Db_Select s not hitting my indexes; how
  54. can I make it better?</title>
  55. <para>
  56. <classname>Zend_Db_Select</classname> is relatively good at its job. However,
  57. if you are performing complex queries requiring joins or
  58. sub-selects, it can often be fairly naive.
  59. </para>
  60. <sect3 id="performance.database.select.writeyourown">
  61. <title>Write your own tuned SQL</title>
  62. <para>
  63. The only real answer is to write your own SQL;
  64. <classname>Zend_Db</classname> does not require the usage of
  65. <classname>Zend_Db_Select</classname>, so providing your own, tuned SQL
  66. select statements is a perfectly legitimate approach,
  67. </para>
  68. <para>
  69. Run <constant>EXPLAIN</constant> on your queries, and test a variety of
  70. approaches until you can reliably hit your indices in the most
  71. performant way -- and then hardcode the SQL as a class property
  72. or constant.
  73. </para>
  74. <para>
  75. If the SQL requires variable arguments, provide placeholders in
  76. the SQL, and utilize a combination of <methodname>vsprintf()</methodname> and
  77. <methodname>array_walk()</methodname> to inject the values into the SQL:
  78. </para>
  79. <programlisting language="php"><![CDATA[
  80. // $adapter is the DB adapter. In Zend_Db_Table, retrieve
  81. // it using $this->getAdapter().
  82. $sql = vsprintf(
  83. self::SELECT_FOO,
  84. array_walk($values, array($adapter, 'quoteInto'))
  85. );
  86. ]]></programlisting>
  87. </sect3>
  88. </sect2>
  89. </sect1>
  90. <!--
  91. vim:se ts=4 sw=4 et:
  92. -->