gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

111 行
9.5KB

  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>50.5. Planner/Optimizer</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="rule-system.html" title="50.4. The PostgreSQL Rule System" /><link rel="next" href="executor.html" title="50.6. Executor" /></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">50.5. Planner/Optimizer</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="rule-system.html" title="50.4. The PostgreSQL Rule System">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="overview.html" title="Chapter 50. Overview of PostgreSQL Internals">Up</a></td><th width="60%" align="center">Chapter 50. Overview of PostgreSQL Internals</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="executor.html" title="50.6. Executor">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLANNER-OPTIMIZER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">50.5. Planner/Optimizer</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="planner-optimizer.html#id-1.10.3.8.5">50.5.1. Generating Possible Plans</a></span></dt></dl></div><p>
  3. The task of the <em class="firstterm">planner/optimizer</em> is to
  4. create an optimal execution plan. A given SQL query (and hence, a
  5. query tree) can be actually executed in a wide variety of
  6. different ways, each of which will produce the same set of
  7. results. If it is computationally feasible, the query optimizer
  8. will examine each of these possible execution plans, ultimately
  9. selecting the execution plan that is expected to run the fastest.
  10. </p><div class="note"><h3 class="title">Note</h3><p>
  11. In some situations, examining each possible way in which a query
  12. can be executed would take an excessive amount of time and memory
  13. space. In particular, this occurs when executing queries
  14. involving large numbers of join operations. In order to determine
  15. a reasonable (not necessarily optimal) query plan in a reasonable amount
  16. of time, <span class="productname">PostgreSQL</span> uses a <em class="firstterm">Genetic
  17. Query Optimizer</em> (see <a class="xref" href="geqo.html" title="Chapter 59. Genetic Query Optimizer">Chapter 59</a>) when the number of joins
  18. exceeds a threshold (see <a class="xref" href="runtime-config-query.html#GUC-GEQO-THRESHOLD">geqo_threshold</a>).
  19. </p></div><p>
  20. The planner's search procedure actually works with data structures
  21. called <em class="firstterm">paths</em>, which are simply cut-down representations of
  22. plans containing only as much information as the planner needs to make
  23. its decisions. After the cheapest path is determined, a full-fledged
  24. <em class="firstterm">plan tree</em> is built to pass to the executor. This represents
  25. the desired execution plan in sufficient detail for the executor to run it.
  26. In the rest of this section we'll ignore the distinction between paths
  27. and plans.
  28. </p><div class="sect2" id="id-1.10.3.8.5"><div class="titlepage"><div><div><h3 class="title">50.5.1. Generating Possible Plans</h3></div></div></div><p>
  29. The planner/optimizer starts by generating plans for scanning each
  30. individual relation (table) used in the query. The possible plans
  31. are determined by the available indexes on each relation.
  32. There is always the possibility of performing a
  33. sequential scan on a relation, so a sequential scan plan is always
  34. created. Assume an index is defined on a
  35. relation (for example a B-tree index) and a query contains the
  36. restriction
  37. <code class="literal">relation.attribute OPR constant</code>. If
  38. <code class="literal">relation.attribute</code> happens to match the key of the B-tree
  39. index and <code class="literal">OPR</code> is one of the operators listed in
  40. the index's <em class="firstterm">operator class</em>, another plan is created using
  41. the B-tree index to scan the relation. If there are further indexes
  42. present and the restrictions in the query happen to match a key of an
  43. index, further plans will be considered. Index scan plans are also
  44. generated for indexes that have a sort ordering that can match the
  45. query's <code class="literal">ORDER BY</code> clause (if any), or a sort ordering that
  46. might be useful for merge joining (see below).
  47. </p><p>
  48. If the query requires joining two or more relations,
  49. plans for joining relations are considered
  50. after all feasible plans have been found for scanning single relations.
  51. The three available join strategies are:
  52. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  53. <em class="firstterm">nested loop join</em>: The right relation is scanned
  54. once for every row found in the left relation. This strategy
  55. is easy to implement but can be very time consuming. (However,
  56. if the right relation can be scanned with an index scan, this can
  57. be a good strategy. It is possible to use values from the current
  58. row of the left relation as keys for the index scan of the right.)
  59. </p></li><li class="listitem"><p>
  60. <em class="firstterm">merge join</em>: Each relation is sorted on the join
  61. attributes before the join starts. Then the two relations are
  62. scanned in parallel, and matching rows are combined to form
  63. join rows. This kind of join is more
  64. attractive because each relation has to be scanned only once.
  65. The required sorting might be achieved either by an explicit sort
  66. step, or by scanning the relation in the proper order using an
  67. index on the join key.
  68. </p></li><li class="listitem"><p>
  69. <em class="firstterm">hash join</em>: the right relation is first scanned
  70. and loaded into a hash table, using its join attributes as hash keys.
  71. Next the left relation is scanned and the
  72. appropriate values of every row found are used as hash keys to
  73. locate the matching rows in the table.
  74. </p></li></ul></div><p>
  75. </p><p>
  76. When the query involves more than two relations, the final result
  77. must be built up by a tree of join steps, each with two inputs.
  78. The planner examines different possible join sequences to find the
  79. cheapest one.
  80. </p><p>
  81. If the query uses fewer than <a class="xref" href="runtime-config-query.html#GUC-GEQO-THRESHOLD">geqo_threshold</a>
  82. relations, a near-exhaustive search is conducted to find the best
  83. join sequence. The planner preferentially considers joins between any
  84. two relations for which there exist a corresponding join clause in the
  85. <code class="literal">WHERE</code> qualification (i.e., for
  86. which a restriction like <code class="literal">where rel1.attr1=rel2.attr2</code>
  87. exists). Join pairs with no join clause are considered only when there
  88. is no other choice, that is, a particular relation has no available
  89. join clauses to any other relation. All possible plans are generated for
  90. every join pair considered by the planner, and the one that is
  91. (estimated to be) the cheapest is chosen.
  92. </p><p>
  93. When <code class="varname">geqo_threshold</code> is exceeded, the join
  94. sequences considered are determined by heuristics, as described
  95. in <a class="xref" href="geqo.html" title="Chapter 59. Genetic Query Optimizer">Chapter 59</a>. Otherwise the process is the same.
  96. </p><p>
  97. The finished plan tree consists of sequential or index scans of
  98. the base relations, plus nested-loop, merge, or hash join nodes as
  99. needed, plus any auxiliary steps needed, such as sort nodes or
  100. aggregate-function calculation nodes. Most of these plan node
  101. types have the additional ability to do <em class="firstterm">selection</em>
  102. (discarding rows that do not meet a specified Boolean condition)
  103. and <em class="firstterm">projection</em> (computation of a derived column set
  104. based on given column values, that is, evaluation of scalar
  105. expressions where needed). One of the responsibilities of the
  106. planner is to attach selection conditions from the
  107. <code class="literal">WHERE</code> clause and computation of required
  108. output expressions to the most appropriate nodes of the plan
  109. tree.
  110. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="rule-system.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="overview.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="executor.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">50.4. The <span class="productname">PostgreSQL</span> Rule System </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 50.6. Executor</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1