]> granicus.if.org Git - postgresql/blob - doc/src/sgml/geqo.sgml
88ebb628ab2e381949a96825d8f08a77315f9212
[postgresql] / doc / src / sgml / geqo.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/geqo.sgml,v 1.5 1998/12/29 02:24:15 thomas Exp $
3 Genetic Optimizer
4
5 $Log: geqo.sgml,v $
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>.
10
11 Revision 1.4  1998/08/15 06:55:05  thomas
12 Change Id field in chapter tag to change html output file name.
13
14 -->
15
16 <Chapter Id="geqo">
17 <DocInfo>
18 <Author>
19 <FirstName>Martin</FirstName>
20 <SurName>Utesch</SurName>
21 <Affiliation>
22 <Orgname>
23 University of Mining and Technology
24 </Orgname>
25 <Orgdiv>
26 Institute of Automatic Control
27 </Orgdiv>
28 <Address>
29 <City>
30 Freiberg
31 </City>
32 <Country>
33 Germany
34 </Country>
35 </Address>
36 </Affiliation>
37 </Author>
38 <Date>1997-10-02</Date>
39 </DocInfo>
40
41 <Title>Genetic Query Optimization in Database Systems</Title>
42
43 <Para>
44 <Note>
45 <Title>Author</Title>
46 <Para>
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.
49 </Para>
50 </Note>
51 </para>
52
53 <Sect1>
54 <Title>Query Handling as a Complex Optimization Problem</Title>
55
56 <Para>
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.
64 </para>
65
66 <Para>
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
71 intelligence.
72 </para>
73
74 <Para>
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.
81 </para>
82
83 <Para>
84    Performance difficulties within exploring the space of possible query
85 plans arose the demand for a new optimization technique being developed.
86 </para>
87
88 <Para>
89    In the following we propose the implementation of a <FirstTerm>Genetic Algorithm</FirstTerm>
90  as an option for the database query optimization problem.
91 </para>
92 </sect1>
93
94 <Sect1>
95 <Title>Genetic Algorithms (<Acronym>GA</Acronym>)</Title>
96
97 <Para>
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>.
103 </para>
104
105 <Para>
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>.
111 </para>
112
113 <Para>
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.
117 </para>
118
119 <Para>
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). 
124
125 <ProgramListing>
126 Structured Diagram of a <Acronym>GA</Acronym>:
127 ---------------------------
128
129 P(t)    generation of ancestors at a time t
130 P''(t)  generation of descendants at a time t
131
132 +=========================================+
133 |>>>>>>>>>>>  Algorithm GA  <<<<<<<<<<<<<<|
134 +=========================================+
135 | INITIALIZE t := 0                       |
136 +=========================================+
137 | INITIALIZE P(t)                         |
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 |   +-------------------------------------+
151 |   | t := t + 1                          |
152 +===+=====================================+
153 </ProgramListing>
154 </para>
155 </sect1>
156
157 <Sect1>
158 <Title>Genetic Query Optimization (<Acronym>GEQO</Acronym>) in Postgres</Title>
159
160 <Para>
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
166 <ProgramListing>
167        /\
168       /\ 2
169      /\ 3
170     4  1
171 </ProgramListing>
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>.
175 </para>
176
177 <Para>
178    Parts of the <Acronym>GEQO</Acronym> module are adapted from D. Whitley's Genitor
179 algorithm.
180 </para>
181
182 <Para>
183    Specific characteristics of the <Acronym>GEQO</Acronym> implementation in <ProductName>Postgres</ProductName>
184 are:
185
186 <ItemizedList Mark="bullet" Spacing="compact">
187 <ListItem>
188 <Para>
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;
193 </Para>
194 </ListItem>
195
196 <ListItem>
197 <Para>
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>;
200 </Para>
201 </ListItem>
202
203 <ListItem>
204 <Para>
205 Mutation as genetic operator is deprecated so that no repair
206    mechanisms are needed to generate legal <Acronym>TSP</Acronym> tours.
207 </Para>
208 </ListItem>
209 </ItemizedList>
210 </para>
211
212 <Para>
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:
215
216 <ItemizedList Mark="bullet" Spacing="compact">
217 <ListItem>
218 <Para>
219 Handling of large <Command>join</Command> queries through non-exhaustive search;
220 </Para>
221 </ListItem>
222
223 <ListItem>
224 <Para>
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).
228 </Para>
229 </ListItem>
230 </ItemizedList>
231 </para>
232
233 </Sect1>
234
235 <Sect1>
236 <Title>Future Implementation Tasks for <ProductName>Postgres</ProductName> <Acronym>GEQO</Acronym></Title>
237
238 <Sect2>
239 <Title>Basic Improvements</Title>
240
241 <Sect3>
242 <Title>Improve freeing of memory when query is already processed</Title>
243
244 <Para>
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.
254 </para>
255 </sect3>
256
257 <Sect3>
258 <Title>Improve genetic algorithm parameter settings</Title>
259
260 <Para>
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">
266 <ListItem>
267 <Para>
268 Optimality of the query plan
269 </Para>
270 </ListItem>
271 <ListItem>
272 <Para>
273 Computing time
274 </Para>
275 </ListItem>
276 </ItemizedList>
277 </para>
278 </sect3>
279
280 <Sect3>
281 <Title>Find better solution for integer overflow</Title>
282
283 <Para>
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.
290 </para>
291 </sect3>
292
293 <Sect3>
294 <Title>Find solution for exhausted memory</Title>
295
296 <Para>
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 :-(
304 </para>
305 </sect3>
306 </sect2>
307
308 <Sect2>
309 <Title>Further Improvements</Title>
310
311 <Para>
312 Enable bushy query tree processing within <ProductName>Postgres</ProductName>;
313 that may improve the quality of query plans.
314 </para>
315
316 <BIBLIOGRAPHY Id="geqo-biblio">
317 <TITLE>
318 References
319 </TITLE>
320 <PARA>Reference information for <Acronym>GEQ</Acronym> algorithms.
321 </PARA>
322 <BIBLIOENTRY>
323
324 <BOOKBIBLIO>
325 <TITLE>
326 The Hitch-Hiker's Guide to Evolutionary Computation
327 </TITLE>
328 <AUTHORGROUP>
329 <AUTHOR>
330 <FIRSTNAME>J&ouml;rg</FIRSTNAME>
331 <SURNAME>Heitk&ouml;tter</SURNAME>
332 </AUTHOR>
333 <AUTHOR>
334 <FIRSTNAME>David</FIRSTNAME>
335 <SURNAME>Beasley</SURNAME>
336 </AUTHOR>
337 </AUTHORGROUP>
338 <PUBLISHER>
339 <PUBLISHERNAME>
340 InterNet resource
341 </PUBLISHERNAME>
342 </PUBLISHER>
343 <ABSTRACT>
344 <Para>
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>.
347 </Para>
348 </ABSTRACT>
349 </BOOKBIBLIO>
350
351 <BOOKBIBLIO>
352 <TITLE>
353 The Design and Implementation of the Postgres Query Optimizer
354 </TITLE>
355 <AUTHORGROUP>
356 <AUTHOR>
357 <FIRSTNAME>Z.</FIRSTNAME>
358 <SURNAME>Fong</SURNAME>
359 </AUTHOR>
360 </AUTHORGROUP>
361 <PUBLISHER>
362 <PUBLISHERNAME>
363 University of California, Berkeley Computer Science Department
364 </PUBLISHERNAME>
365 </PUBLISHER>
366 <ABSTRACT>
367 <Para>
368 File <FileName>planner/Report.ps</FileName> in the 'postgres-papers' distribution.
369 </Para>
370 </ABSTRACT>
371 </BOOKBIBLIO>
372
373 <BOOKBIBLIO>
374 <TITLE>
375 Fundamentals of Database Systems
376 </TITLE>
377 <AUTHORGROUP>
378 <AUTHOR>
379 <FIRSTNAME>R.</FIRSTNAME>
380 <SURNAME>Elmasri</SURNAME>
381 </AUTHOR>
382 <AUTHOR>
383 <FIRSTNAME>S.</FIRSTNAME>
384 <SURNAME>Navathe</SURNAME>
385 </AUTHOR>
386 </AUTHORGROUP>
387 <PUBLISHER>
388 <PUBLISHERNAME>
389 The Benjamin/Cummings Pub., Inc.
390 </PUBLISHERNAME>
391 </PUBLISHER>
392 </BOOKBIBLIO>
393
394 </BIBLIOENTRY>
395 </BIBLIOGRAPHY>
396
397 </sect2>
398 </sect1>
399 </Chapter>