-<!--
-$PostgreSQL: pgsql/doc/src/sgml/geqo.sgml,v 1.33 2005/10/25 13:38:09 momjian Exp $
-Genetic Optimizer
--->
+<!-- doc/src/sgml/geqo.sgml -->
<chapter id="geqo">
- <chapterinfo>
- <author>
- <firstname>Martin</firstname>
- <surname>Utesch</surname>
- <affiliation>
- <orgname>
- University of Mining and Technology
- </orgname>
- <orgdiv>
- Institute of Automatic Control
- </orgdiv>
- <address>
- <city>
- Freiberg
- </city>
- <country>
- Germany
- </country>
- </address>
- </affiliation>
- </author>
- <date>1997-10-02</date>
- </chapterinfo>
-
- <title id="geqo-title">Genetic Query Optimizer</title>
+ <title>Genetic Query Optimizer</title>
<para>
<note>
<para>
Among all relational operators the most difficult one to process
and optimize is the <firstterm>join</firstterm>. The number of
- alternative plans to answer a query grows exponentially with the
- number of joins included in it. Further optimization effort is
+ possible query plans grows exponentially with the
+ number of joins in the query. Further optimization effort is
caused by the support of a variety of <firstterm>join
methods</firstterm> (e.g., nested loop, hash join, merge join in
<productname>PostgreSQL</productname>) to process individual joins
- and a diversity of <firstterm>indexes</firstterm> (e.g., R-tree,
- B-tree, hash in <productname>PostgreSQL</productname>) as access
- paths for relations.
+ and a diversity of <firstterm>indexes</firstterm> (e.g.,
+ B-tree, hash, GiST and GIN in <productname>PostgreSQL</productname>) as
+ access paths for relations.
</para>
<para>
- The current <productname>PostgreSQL</productname> optimizer
- implementation performs a <firstterm>near-exhaustive
- search</firstterm> over the space of alternative strategies. This
- algorithm, first introduced in the <quote>System R</quote>
- database, produces a near-optimal join order, but can take an
- enormous amount of time and memory space when the number of joins
- in the query grows large. This makes the ordinary
+ The normal <productname>PostgreSQL</productname> query optimizer
+ performs a <firstterm>near-exhaustive search</firstterm> over the
+ space of alternative strategies. This algorithm, first introduced
+ in IBM's System R database, produces a near-optimal join order,
+ but can take an enormous amount of time and memory space when the
+ number of joins in the query grows large. This makes the ordinary
<productname>PostgreSQL</productname> query optimizer
inappropriate for queries that join a large number of tables.
</para>
<para>
The Institute of Automatic Control at the University of Mining and
- Technology, in Freiberg, Germany, encountered the described problems as its
- folks wanted to take the <productname>PostgreSQL</productname> DBMS as the backend for a decision
- support knowledge based system for the maintenance of an electrical
- power grid. The DBMS needed to handle large join queries for the
- inference machine of the knowledge based system.
- </para>
-
- <para>
- Performance difficulties in exploring the space of possible query
- plans created the demand for a new optimization technique to be developed.
+ Technology, in Freiberg, Germany, encountered some problems when
+ it wanted to use <productname>PostgreSQL</productname> as the
+ backend for a decision support knowledge based system for the
+ maintenance of an electrical power grid. The DBMS needed to handle
+ large join queries for the inference machine of the knowledge
+ based system. The number of joins in these queries made using the
+ normal query optimizer infeasible.
</para>
<para>
In the following we describe the implementation of a
- <firstterm>Genetic Algorithm</firstterm> to solve the join
+ <firstterm>genetic algorithm</firstterm> to solve the join
ordering problem in a manner that is efficient for queries
involving large numbers of joins.
</para>
<para>
The genetic algorithm (<acronym>GA</acronym>) is a heuristic optimization method which
- operates through
- nondeterministic, randomized search. The set of possible solutions for the
+ operates through randomized search. The set of possible solutions for the
optimization problem is considered as a
<firstterm>population</firstterm> of <firstterm>individuals</firstterm>.
The degree of adaptation of an individual to its environment is specified
According to the <systemitem class="resource">comp.ai.genetic</> <acronym>FAQ</acronym> it cannot be stressed too
strongly that a <acronym>GA</acronym> is not a pure random search for a solution to a
problem. A <acronym>GA</acronym> uses stochastic processes, but the result is distinctly
- non-random (better than random).
+ non-random (better than random).
</para>
<figure id="geqo-diagram">
<productname>PostgreSQL</productname> optimizer.
</para>
- <para>
- Parts of the <acronym>GEQO</acronym> module are adapted from D. Whitley's Genitor
- algorithm.
- </para>
-
<para>
Specific characteristics of the <acronym>GEQO</acronym>
implementation in <productname>PostgreSQL</productname>
</itemizedlist>
</para>
+ <para>
+ Parts of the <acronym>GEQO</acronym> module are adapted from D. Whitley's
+ Genitor algorithm.
+ </para>
+
<para>
The <acronym>GEQO</acronym> module allows
the <productname>PostgreSQL</productname> query optimizer to
non-exhaustive search.
</para>
+ <sect2>
+ <title>Generating Possible Plans with <acronym>GEQO</acronym></title>
+
+ <para>
+ The <acronym>GEQO</acronym> planning process uses the standard planner
+ code to generate plans for scans of individual relations. Then join
+ plans are developed using the genetic approach. As shown above, each
+ candidate join plan is represented by a sequence in which to join
+ the base relations. In the initial stage, the <acronym>GEQO</acronym>
+ code simply generates some possible join sequences at random. For each
+ join sequence considered, the standard planner code is invoked to
+ estimate the cost of performing the query using that join sequence.
+ (For each step of the join sequence, all three possible join strategies
+ are considered; and all the initially-determined relation scan plans
+ are available. The estimated cost is the cheapest of these
+ possibilities.) Join sequences with lower estimated cost are considered
+ <quote>more fit</> than those with higher cost. The genetic algorithm
+ discards the least fit candidates. Then new candidates are generated
+ by combining genes of more-fit candidates — that is, by using
+ randomly-chosen portions of known low-cost join sequences to create
+ new sequences for consideration. This process is repeated until a
+ preset number of join sequences have been considered; then the best
+ one found at any time during the search is used to generate the finished
+ plan.
+ </para>
+
+ <para>
+ This process is inherently nondeterministic, because of the randomized
+ choices made during both the initial population selection and subsequent
+ <quote>mutation</> of the best candidates. To avoid surprising changes
+ of the selected plan, each run of the GEQO algorithm restarts its
+ random number generator with the current <xref linkend="guc-geqo-seed">
+ parameter setting. As long as <varname>geqo_seed</> and the other
+ GEQO parameters are kept fixed, the same plan will be generated for a
+ given query (and other planner inputs such as statistics). To experiment
+ with different search paths, try changing <varname>geqo_seed</>.
+ </para>
+
+ </sect2>
+
<sect2 id="geqo-future">
<title>Future Implementation Tasks for
<productname>PostgreSQL</> <acronym>GEQO</acronym></title>
</itemizedlist>
</para>
+ <para>
+ In the current implementation, the fitness of each candidate join
+ sequence is estimated by running the standard planner's join selection
+ and cost estimation code from scratch. To the extent that different
+ candidates use similar sub-sequences of joins, a great deal of work
+ will be repeated. This could be made significantly faster by retaining
+ cost estimates for sub-joins. The problem is to avoid expending
+ unreasonable amounts of memory on retaining that state.
+ </para>
+
<para>
At a more basic level, it is not clear that solving query optimization
with a GA algorithm designed for TSP is appropriate. In the TSP case,
<itemizedlist>
<listitem>
<para>
- <ulink url="http://www.cs.bham.ac.uk/Mirrors/ftp.de.uu.net/EC/clife/www/location.htm">
+ <ulink url="http://www.aip.de/~ast/EvolCompFAQ/">
The Hitch-Hiker's Guide to Evolutionary Computation</ulink>, (FAQ for <ulink
url="news://comp.ai.genetic"></ulink>)
</para>
</listitem>
-
+
<listitem>
<para>
<ulink url="http://www.red3d.com/cwr/evolve.html">
</sect1>
</chapter>
-
-<!-- Keep this comment at the end of the file
-Local variables:
-mode:sgml
-sgml-omittag:nil
-sgml-shorttag:t
-sgml-minimize-attributes:nil
-sgml-always-quote-attributes:t
-sgml-indent-step:1
-sgml-indent-data:t
-sgml-parent-document:nil
-sgml-default-dtd-file:"./reference.ced"
-sgml-exposed-tags:nil
-sgml-local-catalogs:("/usr/lib/sgml/catalog")
-sgml-local-ecat-files:nil
-End:
--->