<!-- doc/src/sgml/geqo.sgml -->
<chapter id="geqo">
<title>Genetic Query Optimizer</title>
<para>
<note>
<title>Author</title>
<para>
Written by Martin Utesch (<email>utesch@aut.tu-freiberg.de</email>)
for the Institute of Automatic Control at the University of Mining and Technology in Freiberg, Germany.
</para>
</note>
</para>
<sect1 id="geqo-intro">
<title>Query Handling as a Complex Optimization Problem</title>
<para>
Among all relational operators the most difficult one to process
and optimize is the <firstterm>join</firstterm>. The number of
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.,
B-tree, hash, GiST and GIN in <productname>PostgreSQL</productname>) as
access paths for relations.
</para>
<para>
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 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
ordering problem in a manner that is efficient for queries
involving large numbers of joins.
</para>
</sect1>
<sect1 id="geqo-intro2">
<title>Genetic Algorithms</title>
<para>
The genetic algorithm (<acronym>GA</acronym>) is a heuristic optimization method which
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
by its <firstterm>fitness</firstterm>.
</para>
<para>
The coordinates of an individual in the search space are represented
by <firstterm>chromosomes</firstterm>, in essence a set of character
strings. A <firstterm>gene</firstterm> is a
subsection of a chromosome which encodes the value of a single parameter
being optimized. Typical encodings for a gene could be <firstterm>binary</firstterm> or
<firstterm>integer</firstterm>.
</para>
<para>
Through simulation of the evolutionary operations <firstterm>recombination</firstterm>,
<firstterm>mutation</firstterm>, and
<firstterm>selection</firstterm> new generations of search points are found
that show a higher average fitness than their ancestors. <xref linkend="geqo-figure"/>
illustrates these steps.
</para>
<figure id="geqo-figure">
<title>Structure of a Genetic Algorithm</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/genetic-algorithm.svg" format="SVG" width="100%"/>
</imageobject>
</mediaobject>
</figure>
<para>