gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

142 lines
10KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>61.6. Index Cost Estimation Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="index-unique-checks.html" title="61.5. Index Uniqueness Checks" /><link rel="next" href="generic-wal.html" title="Chapter 62. Generic WAL Records" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">61.6. Index Cost Estimation Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="index-unique-checks.html" title="61.5. Index Uniqueness Checks">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexam.html" title="Chapter 61. Index Access Method Interface Definition">Up</a></td><th width="60%" align="center">Chapter 61. Index Access Method Interface Definition</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="generic-wal.html" title="Chapter 62. Generic WAL Records">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEX-COST-ESTIMATION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">61.6. Index Cost Estimation Functions</h2></div></div></div><p>
  3. The <code class="function">amcostestimate</code> function is given information describing
  4. a possible index scan, including lists of WHERE and ORDER BY clauses that
  5. have been determined to be usable with the index. It must return estimates
  6. of the cost of accessing the index and the selectivity of the WHERE
  7. clauses (that is, the fraction of parent-table rows that will be
  8. retrieved during the index scan). For simple cases, nearly all the
  9. work of the cost estimator can be done by calling standard routines
  10. in the optimizer; the point of having an <code class="function">amcostestimate</code> function is
  11. to allow index access methods to provide index-type-specific knowledge,
  12. in case it is possible to improve on the standard estimates.
  13. </p><p>
  14. Each <code class="function">amcostestimate</code> function must have the signature:
  15. </p><pre class="programlisting">
  16. void
  17. amcostestimate (PlannerInfo *root,
  18. IndexPath *path,
  19. double loop_count,
  20. Cost *indexStartupCost,
  21. Cost *indexTotalCost,
  22. Selectivity *indexSelectivity,
  23. double *indexCorrelation,
  24. double *indexPages);
  25. </pre><p>
  26. The first three parameters are inputs:
  27. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="parameter"><code>root</code></em></span></dt><dd><p>
  28. The planner's information about the query being processed.
  29. </p></dd><dt><span class="term"><em class="parameter"><code>path</code></em></span></dt><dd><p>
  30. The index access path being considered. All fields except cost and
  31. selectivity values are valid.
  32. </p></dd><dt><span class="term"><em class="parameter"><code>loop_count</code></em></span></dt><dd><p>
  33. The number of repetitions of the index scan that should be factored
  34. into the cost estimates. This will typically be greater than one when
  35. considering a parameterized scan for use in the inside of a nestloop
  36. join. Note that the cost estimates should still be for just one scan;
  37. a larger <em class="parameter"><code>loop_count</code></em> means that it may be appropriate
  38. to allow for some caching effects across multiple scans.
  39. </p></dd></dl></div><p>
  40. </p><p>
  41. The last five parameters are pass-by-reference outputs:
  42. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="parameter"><code>*indexStartupCost</code></em></span></dt><dd><p>
  43. Set to cost of index start-up processing
  44. </p></dd><dt><span class="term"><em class="parameter"><code>*indexTotalCost</code></em></span></dt><dd><p>
  45. Set to total cost of index processing
  46. </p></dd><dt><span class="term"><em class="parameter"><code>*indexSelectivity</code></em></span></dt><dd><p>
  47. Set to index selectivity
  48. </p></dd><dt><span class="term"><em class="parameter"><code>*indexCorrelation</code></em></span></dt><dd><p>
  49. Set to correlation coefficient between index scan order and
  50. underlying table's order
  51. </p></dd><dt><span class="term"><em class="parameter"><code>*indexPages</code></em></span></dt><dd><p>
  52. Set to number of index leaf pages
  53. </p></dd></dl></div><p>
  54. </p><p>
  55. Note that cost estimate functions must be written in C, not in SQL or
  56. any available procedural language, because they must access internal
  57. data structures of the planner/optimizer.
  58. </p><p>
  59. The index access costs should be computed using the parameters used by
  60. <code class="filename">src/backend/optimizer/path/costsize.c</code>: a sequential
  61. disk block fetch has cost <code class="varname">seq_page_cost</code>, a nonsequential fetch
  62. has cost <code class="varname">random_page_cost</code>, and the cost of processing one index
  63. row should usually be taken as <code class="varname">cpu_index_tuple_cost</code>. In
  64. addition, an appropriate multiple of <code class="varname">cpu_operator_cost</code> should
  65. be charged for any comparison operators invoked during index processing
  66. (especially evaluation of the indexquals themselves).
  67. </p><p>
  68. The access costs should include all disk and CPU costs associated with
  69. scanning the index itself, but <span class="emphasis"><em>not</em></span> the costs of retrieving or
  70. processing the parent-table rows that are identified by the index.
  71. </p><p>
  72. The <span class="quote">“<span class="quote">start-up cost</span>”</span> is the part of the total scan cost that
  73. must be expended before we can begin to fetch the first row. For most
  74. indexes this can be taken as zero, but an index type with a high start-up
  75. cost might want to set it nonzero.
  76. </p><p>
  77. The <em class="parameter"><code>indexSelectivity</code></em> should be set to the estimated fraction of the parent
  78. table rows that will be retrieved during the index scan. In the case
  79. of a lossy query, this will typically be higher than the fraction of
  80. rows that actually pass the given qual conditions.
  81. </p><p>
  82. The <em class="parameter"><code>indexCorrelation</code></em> should be set to the correlation (ranging between
  83. -1.0 and 1.0) between the index order and the table order. This is used
  84. to adjust the estimate for the cost of fetching rows from the parent
  85. table.
  86. </p><p>
  87. The <em class="parameter"><code>indexPages</code></em> should be set to the number of leaf pages.
  88. This is used to estimate the number of workers for parallel index scan.
  89. </p><p>
  90. When <em class="parameter"><code>loop_count</code></em> is greater than one, the returned numbers
  91. should be averages expected for any one scan of the index.
  92. </p><div class="procedure" id="id-1.10.14.12.13"><p class="title"><strong>Cost Estimation</strong></p><p>
  93. A typical cost estimator will proceed as follows:
  94. </p><ol class="procedure" type="1"><li class="step"><p>
  95. Estimate and return the fraction of parent-table rows that will be visited
  96. based on the given qual conditions. In the absence of any index-type-specific
  97. knowledge, use the standard optimizer function <code class="function">clauselist_selectivity()</code>:
  98. </p><pre class="programlisting">
  99. *indexSelectivity = clauselist_selectivity(root, path-&gt;indexquals,
  100. path-&gt;indexinfo-&gt;rel-&gt;relid,
  101. JOIN_INNER, NULL);
  102. </pre><p>
  103. </p></li><li class="step"><p>
  104. Estimate the number of index rows that will be visited during the
  105. scan. For many index types this is the same as <em class="parameter"><code>indexSelectivity</code></em> times
  106. the number of rows in the index, but it might be more. (Note that the
  107. index's size in pages and rows is available from the
  108. <code class="literal">path-&gt;indexinfo</code> struct.)
  109. </p></li><li class="step"><p>
  110. Estimate the number of index pages that will be retrieved during the scan.
  111. This might be just <em class="parameter"><code>indexSelectivity</code></em> times the index's size in pages.
  112. </p></li><li class="step"><p>
  113. Compute the index access cost. A generic estimator might do this:
  114. </p><pre class="programlisting">
  115. /*
  116. * Our generic assumption is that the index pages will be read
  117. * sequentially, so they cost seq_page_cost each, not random_page_cost.
  118. * Also, we charge for evaluation of the indexquals at each index row.
  119. * All the costs are assumed to be paid incrementally during the scan.
  120. */
  121. cost_qual_eval(&amp;index_qual_cost, path-&gt;indexquals, root);
  122. *indexStartupCost = index_qual_cost.startup;
  123. *indexTotalCost = seq_page_cost * numIndexPages +
  124. (cpu_index_tuple_cost + index_qual_cost.per_tuple) * numIndexTuples;
  125. </pre><p>
  126. However, the above does not account for amortization of index reads
  127. across repeated index scans.
  128. </p></li><li class="step"><p>
  129. Estimate the index correlation. For a simple ordered index on a single
  130. field, this can be retrieved from pg_statistic. If the correlation
  131. is not known, the conservative estimate is zero (no correlation).
  132. </p></li></ol></div><p>
  133. Examples of cost estimator functions can be found in
  134. <code class="filename">src/backend/utils/adt/selfuncs.c</code>.
  135. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index-unique-checks.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexam.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="generic-wal.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">61.5. Index Uniqueness Checks </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 62. Generic WAL Records</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1