4 <FirstName>Martin</FirstName>
5 <SurName>Utesch</SurName>
8 University of Mining and Technology
11 Institute of Automatic Control
23 <Date>1997-10-02</Date>
26 <Title>Genetic Query Optimization in Database Systems</Title>
32 Written by <ULink url="utesch@aut.tu-freiberg.de">Martin Utesch</ULink>
33 for the Institute of Automatic Control at the University of Mining and Technology in Freiberg, Germany.
38 <Title>Query Handling as a Complex Optimization Problem</Title>
41 Among all relational operators the most difficult one to process and
42 optimize is the <FirstTerm>join</FirstTerm>. The number of alternative plans to answer a query
43 grows exponentially with the number of <Command>join</Command>s included in it. Further
44 optimization effort is caused by the support of a variety of <FirstTerm>join methods</FirstTerm>
45 (e.g., nested loop, index scan, merge join in <ProductName>Postgres</ProductName>) to
46 process individual <Command>join</Command>s and a diversity of <FirstTerm>indices</FirstTerm> (e.g., r-tree,
47 b-tree, hash in <ProductName>Postgres</ProductName>) as access paths for relations.
50 The current <ProductName>Postgres</ProductName> optimizer implementation performs a <FirstTerm>near-
51 exhaustive search</FirstTerm> over the space of alternative strategies. This query
52 optimization technique is inadequate to support database application
53 domains that involve the need for extensive queries, such as artificial
57 The Institute of Automatic Control at the University of Mining and
58 Technology, in Freiberg, Germany, encountered the described problems as its
59 folks wanted to take the <ProductName>Postgres</ProductName> DBMS as the backend for a decision
60 support knowledge based system for the maintenance of an electrical
61 power grid. The DBMS needed to handle large <Command>join</Command> queries for the
62 inference machine of the knowledge based system.
65 Performance difficulties within exploring the space of possible query
66 plans arose the demand for a new optimization technique being developed.
69 In the following we propose the implementation of a <FirstTerm>Genetic Algorithm</FirstTerm>
70 as an option for the database query optimization problem.
74 <Title>Genetic Algorithms (<Acronym>GA</Acronym>)</Title>
77 The <Acronym>GA</Acronym> is a heuristic optimization method which operates through
78 determined, randomized search. The set of possible solutions for the
79 optimization problem is considered as a <FirstTerm>population</FirstTerm> of <FirstTerm>individuals</FirstTerm>.
80 The degree of adaption of an individual to its environment is specified
81 by its <FirstTerm>fitness</FirstTerm>.
84 The coordinates of an individual in the search space are represented
85 by <FirstTerm>chromosomes</FirstTerm>, in essence a set of character strings. A <FirstTerm>gene</FirstTerm> is a
86 subsection of a chromosome which encodes the value of a single parameter
87 being optimized. Typical encodings for a gene could be <FirstTerm>binary</FirstTerm> or
88 <FirstTerm>integer</FirstTerm>.
91 Through simulation of the evolutionary operations <FirstTerm>recombination</FirstTerm>,
92 <FirstTerm>mutation</FirstTerm>, and <FirstTerm>selection</FirstTerm> new generations of search points are found
93 that show a higher average fitness than their ancestors.
96 According to the "comp.ai.genetic" <Acronym>FAQ</Acronym> it cannot be stressed too
97 strongly that a <Acronym>GA</Acronym> is not a pure random search for a solution to a
98 problem. A <Acronym>GA</Acronym> uses stochastic processes, but the result is distinctly
99 non-random (better than random).
102 Structured Diagram of a <Acronym>GA</Acronym>:
103 ---------------------------
105 P(t) generation of ancestors at a time t
106 P''(t) generation of descendants at a time t
108 +=========================================+
109 |>>>>>>>>>>> Algorithm GA <<<<<<<<<<<<<<|
110 +=========================================+
111 | INITIALIZE t := 0 |
112 +=========================================+
114 +=========================================+
115 | evalute FITNESS of P(t) |
116 +=========================================+
117 | while not STOPPING CRITERION do |
118 | +-------------------------------------+
119 | | P'(t) := RECOMBINATION{P(t)} |
120 | +-------------------------------------+
121 | | P''(t) := MUTATION{P'(t)} |
122 | +-------------------------------------+
123 | | P(t+1) := SELECTION{P''(t) + P(t)} |
124 | +-------------------------------------+
125 | | evalute FITNESS of P''(t) |
126 | +-------------------------------------+
128 +===+=====================================+
132 <Title>Genetic Query Optimization (<Acronym>GEQO</Acronym>) in Postgres</Title>
135 The <Acronym>GEQO</Acronym> module is intended for the solution of the query
136 optimization problem similar to a traveling salesman problem (<Acronym>TSP</Acronym>).
137 Possible query plans are encoded as integer strings. Each string
138 represents the <Command>join</Command> order from one relation of the query to the next.
139 E. g., the query tree
146 is encoded by the integer string '4-1-3-2',
147 which means, first join relation '4' and '1', then '3', and
148 then '2', where 1, 2, 3, 4 are relids in <ProductName>Postgres</ProductName>.
151 Parts of the <Acronym>GEQO</Acronym> module are adapted from D. Whitley's Genitor
155 Specific characteristics of the <Acronym>GEQO</Acronym> implementation in <ProductName>Postgres</ProductName>
158 <ItemizedList Mark="bullet" Spacing="compact">
161 Usage of a <FirstTerm>steady state</FirstTerm> <Acronym>GA</Acronym> (replacement of the least fit
162 individuals in a population, not whole-generational replacement)
163 allows fast convergence towards improved query plans. This is
164 essential for query handling with reasonable time;
170 Usage of <FirstTerm>edge recombination crossover</FirstTerm> which is especially suited
171 to keep edge losses low for the solution of the <Acronym>TSP</Acronym> by means of a <Acronym>GA</Acronym>;
177 Mutation as genetic operator is deprecated so that no repair
178 mechanisms are needed to generate legal <Acronym>TSP</Acronym> tours.
184 The <Acronym>GEQO</Acronym> module gives the following benefits to the <ProductName>Postgres</ProductName> DBMS
185 compared to the <ProductName>Postgres</ProductName> query optimizer implementation:
187 <ItemizedList Mark="bullet" Spacing="compact">
190 Handling of large <Command>join</Command> queries through non-exhaustive search;
196 Improved cost size approximation of query plans since no longer
197 plan merging is needed (the <Acronym>GEQO</Acronym> module evaluates the cost for a
198 query plan as an individual).
206 <Title>Future Implementation Tasks for <ProductName>Postgres</ProductName> <Acronym>GEQO</Acronym></Title>
209 <Title>Basic Improvements</Title>
212 <Title>Improve freeing of memory when query is already processed</Title>
215 With large <Command>join</Command> queries the computing time spent for the genetic query
216 optimization seems to be a mere <Emphasis>fraction</Emphasis> of the time
217 <ProductName>Postgres</ProductName>
218 needs for freeing memory via routine <Function>MemoryContextFree</Function>,
219 file <FileName>backend/utils/mmgr/mcxt.c</FileName>.
220 Debugging showed that it get stucked in a loop of routine
221 <Function>OrderedElemPop</Function>, file <FileName>backend/utils/mmgr/oset.c</FileName>.
222 The same problems arise with long queries when using the normal
223 <ProductName>Postgres</ProductName> query optimization algorithm.
226 <Title>Improve genetic algorithm parameter settings</Title>
229 In file <FileName>backend/optimizer/geqo/geqo_params.c</FileName>, routines
230 <Function>gimme_pool_size</Function> and <Function>gimme_number_generations</Function>,
231 we have to find a compromise for the parameter settings
232 to satisfy two competing demands:
233 <ItemizedList Spacing="compact">
236 Optimality of the query plan
247 <Title>Find better solution for integer overflow</Title>
250 In file <FileName>backend/optimizer/geqo/geqo_eval.c</FileName>, routine
251 <Function>geqo_joinrel_size</Function>,
252 the present hack for MAXINT overflow is to set the <ProductName>Postgres</ProductName> integer
253 value of <StructField>rel->size</StructField> to its logarithm.
254 Modifications of <StructName>Rel</StructName> in <FileName>backend/nodes/relation.h</FileName> will
255 surely have severe impacts on the whole <ProductName>Postgres</ProductName> implementation.
258 <Title>Find solution for exhausted memory</Title>
261 Memory exhaustion may occur with more than 10 relations involved in a query.
262 In file <FileName>backend/optimizer/geqo/geqo_eval.c</FileName>, routine
263 <Function>gimme_tree</Function> is recursively called.
264 Maybe I forgot something to be freed correctly, but I dunno what.
265 Of course the <StructName>rel</StructName> data structure of the <Command>join</Command> keeps growing and
266 growing the more relations are packed into it.
267 Suggestions are welcome :-(
271 <Title>Further Improvements</Title>
274 Enable bushy query tree processing within <ProductName>Postgres</ProductName>;
275 that may improve the quality of query plans.
277 <BIBLIOGRAPHY Id="geqo-references">
281 <PARA>Reference information for <Acronym>GEQ</Acronym> algorithms.
287 The Hitch-Hiker's Guide to Evolutionary Computation
291 <FIRSTNAME>Jörg</FIRSTNAME>
292 <SURNAME>Heitkötter</SURNAME>
295 <FIRSTNAME>David</FIRSTNAME>
296 <SURNAME>Beasley</SURNAME>
306 FAQ in <ULink url="news://comp.ai.genetic">comp.ai.genetic</ULink>
307 is available at <ULink url="ftp://ftp.Germany.EU.net/pub/research/softcomp/EC/Welcome.html">Encore</ULink>.
314 The Design and Implementation of the Postgres Query Optimizer
318 <FIRSTNAME>Z.</FIRSTNAME>
319 <SURNAME>Fong</SURNAME>
324 University of California, Berkeley Computer Science Department
329 File <FileName>planner/Report.ps</FileName> in the 'postgres-papers' distribution.
336 Fundamentals of Database Systems
340 <FIRSTNAME>R.</FIRSTNAME>
341 <SURNAME>Elmasri</SURNAME>
344 <FIRSTNAME>S.</FIRSTNAME>
345 <SURNAME>Navathe</SURNAME>
350 The Benjamin/Cummings Pub., Inc.