2 $Header: /cvsroot/pgsql/doc/src/sgml/geqo.sgml,v 1.5 1998/12/29 02:24:15 thomas Exp $
6 Revision 1.5 1998/12/29 02:24:15 thomas
7 Clean up to ensure tag completion as required by the newest versions
8 of Norm's Modular Style Sheets and jade/docbook.
9 From Vince Vielhaber <vev@michvhf.com>.
11 Revision 1.4 1998/08/15 06:55:05 thomas
12 Change Id field in chapter tag to change html output file name.
19 <FirstName>Martin</FirstName>
20 <SurName>Utesch</SurName>
23 University of Mining and Technology
26 Institute of Automatic Control
38 <Date>1997-10-02</Date>
41 <Title>Genetic Query Optimization in Database Systems</Title>
47 Written by <ULink url="utesch@aut.tu-freiberg.de">Martin Utesch</ULink>
48 for the Institute of Automatic Control at the University of Mining and Technology in Freiberg, Germany.
54 <Title>Query Handling as a Complex Optimization Problem</Title>
57 Among all relational operators the most difficult one to process and
58 optimize is the <FirstTerm>join</FirstTerm>. The number of alternative plans to answer a query
59 grows exponentially with the number of <Command>join</Command>s included in it. Further
60 optimization effort is caused by the support of a variety of <FirstTerm>join methods</FirstTerm>
61 (e.g., nested loop, index scan, merge join in <ProductName>Postgres</ProductName>) to
62 process individual <Command>join</Command>s and a diversity of <FirstTerm>indices</FirstTerm> (e.g., r-tree,
63 b-tree, hash in <ProductName>Postgres</ProductName>) as access paths for relations.
67 The current <ProductName>Postgres</ProductName> optimizer implementation performs a <FirstTerm>near-
68 exhaustive search</FirstTerm> over the space of alternative strategies. This query
69 optimization technique is inadequate to support database application
70 domains that involve the need for extensive queries, such as artificial
75 The Institute of Automatic Control at the University of Mining and
76 Technology, in Freiberg, Germany, encountered the described problems as its
77 folks wanted to take the <ProductName>Postgres</ProductName> DBMS as the backend for a decision
78 support knowledge based system for the maintenance of an electrical
79 power grid. The DBMS needed to handle large <Command>join</Command> queries for the
80 inference machine of the knowledge based system.
84 Performance difficulties within exploring the space of possible query
85 plans arose the demand for a new optimization technique being developed.
89 In the following we propose the implementation of a <FirstTerm>Genetic Algorithm</FirstTerm>
90 as an option for the database query optimization problem.
95 <Title>Genetic Algorithms (<Acronym>GA</Acronym>)</Title>
98 The <Acronym>GA</Acronym> is a heuristic optimization method which operates through
99 determined, randomized search. The set of possible solutions for the
100 optimization problem is considered as a <FirstTerm>population</FirstTerm> of <FirstTerm>individuals</FirstTerm>.
101 The degree of adaption of an individual to its environment is specified
102 by its <FirstTerm>fitness</FirstTerm>.
106 The coordinates of an individual in the search space are represented
107 by <FirstTerm>chromosomes</FirstTerm>, in essence a set of character strings. A <FirstTerm>gene</FirstTerm> is a
108 subsection of a chromosome which encodes the value of a single parameter
109 being optimized. Typical encodings for a gene could be <FirstTerm>binary</FirstTerm> or
110 <FirstTerm>integer</FirstTerm>.
114 Through simulation of the evolutionary operations <FirstTerm>recombination</FirstTerm>,
115 <FirstTerm>mutation</FirstTerm>, and <FirstTerm>selection</FirstTerm> new generations of search points are found
116 that show a higher average fitness than their ancestors.
120 According to the "comp.ai.genetic" <Acronym>FAQ</Acronym> it cannot be stressed too
121 strongly that a <Acronym>GA</Acronym> is not a pure random search for a solution to a
122 problem. A <Acronym>GA</Acronym> uses stochastic processes, but the result is distinctly
123 non-random (better than random).
126 Structured Diagram of a <Acronym>GA</Acronym>:
127 ---------------------------
129 P(t) generation of ancestors at a time t
130 P''(t) generation of descendants at a time t
132 +=========================================+
133 |>>>>>>>>>>> Algorithm GA <<<<<<<<<<<<<<|
134 +=========================================+
135 | INITIALIZE t := 0 |
136 +=========================================+
138 +=========================================+
139 | evalute FITNESS of P(t) |
140 +=========================================+
141 | while not STOPPING CRITERION do |
142 | +-------------------------------------+
143 | | P'(t) := RECOMBINATION{P(t)} |
144 | +-------------------------------------+
145 | | P''(t) := MUTATION{P'(t)} |
146 | +-------------------------------------+
147 | | P(t+1) := SELECTION{P''(t) + P(t)} |
148 | +-------------------------------------+
149 | | evalute FITNESS of P''(t) |
150 | +-------------------------------------+
152 +===+=====================================+
158 <Title>Genetic Query Optimization (<Acronym>GEQO</Acronym>) in Postgres</Title>
161 The <Acronym>GEQO</Acronym> module is intended for the solution of the query
162 optimization problem similar to a traveling salesman problem (<Acronym>TSP</Acronym>).
163 Possible query plans are encoded as integer strings. Each string
164 represents the <Command>join</Command> order from one relation of the query to the next.
165 E. g., the query tree
172 is encoded by the integer string '4-1-3-2',
173 which means, first join relation '4' and '1', then '3', and
174 then '2', where 1, 2, 3, 4 are relids in <ProductName>Postgres</ProductName>.
178 Parts of the <Acronym>GEQO</Acronym> module are adapted from D. Whitley's Genitor
183 Specific characteristics of the <Acronym>GEQO</Acronym> implementation in <ProductName>Postgres</ProductName>
186 <ItemizedList Mark="bullet" Spacing="compact">
189 Usage of a <FirstTerm>steady state</FirstTerm> <Acronym>GA</Acronym> (replacement of the least fit
190 individuals in a population, not whole-generational replacement)
191 allows fast convergence towards improved query plans. This is
192 essential for query handling with reasonable time;
198 Usage of <FirstTerm>edge recombination crossover</FirstTerm> which is especially suited
199 to keep edge losses low for the solution of the <Acronym>TSP</Acronym> by means of a <Acronym>GA</Acronym>;
205 Mutation as genetic operator is deprecated so that no repair
206 mechanisms are needed to generate legal <Acronym>TSP</Acronym> tours.
213 The <Acronym>GEQO</Acronym> module gives the following benefits to the <ProductName>Postgres</ProductName> DBMS
214 compared to the <ProductName>Postgres</ProductName> query optimizer implementation:
216 <ItemizedList Mark="bullet" Spacing="compact">
219 Handling of large <Command>join</Command> queries through non-exhaustive search;
225 Improved cost size approximation of query plans since no longer
226 plan merging is needed (the <Acronym>GEQO</Acronym> module evaluates the cost for a
227 query plan as an individual).
236 <Title>Future Implementation Tasks for <ProductName>Postgres</ProductName> <Acronym>GEQO</Acronym></Title>
239 <Title>Basic Improvements</Title>
242 <Title>Improve freeing of memory when query is already processed</Title>
245 With large <Command>join</Command> queries the computing time spent for the genetic query
246 optimization seems to be a mere <Emphasis>fraction</Emphasis> of the time
247 <ProductName>Postgres</ProductName>
248 needs for freeing memory via routine <Function>MemoryContextFree</Function>,
249 file <FileName>backend/utils/mmgr/mcxt.c</FileName>.
250 Debugging showed that it get stucked in a loop of routine
251 <Function>OrderedElemPop</Function>, file <FileName>backend/utils/mmgr/oset.c</FileName>.
252 The same problems arise with long queries when using the normal
253 <ProductName>Postgres</ProductName> query optimization algorithm.
258 <Title>Improve genetic algorithm parameter settings</Title>
261 In file <FileName>backend/optimizer/geqo/geqo_params.c</FileName>, routines
262 <Function>gimme_pool_size</Function> and <Function>gimme_number_generations</Function>,
263 we have to find a compromise for the parameter settings
264 to satisfy two competing demands:
265 <ItemizedList Spacing="compact">
268 Optimality of the query plan
281 <Title>Find better solution for integer overflow</Title>
284 In file <FileName>backend/optimizer/geqo/geqo_eval.c</FileName>, routine
285 <Function>geqo_joinrel_size</Function>,
286 the present hack for MAXINT overflow is to set the <ProductName>Postgres</ProductName> integer
287 value of <StructField>rel->size</StructField> to its logarithm.
288 Modifications of <StructName>Rel</StructName> in <FileName>backend/nodes/relation.h</FileName> will
289 surely have severe impacts on the whole <ProductName>Postgres</ProductName> implementation.
294 <Title>Find solution for exhausted memory</Title>
297 Memory exhaustion may occur with more than 10 relations involved in a query.
298 In file <FileName>backend/optimizer/geqo/geqo_eval.c</FileName>, routine
299 <Function>gimme_tree</Function> is recursively called.
300 Maybe I forgot something to be freed correctly, but I dunno what.
301 Of course the <StructName>rel</StructName> data structure of the <Command>join</Command> keeps growing and
302 growing the more relations are packed into it.
303 Suggestions are welcome :-(
309 <Title>Further Improvements</Title>
312 Enable bushy query tree processing within <ProductName>Postgres</ProductName>;
313 that may improve the quality of query plans.
316 <BIBLIOGRAPHY Id="geqo-biblio">
320 <PARA>Reference information for <Acronym>GEQ</Acronym> algorithms.
326 The Hitch-Hiker's Guide to Evolutionary Computation
330 <FIRSTNAME>Jörg</FIRSTNAME>
331 <SURNAME>Heitkötter</SURNAME>
334 <FIRSTNAME>David</FIRSTNAME>
335 <SURNAME>Beasley</SURNAME>
345 FAQ in <ULink url="news://comp.ai.genetic">comp.ai.genetic</ULink>
346 is available at <ULink url="ftp://ftp.Germany.EU.net/pub/research/softcomp/EC/Welcome.html">Encore</ULink>.
353 The Design and Implementation of the Postgres Query Optimizer
357 <FIRSTNAME>Z.</FIRSTNAME>
358 <SURNAME>Fong</SURNAME>
363 University of California, Berkeley Computer Science Department
368 File <FileName>planner/Report.ps</FileName> in the 'postgres-papers' distribution.
375 Fundamentals of Database Systems
379 <FIRSTNAME>R.</FIRSTNAME>
380 <SURNAME>Elmasri</SURNAME>
383 <FIRSTNAME>S.</FIRSTNAME>
384 <SURNAME>Navathe</SURNAME>
389 The Benjamin/Cummings Pub., Inc.