My Project
sql_planner.h
00001 #ifndef SQL_PLANNER_INCLUDED
00002 #define SQL_PLANNER_INCLUDED
00003 
00004 /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; version 2 of the License.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00018 
00021 #include "sql_class.h"
00022 #include "sql_select.h"
00023 #include "sql_test.h"
00024 #include "sql_optimizer.h"
00025 
00026 class Opt_trace_object;
00027 
00044 class Optimize_table_order
00045 {
00046 public:
00047   Optimize_table_order(THD *thd, JOIN *join, TABLE_LIST *sjm_nest)
00048   : search_depth(determine_search_depth(thd->variables.optimizer_search_depth,
00049                                         join->tables - join->const_tables)),
00050     prune_level(thd->variables.optimizer_prune_level),
00051     thd(thd), join(join),
00052     cur_embedding_map(0), emb_sjm_nest(sjm_nest),
00053     excluded_tables((sjm_nest ?
00054                      (join->all_table_map & ~sjm_nest->sj_inner_tables) : 0) |
00055                     (join->allow_outer_refs ? 0 : OUTER_REF_TABLE_BIT))
00056   {}
00057   ~Optimize_table_order()
00058   {}
00065   bool choose_table_order();
00066 
00067 private:
00068   const uint search_depth;   // Maximum search depth to apply in greedy search
00069   const uint prune_level;    // pruning heuristics to be applied
00070                              // (0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
00071   THD *const thd;            // Pointer to current THD
00072   JOIN *const join;          // Pointer to the current plan being developed
00077   nested_join_map cur_embedding_map;
00082   const TABLE_LIST *const emb_sjm_nest;
00091   const table_map excluded_tables;
00092 
00093   void best_access_path(JOIN_TAB *s, table_map remaining_tables, uint idx, 
00094                         bool disable_jbuf, double record_count,
00095                         POSITION *pos, POSITION *loose_scan_pos);
00096   bool check_interleaving_with_nj(JOIN_TAB *next_tab);
00097   void advance_sj_state(table_map remaining_tables,
00098                         const JOIN_TAB *tab, uint idx,
00099                         double *current_rowcount, double *current_cost,
00100                         POSITION *loose_scan_pos);
00101   void backout_nj_state(const table_map remaining_tables,
00102                         const JOIN_TAB *tab);
00103   void optimize_straight_join(table_map join_tables);
00104   bool greedy_search(table_map remaining_tables);
00105   bool best_extension_by_limited_search(table_map remaining_tables,
00106                                         uint idx,
00107                                         double record_count,
00108                                         double read_time,
00109                                         uint current_search_depth);
00110   table_map eq_ref_extension_by_limited_search(
00111                                         table_map remaining_tables,
00112                                         uint idx,
00113                                         double record_count,
00114                                         double read_time,
00115                                         uint current_search_depth);
00116   void consider_plan(uint idx, double record_count, double read_time,
00117                      Opt_trace_object *trace_obj);
00118   bool fix_semijoin_strategies();
00119   bool semijoin_firstmatch_loosescan_access_paths(
00120                 uint first_tab, uint last_tab, table_map remaining_tables, 
00121                 bool loosescan, bool final,
00122                 double *newcount, double *newcost);
00123   void semijoin_mat_scan_access_paths(
00124                 uint last_inner_tab, uint last_outer_tab, 
00125                 table_map remaining_tables, TABLE_LIST *sjm_nest, bool final,
00126                 double *newcount, double *newcost);
00127   void semijoin_mat_lookup_access_paths(
00128                 uint last_inner, TABLE_LIST *sjm_nest,
00129                 double *newcount, double *newcost);
00130   void semijoin_dupsweedout_access_paths(
00131                 uint first_tab, uint last_tab, 
00132                 table_map remaining_tables, 
00133                 double *newcount, double *newcost);
00134 
00135   static uint determine_search_depth(uint search_depth, uint table_count);
00136 };
00137 
00138 void get_partial_join_cost(JOIN *join, uint n_tables, double *read_time_arg,
00139                            double *record_count_arg);
00140 
00141 #endif /* SQL_PLANNER_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines