MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_planner.h
1 #ifndef SQL_PLANNER_INCLUDED
2 #define SQL_PLANNER_INCLUDED
3 
4 /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
21 #include "sql_class.h"
22 #include "sql_select.h"
23 #include "sql_test.h"
24 #include "sql_optimizer.h"
25 
26 class Opt_trace_object;
27 
45 {
46 public:
47  Optimize_table_order(THD *thd, JOIN *join, TABLE_LIST *sjm_nest)
48  : search_depth(determine_search_depth(thd->variables.optimizer_search_depth,
49  join->tables - join->const_tables)),
50  prune_level(thd->variables.optimizer_prune_level),
51  thd(thd), join(join),
52  cur_embedding_map(0), emb_sjm_nest(sjm_nest),
53  excluded_tables((sjm_nest ?
54  (join->all_table_map & ~sjm_nest->sj_inner_tables) : 0) |
55  (join->allow_outer_refs ? 0 : OUTER_REF_TABLE_BIT))
56  {}
58  {}
65  bool choose_table_order();
66 
67 private:
68  const uint search_depth; // Maximum search depth to apply in greedy search
69  const uint prune_level; // pruning heuristics to be applied
70  // (0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
71  THD *const thd; // Pointer to current THD
72  JOIN *const join; // Pointer to the current plan being developed
77  nested_join_map cur_embedding_map;
82  const TABLE_LIST *const emb_sjm_nest;
91  const table_map excluded_tables;
92 
93  void best_access_path(JOIN_TAB *s, table_map remaining_tables, uint idx,
94  bool disable_jbuf, double record_count,
95  POSITION *pos, POSITION *loose_scan_pos);
96  bool check_interleaving_with_nj(JOIN_TAB *next_tab);
97  void advance_sj_state(table_map remaining_tables,
98  const JOIN_TAB *tab, uint idx,
99  double *current_rowcount, double *current_cost,
100  POSITION *loose_scan_pos);
101  void backout_nj_state(const table_map remaining_tables,
102  const JOIN_TAB *tab);
103  void optimize_straight_join(table_map join_tables);
104  bool greedy_search(table_map remaining_tables);
105  bool best_extension_by_limited_search(table_map remaining_tables,
106  uint idx,
107  double record_count,
108  double read_time,
109  uint current_search_depth);
110  table_map eq_ref_extension_by_limited_search(
111  table_map remaining_tables,
112  uint idx,
113  double record_count,
114  double read_time,
115  uint current_search_depth);
116  void consider_plan(uint idx, double record_count, double read_time,
117  Opt_trace_object *trace_obj);
118  bool fix_semijoin_strategies();
119  bool semijoin_firstmatch_loosescan_access_paths(
120  uint first_tab, uint last_tab, table_map remaining_tables,
121  bool loosescan, bool final,
122  double *newcount, double *newcost);
123  void semijoin_mat_scan_access_paths(
124  uint last_inner_tab, uint last_outer_tab,
125  table_map remaining_tables, TABLE_LIST *sjm_nest, bool final,
126  double *newcount, double *newcost);
127  void semijoin_mat_lookup_access_paths(
128  uint last_inner, TABLE_LIST *sjm_nest,
129  double *newcount, double *newcost);
130  void semijoin_dupsweedout_access_paths(
131  uint first_tab, uint last_tab,
132  table_map remaining_tables,
133  double *newcount, double *newcost);
134 
135  static uint determine_search_depth(uint search_depth, uint table_count);
136 };
137 
138 void get_partial_join_cost(JOIN *join, uint n_tables, double *read_time_arg,
139  double *record_count_arg);
140 
141 #endif /* SQL_PLANNER_INCLUDED */