バージョンごとのドキュメント一覧

62.3. PostgreSQLの遺伝的問い合わせ最適化(GEQO#

<title>Genetic Query Optimization (<acronym>GEQO</acronym>) in PostgreSQL</title>

The <acronym>GEQO</acronym> module approaches the query optimization problem as though it were the well-known traveling salesman problem (<acronym>TSP</acronym>). Possible query plans are encoded as integer strings. Each string represents the join order from one relation of the query to the next. For example, the join tree GEQOのモジュールは、問い合わせ最適化問題をあたかもよく知られている巡回セールスマン問題(TSP)のように扱います。 可能な問い合わせプランは、整数の文字列として符号化されます。 それぞれの文字列は、問い合わせの1つのリレーションから次へと結合の順番を表します。 例えば、以下の結合ツリーは整数文字列「4-1-3-2」によって符号化されています。

   /\
  /\ 2
 /\ 3
4  1

is encoded by the integer string '4-1-3-2', which means, first join relation '4' and '1', then '3', and then '2', where 1, 2, 3, 4 are relation IDs within the <productname>PostgreSQL</productname> optimizer. これが意味するのは、まずリレーション「4」と「1」を、次に「3」を、そして「2」を結合するということです。 ここで1、2、3、4はPostgreSQLオプティマイザ内でリレーションIDを表します。

Specific characteristics of the <acronym>GEQO</acronym> implementation in <productname>PostgreSQL</productname> are: PostgreSQLにおけるGEQO実装の特有な特徴は下記の様なものです。

Parts of the <acronym>GEQO</acronym> module are adapted from D. Whitley's Genitor algorithm. GEQOモジュールの部品は D. WhitleyのGenitorアルゴリズムを適合させたものです。

The <acronym>GEQO</acronym> module allows the <productname>PostgreSQL</productname> query optimizer to support large join queries effectively through non-exhaustive search. GEQOモジュールにより、PostgreSQL問い合わせオプティマイザが、大きな結合問い合わせをしらみつぶし検索以外の方法で実行することが可能になります。

62.3.1. GEQOを使用した計画候補の生成 #

<title>Generating Possible Plans with <acronym>GEQO</acronym></title>

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</quote> 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 &mdash; 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. GEQOの計画作成では、個々のリレーションのスキャンに対する計画を生成するために標準のプランナが使用されます。 そして、結合計画が遺伝的手法を用いて展開されます。 上で示した通り、 結合計画候補はそれぞれ、基本リレーションの結合順によって表現されています。 初期段階では、GEQOコードは単純にランダムに取り得る結合順をいくつか生成します。 考慮された結合順それぞれについて、標準プランナコードが呼び出され、その結合順を使用して問い合わせを行った場合のコストを推定します。 (結合順の各段階において、全体で3つの取り得る結合戦略が考慮されます。 そして、あらかじめ決められたリレーションスキャン計画もすべて利用可能です。 推定コストとはこれらの可能性の中から最も安価なものです。) より低い推定コストの結合順を、より高い推定コストのものよりより高い適応度と判断します。 遺伝的アルゴリズムは適応度が低い候補を破棄します。 そして、より多く合致する候補の遺伝子を組み合わせて、つまり、検討すべき新しい順序を作成するために既知の低コスト結合順をランダムに位置を選択して、新しい候補が生成されます。 事前に設定された数まで結合順を検討するまで、この処理が繰り返されます。 そして、この検索の間にもっとも優れたものが、最終的な計画を生成するために使用されます。

This process is inherently nondeterministic, because of the randomized choices made during both the initial population selection and subsequent <quote>mutation</quote> 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</varname> 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</varname>. 初期の群を選択する時、および、その後の最善の候補の突然変異の時に無作為な選択がなされますので、この処理は生来非決定論的なものです。 選択された計画の予期せぬ変化を避けるために、GEQOアルゴリズムの各実行では乱数生成器を現在のgeqo_seedパラメータ設定で再スタートさせます。 geqo_seedとその他のGEQOパラメータが変更されない限り、一定の問い合わせ(と統計のようなプランナへの他の入力)に対しては同じ計画が生成されます。 異なる検索パスで実験するためには、geqo_seedを変更してみて下さい。

62.3.2. PostgreSQL GEQOの今後の実装作業 #

<title>Future Implementation Tasks for <productname>PostgreSQL</productname> <acronym>GEQO</acronym></title>

Work is still needed to improve the genetic algorithm parameter settings. In file <filename>src/backend/optimizer/geqo/geqo_main.c</filename>, routines <function>gimme_pool_size</function> and <function>gimme_number_generations</function>, we have to find a compromise for the parameter settings to satisfy two competing demands: 遺伝的アルゴリズムのパラメータ設定を改善するためにはまだ課題が残っています。 src/backend/optimizer/geqo/geqo_main.cgimme_pool_sizegimme_number_generationsというルーチンでは、次の2つの相反する要求を満たす妥協点を見つけなければいけません。

  • Optimality of the query plan 問い合わせ計画の最適性

  • Computing time 計算時間

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. 現在の実装では、各結合順候補の適応度は標準プランナの結合選択と、一から作成したコスト推定コードを実行して推定されます。 異なる候補が同様の副結合順で使用されるにつれて、多くの作業が繰り返されることになります。 これは、副結合のコスト推定を記憶することで、非常に高速になるはずです。 この状態を記憶するために要するメモリ量が非合理的に拡大することを防止することが問題です。

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, the cost associated with any substring (partial tour) is independent of the rest of the tour, but this is certainly not true for query optimization. Thus it is questionable whether edge recombination crossover is the most effective mutation procedure. 最も基本的なレベルでは、TSP用に設計されたGAアルゴリズムを用いた問い合わせ最適化の解法が適切かどうかは明確ではありません。 TSPの場合は、部分文字列(巡回経路の一部)に関連付けられたコストは残りの巡回経路と独立していますが、これは問い合わせ最適化の場合には確実に成り立ちません。 したがって、辺再組合せ交叉が最も有効な突然変異手続きかどうかは疑わしいと言えます。