README 3.7 KB
Newer Older
B
Bruce Momjian 已提交
1 2 3 4 5 6 7 8
The optimizer generates optimial query plans by doing several steps:

Take each relation in a query, and make a RelOptInfo structure for it. 
Find each way of accessing the relation, called a Path, including
sequential and index scans, and add it to the RelOptInfo.path_order
list.


B
Bruce Momjian 已提交
9 10 11
Optimizer Functions
-------------------

12
These directories take the Query structure returned by the parser, and
B
Bruce Momjian 已提交
13 14 15 16 17 18 19 20 21 22
generate a plan used by the executor.  The /plan directory generates the
plan, the /path generates all possible ways to join the tables, and
/prep handles special cases like inheritance.  /utils is utility stuff.

planner()
 handle inheritance by processing separately
-init_query_planner()
  preprocess target list
  preprocess qualifications(WHERE)
--query_planner()
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
   cnfify()
    Summary:

     Simple cases with all AND's are handled by removing the AND's:

     convert:   a = 1 AND b = 2 AND c = 3
     to:        a = 1, b = 2, c = 3

     Qualifications with OR's are handled differently.  OR's inside AND
     clauses are not modified drastically:

     convert:   a = 1 AND b = 2 AND (c = 3 OR d = 4)
     to:        a = 1, b = 2, c = 3 OR d = 4

     OR's in the upper level are more complex to handle:

     convert:   (a = 1 AND b = 2) OR c = 3
     to:        (a = 1 OR c = 3) AND (b = 2 OR c = 3)
     finally:   (a = 1 OR c = 3), (b = 2 OR c = 3)

     These clauses all have to be true for a result to be returned,
     so the optimizer can choose the most restrictive clauses.

B
Bruce Momjian 已提交
46 47 48 49 50 51
   pull out constants from target list
   get a target list that only contains column names, no expressions
   if none, then return
---subplanner()
    make list of relations in target
    make list of relations in where clause
52
     split up the qual into restrictions (a=1) and joins (b=c)
B
Bruce Momjian 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    find which relations can do merge sort and hash joins
----find_paths()
     find scan and all index paths for each relation not yet joined
     one relation, return
     find selectivity of columns used in joins
-----find_join_paths()
      Summary:  With OPTIMIZER_DEBUG defined, you see:

      Tables 1, 2, 3, and 4 are joined as:
         {1 2},{1 3},{1 4},{2 3},{2 4}
         {1 2 3},{1 2 4},{2 3 4}
         {1 2 3 4}

      Actual output tests show combinations:
         {4 2},{3 2},{1 4},{1 3},{1 2}
         {4 2 3},{1 4 2},{1 3 2}
         {4 2 3 1}

      Cheapest join order shows:
         {4 2},{3 2},{1 4},{1 3},{1 2}
         {3 2 4},{1 4 2},{1 3 2}
         {1 4 2 3}

      It first finds the best way to join each table to every other
      table.  It then takes those joined table combinations, and joins
      them to the other joined table combinations, until all tables are
      joined.

      jump to geqo if needed
      again:
       find_join_rels():
        for each joinrel:
         find_clause_joins()
          for each join on joinrel:
           if a join from the join clause adds only one relation, do the join
         or find_clauseless_joins()
       find_all_join_paths()
B
Bruce Momjian 已提交
90
        generate paths(nested,sortmerge) for joins found in find_join_rels()
B
Bruce Momjian 已提交
91 92 93 94
       prune_joinrels()
        remove from the join list the relation we just added to each join
       prune_rel_paths()
        set cheapest and perhaps remove unordered path, recompute table sizes
95
       if we have not done all the tables, go to again:
B
Bruce Momjian 已提交
96 97 98 99 100 101
   do group(GROUP)
   do aggregate
   put back constants
   re-flatten target list
 make unique(DISTINCT)
 make sort(ORDER BY)
102 103 104 105 106



Optimizer Structures
--------------------
B
Bruce Momjian 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120

RelOptInfo		- Every relation

 RestrictInfo	- restriction clauses
 JoinInfo		- join combinations

 Path			- every way to access a relation(sequential, index)
  IndexPath		- index scans

  JoinPath		- joins
   MergePath	- merge joins
   HashPath		- hash joins

 PathOrder		- every ordering type (sort, merge of relations)
121