gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
9.2KB

  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>59.3. Genetic Query Optimization (GEQO) in PostgreSQL</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="geqo-intro2.html" title="59.2. Genetic Algorithms" /><link rel="next" href="geqo-biblio.html" title="59.4. Further Reading" /></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">59.3. Genetic Query Optimization (<acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">GEQO</acronym>) in PostgreSQL</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="geqo-intro2.html" title="59.2. Genetic Algorithms">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="geqo.html" title="Chapter 59. Genetic Query Optimizer">Up</a></td><th width="60%" align="center">Chapter 59. Genetic Query Optimizer</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="geqo-biblio.html" title="59.4. Further Reading">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="GEQO-PG-INTRO"><div class="titlepage"><div><div><h2 class="title" style="clear: both">59.3. Genetic Query Optimization (<acronym class="acronym">GEQO</acronym>) in PostgreSQL</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="geqo-pg-intro.html#id-1.10.12.5.6">59.3.1. Generating Possible Plans with <acronym class="acronym">GEQO</acronym></a></span></dt><dt><span class="sect2"><a href="geqo-pg-intro.html#GEQO-FUTURE">59.3.2. Future Implementation Tasks for
  3. <span class="productname">PostgreSQL</span> <acronym class="acronym">GEQO</acronym></a></span></dt></dl></div><p>
  4. The <acronym class="acronym">GEQO</acronym> module approaches the query
  5. optimization problem as though it were the well-known traveling salesman
  6. problem (<acronym class="acronym">TSP</acronym>).
  7. Possible query plans are encoded as integer strings. Each string
  8. represents the join order from one relation of the query to the next.
  9. For example, the join tree
  10. </p><pre class="literallayout">
  11. /\
  12. /\ 2
  13. /\ 3
  14. 4 1
  15. </pre><p>
  16. is encoded by the integer string '4-1-3-2',
  17. which means, first join relation '4' and '1', then '3', and
  18. then '2', where 1, 2, 3, 4 are relation IDs within the
  19. <span class="productname">PostgreSQL</span> optimizer.
  20. </p><p>
  21. Specific characteristics of the <acronym class="acronym">GEQO</acronym>
  22. implementation in <span class="productname">PostgreSQL</span>
  23. are:
  24. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: bullet; "><li class="listitem" style="list-style-type: disc"><p>
  25. Usage of a <em class="firstterm">steady state</em> <acronym class="acronym">GA</acronym> (replacement of the least fit
  26. individuals in a population, not whole-generational replacement)
  27. allows fast convergence towards improved query plans. This is
  28. essential for query handling with reasonable time;
  29. </p></li><li class="listitem" style="list-style-type: disc"><p>
  30. Usage of <em class="firstterm">edge recombination crossover</em>
  31. which is especially suited to keep edge losses low for the
  32. solution of the <acronym class="acronym">TSP</acronym> by means of a
  33. <acronym class="acronym">GA</acronym>;
  34. </p></li><li class="listitem" style="list-style-type: disc"><p>
  35. Mutation as genetic operator is deprecated so that no repair
  36. mechanisms are needed to generate legal <acronym class="acronym">TSP</acronym> tours.
  37. </p></li></ul></div><p>
  38. </p><p>
  39. Parts of the <acronym class="acronym">GEQO</acronym> module are adapted from D. Whitley's
  40. Genitor algorithm.
  41. </p><p>
  42. The <acronym class="acronym">GEQO</acronym> module allows
  43. the <span class="productname">PostgreSQL</span> query optimizer to
  44. support large join queries effectively through
  45. non-exhaustive search.
  46. </p><div class="sect2" id="id-1.10.12.5.6"><div class="titlepage"><div><div><h3 class="title">59.3.1. Generating Possible Plans with <acronym class="acronym">GEQO</acronym></h3></div></div></div><p>
  47. The <acronym class="acronym">GEQO</acronym> planning process uses the standard planner
  48. code to generate plans for scans of individual relations. Then join
  49. plans are developed using the genetic approach. As shown above, each
  50. candidate join plan is represented by a sequence in which to join
  51. the base relations. In the initial stage, the <acronym class="acronym">GEQO</acronym>
  52. code simply generates some possible join sequences at random. For each
  53. join sequence considered, the standard planner code is invoked to
  54. estimate the cost of performing the query using that join sequence.
  55. (For each step of the join sequence, all three possible join strategies
  56. are considered; and all the initially-determined relation scan plans
  57. are available. The estimated cost is the cheapest of these
  58. possibilities.) Join sequences with lower estimated cost are considered
  59. <span class="quote">“<span class="quote">more fit</span>”</span> than those with higher cost. The genetic algorithm
  60. discards the least fit candidates. Then new candidates are generated
  61. by combining genes of more-fit candidates — that is, by using
  62. randomly-chosen portions of known low-cost join sequences to create
  63. new sequences for consideration. This process is repeated until a
  64. preset number of join sequences have been considered; then the best
  65. one found at any time during the search is used to generate the finished
  66. plan.
  67. </p><p>
  68. This process is inherently nondeterministic, because of the randomized
  69. choices made during both the initial population selection and subsequent
  70. <span class="quote">“<span class="quote">mutation</span>”</span> of the best candidates. To avoid surprising changes
  71. of the selected plan, each run of the GEQO algorithm restarts its
  72. random number generator with the current <a class="xref" href="runtime-config-query.html#GUC-GEQO-SEED">geqo_seed</a>
  73. parameter setting. As long as <code class="varname">geqo_seed</code> and the other
  74. GEQO parameters are kept fixed, the same plan will be generated for a
  75. given query (and other planner inputs such as statistics). To experiment
  76. with different search paths, try changing <code class="varname">geqo_seed</code>.
  77. </p></div><div class="sect2" id="GEQO-FUTURE"><div class="titlepage"><div><div><h3 class="title">59.3.2. Future Implementation Tasks for
  78. <span class="productname">PostgreSQL</span> <acronym class="acronym">GEQO</acronym></h3></div></div></div><p>
  79. Work is still needed to improve the genetic algorithm parameter
  80. settings.
  81. In file <code class="filename">src/backend/optimizer/geqo/geqo_main.c</code>,
  82. routines
  83. <code class="function">gimme_pool_size</code> and <code class="function">gimme_number_generations</code>,
  84. we have to find a compromise for the parameter settings
  85. to satisfy two competing demands:
  86. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>
  87. Optimality of the query plan
  88. </p></li><li class="listitem"><p>
  89. Computing time
  90. </p></li></ul></div><p>
  91. </p><p>
  92. In the current implementation, the fitness of each candidate join
  93. sequence is estimated by running the standard planner's join selection
  94. and cost estimation code from scratch. To the extent that different
  95. candidates use similar sub-sequences of joins, a great deal of work
  96. will be repeated. This could be made significantly faster by retaining
  97. cost estimates for sub-joins. The problem is to avoid expending
  98. unreasonable amounts of memory on retaining that state.
  99. </p><p>
  100. At a more basic level, it is not clear that solving query optimization
  101. with a GA algorithm designed for TSP is appropriate. In the TSP case,
  102. the cost associated with any substring (partial tour) is independent
  103. of the rest of the tour, but this is certainly not true for query
  104. optimization. Thus it is questionable whether edge recombination
  105. crossover is the most effective mutation procedure.
  106. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="geqo-intro2.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="geqo.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="geqo-biblio.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">59.2. Genetic Algorithms </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 59.4. Further Reading</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1