MySQL  8.0.22
Source Code Documentation
sql_optimizer.h
Go to the documentation of this file.
1 #ifndef SQL_OPTIMIZER_INCLUDED
2 #define SQL_OPTIMIZER_INCLUDED
3 
4 /* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
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, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 /**
27  @file sql/sql_optimizer.h
28  Classes used for query optimizations.
29 */
30 
31 /*
32  This structure is used to collect info on potentially sargable
33  predicates in order to check whether they become sargable after
34  reading const tables.
35  We form a bitmap of indexes that can be used for sargable predicates.
36  Only such indexes are involved in range analysis.
37 */
38 
39 #include <sys/types.h>
40 
41 #include <cstring>
42 #include <memory>
43 #include <utility>
44 
45 #include "field_types.h"
46 #include "my_alloc.h"
47 #include "my_base.h"
48 #include "my_dbug.h"
49 #include "my_table_map.h"
50 #include "sql/field.h"
51 #include "sql/item.h"
52 #include "sql/mem_root_array.h"
53 #include "sql/opt_explain_format.h" // Explain_sort_clause
54 #include "sql/row_iterator.h"
55 #include "sql/sql_executor.h"
56 #include "sql/sql_lex.h"
57 #include "sql/sql_list.h"
59 #include "sql/sql_select.h" // Key_use
60 #include "sql/table.h"
61 #include "sql/temp_table_param.h"
62 
63 enum class Subquery_strategy : int;
64 class COND_EQUAL;
65 class Item_subselect;
66 class Item_sum;
67 class Opt_trace_context;
68 class THD;
69 class Window;
70 struct AccessPath;
71 struct MYSQL_LOCK;
72 
73 class Item_equal;
74 template <class T>
76 
77 // Key_use has a trivial destructor, no need to run it from Mem_root_array.
78 typedef Mem_root_array<Key_use> Key_use_array;
79 
80 class Cost_model_server;
81 
83  Field *field; /* field against which to check sargability */
84  Item **arg_value; /* values of potential keys for lookups */
85  uint num_values; /* number of values in the above array */
86 };
87 
88 /**
89  Wrapper for ORDER* pointer to trace origins of ORDER list
90 
91  As far as ORDER is just a head object of ORDER expression
92  chain, we need some wrapper object to associate flags with
93  the whole ORDER list.
94 */
96  public:
97  ORDER *order; ///< ORDER expression that we are wrapping with this class
98  Explain_sort_clause src; ///< origin of order list
99 
100  private:
101  int flags; ///< bitmap of Explain_sort_property
102 
103  public:
104  ORDER_with_src() { clean(); }
105 
107  : order(order_arg),
108  src(src_arg),
109  flags(order_arg ? ESP_EXISTS : ESP_none) {}
110 
111  bool empty() const { return order == nullptr; }
112 
113  void clean() {
114  order = nullptr;
115  src = ESC_none;
116  flags = ESP_none;
117  }
118 
119  int get_flags() const {
120  DBUG_ASSERT(order);
121  return flags;
122  }
123 };
124 
125 class JOIN {
126  public:
127  JOIN(THD *thd_arg, SELECT_LEX *select);
128  JOIN(const JOIN &rhs) = delete;
129  JOIN &operator=(const JOIN &rhs) = delete;
130 
131  ~JOIN() {}
132 
133  /// Query block that is optimized and executed using this JOIN
135  /// Query expression referring this query block
137  /// Thread handler
138  THD *const thd;
139 
140  /**
141  Optimal query execution plan. Initialized with a tentative plan in
142  JOIN::make_join_plan() and later replaced with the optimal plan in
143  get_best_combination().
144  */
145  JOIN_TAB *join_tab{nullptr};
146  /// Array of QEP_TABs
147  QEP_TAB *qep_tab{nullptr};
148 
149  /**
150  Array of plan operators representing the current (partial) best
151  plan. The array is allocated in JOIN::make_join_plan() and is valid only
152  inside this function. Initially (*best_ref[i]) == join_tab[i].
153  The optimizer reorders best_ref.
154  */
155  JOIN_TAB **best_ref{nullptr};
156  /// mapping between table indexes and JOIN_TABs
157  JOIN_TAB **map2table{nullptr};
158  ///< mapping between table indexes and QEB_TABs
159  QEP_TAB **map2qep_tab{nullptr};
160  /*
161  The table which has an index that allows to produce the requried ordering.
162  A special value of 0x1 means that the ordering will be produced by
163  passing 1st non-const table to filesort(). NULL means no such table exists.
164  */
165  TABLE *sort_by_table{nullptr};
166  /**
167  Before plan has been created, "tables" denote number of input tables in the
168  query block and "primary_tables" is equal to "tables".
169  After plan has been created (after JOIN::get_best_combination()),
170  the JOIN_TAB objects are enumerated as follows:
171  - "tables" gives the total number of allocated JOIN_TAB objects
172  - "primary_tables" gives the number of input tables, including
173  materialized temporary tables from semi-join operation.
174  - "const_tables" are those tables among primary_tables that are detected
175  to be constant.
176  - "tmp_tables" is 0, 1 or 2 (more if windows) and counts the maximum
177  possible number of intermediate tables in post-processing (ie sorting and
178  duplicate removal).
179  Later, tmp_tables will be adjusted to the correct number of
180  intermediate tables, @see JOIN::make_tmp_tables_info.
181  - The remaining tables (ie. tables - primary_tables - tmp_tables) are
182  input tables to materialized semi-join operations.
183  The tables are ordered as follows in the join_tab array:
184  1. const primary table
185  2. non-const primary tables
186  3. intermediate sort/group tables
187  4. possible holes in array
188  5. semi-joined tables used with materialization strategy
189  */
190  uint tables{0}; ///< Total number of tables in query block
191  uint primary_tables{0}; ///< Number of primary input tables in query block
192  uint const_tables{0}; ///< Number of primary tables deemed constant
193  uint tmp_tables{0}; ///< Number of temporary tables used by query
194  uint send_group_parts{0};
195  /**
196  Indicates that the data will be aggregated (typically GROUP BY),
197  _and_ that it is already processed in an order that is compatible with
198  the grouping in use (e.g. because we are scanning along an index,
199  or because an earlier step sorted the data in a group-compatible order).
200 
201  Note that this flag changes value at multiple points during optimization;
202  if it's set when a temporary table is created, this means we aggregate
203  into said temporary table (end_write_group is chosen instead of end_write),
204  but if it's set later, it means that we can aggregate as we go,
205  just before sending the data to the client (end_send_group is chosen
206  instead of end_send).
207 
208  @see make_group_fields, alloc_group_fields, JOIN::exec
209  */
210  bool streaming_aggregation{false};
211  /// If query contains GROUP BY clause
212  bool grouped;
213  /// If true, send produced rows using query_result
214  bool do_send_rows{true};
215  /// Set of tables contained in query
216  table_map all_table_map{0};
217  table_map const_table_map; ///< Set of tables found to be const
218  /**
219  Const tables which are either:
220  - not empty
221  - empty but inner to a LEFT JOIN, thus "considered" not empty for the
222  rest of execution (a NULL-complemented row will be used).
223  */
225  /**
226  Used in some loops which scan the JOIN's tables: it is the bitmap of all
227  tables which are dependencies of lateral derived tables which the loop
228  has not yet processed.
229  */
230  table_map deps_of_remaining_lateral_derived_tables{0};
231 
232  /* Number of records produced after join + group operation */
233  ha_rows send_records{0};
234  ha_rows found_records{0};
235  ha_rows examined_rows{0};
236  ha_rows row_limit{0};
237  // m_select_limit is used to decide if we are likely to scan the whole table.
238  ha_rows m_select_limit{0};
239  /**
240  Used to fetch no more than given amount of rows per one
241  fetch operation of server side cursor.
242  The value is checked in end_send and end_send_group in fashion, similar
243  to offset_limit_cnt:
244  - fetch_limit= HA_POS_ERROR if there is no cursor.
245  - when we open a cursor, we set fetch_limit to 0,
246  - on each fetch iteration we add num_rows to fetch to fetch_limit
247  */
248  ha_rows fetch_limit{HA_POS_ERROR};
249 
250  /**
251  This is the result of join optimization.
252 
253  @note This is a scratch array, not used after get_best_combination().
254  */
255  POSITION *best_positions{nullptr};
256 
257  /******* Join optimization state members start *******/
258 
259  /* Current join optimization state */
260  POSITION *positions{nullptr};
261 
262  /* We also maintain a stack of join optimization states in * join->positions[]
263  */
264  /******* Join optimization state members end *******/
265 
266  /// A hook that secondary storage engines can use to override the executor
267  /// completely.
269  Override_executor_func override_executor_func = nullptr;
270 
271  /**
272  The cost of best complete join plan found so far during optimization,
273  after optimization phase - cost of picked join order (not taking into
274  account the changes made by test_if_skip_sort_order()).
275  */
276  double best_read{0.0};
277  /**
278  The estimated row count of the plan with best read time (see above).
279  */
280  ha_rows best_rowcount{0};
281  /// Expected cost of filesort.
282  double sort_cost{0.0};
283  /// Expected cost of windowing;
284  double windowing_cost{0.0};
286  List<Cached_item> group_fields{};
287  List<Cached_item> group_fields_cache{};
288  Item_sum **sum_funcs{nullptr};
289  /**
290  Describes a temporary table.
291  Each tmp table has its own tmp_table_param.
292  The one here has two roles:
293  - is transiently used as a model by create_intermediate_table(), to build
294  the tmp table's own tmp_table_param.
295  - is also used as description of the pseudo-tmp-table of grouping
296  (REF_SLICE_ORDERED_GROUP_BY) (e.g. in end_send_group()).
297  */
300 
301  enum class RollupState { NONE, INITED, READY };
303  bool implicit_grouping; ///< True if aggregated but no GROUP BY
304 
305  /**
306  At construction time, set if SELECT DISTINCT. May be reset to false
307  later, when we set up a temporary table operation that deduplicates for us.
308  */
310 
311  /**
312  If we have the GROUP BY statement in the query,
313  but the group_list was emptied by optimizer, this
314  flag is true.
315  It happens when fields in the GROUP BY are from
316  constant table
317  */
318  bool group_optimized_away{false};
319 
320  /*
321  simple_xxxxx is set if ORDER/GROUP BY doesn't include any references
322  to other tables than the first non-constant table in the JOIN.
323  It's also set if ORDER/GROUP BY is empty.
324  Used for deciding for or against using a temporary table to compute
325  GROUP/ORDER BY.
326  */
327  bool simple_order{false};
328  bool simple_group{false};
329 
330  /*
331  m_ordered_index_usage is set if an ordered index access
332  should be used instead of a filesort when computing
333  ORDER/GROUP BY.
334  */
335  enum {
336  ORDERED_INDEX_VOID, // No ordered index avail.
337  ORDERED_INDEX_GROUP_BY, // Use index for GROUP BY
338  ORDERED_INDEX_ORDER_BY // Use index for ORDER BY
339  } m_ordered_index_usage{ORDERED_INDEX_VOID};
340 
341  /**
342  Is set if we have a GROUP BY and we have ORDER BY on a constant or when
343  sorting isn't required.
344  */
345  bool skip_sort_order{false};
346 
347  /**
348  If true we need a temporary table on the result set before any
349  windowing steps, e.g. for DISTINCT or we have a query ORDER BY.
350  See details in JOIN::optimize
351  */
352  bool need_tmp_before_win{false};
353 
354  /// If JOIN has lateral derived tables (is set at start of planning)
355  bool has_lateral{false};
356 
357  /// Used and updated by JOIN::make_join_plan() and optimize_keyuse()
359 
360  /// List storing all expressions used in query block
362 
363  /**
364  Array of pointers to lists of expressions.
365  Each list represents the SELECT list at a certain stage of execution,
366  and also contains necessary extras: expressions added for ORDER BY,
367  GROUP BY, window clauses, underlying items of split items.
368  This array is only used when the query makes use of tmp tables: after
369  writing to tmp table (e.g. for GROUP BY), if this write also does a
370  function's calculation (e.g. of SUM), after the write the function's value
371  is in a column of the tmp table. If a SELECT list expression is the SUM,
372  and we now want to read that materialized SUM and send it forward, a new
373  expression (Item_field type instead of Item_sum), is needed. The new
374  expressions are listed in JOIN::tmp_fields_list[x]; 'x' is a number
375  (REF_SLICE_).
376  @see JOIN::make_tmp_tables_info()
377  */
378  mem_root_deque<Item *> *tmp_fields = nullptr;
379 
380  int error{0}; ///< set in optimize(), exec(), prepare_result()
381 
382  /**
383  ORDER BY and GROUP BY lists, to transform with prepare,optimize and exec
384  */
385  ORDER_with_src order, group_list;
386 
387  // Used so that AggregateIterator knows which items to signal when the rollup
388  // level changes. Obviously only used in the presence of rollup.
393 
394  /**
395  Any window definitions
396  */
398 
399  /**
400  True if a window requires a certain order of rows, which implies that any
401  order of rows coming out of the pre-window join will be disturbed.
402  */
403  bool m_windows_sort{false};
404 
405  /// If we have set up tmp tables for windowing, @see make_tmp_tables_info
406  bool m_windowing_steps{false};
407 
408  /**
409  Buffer to gather GROUP BY, ORDER BY and DISTINCT QEP details for EXPLAIN
410  */
411  Explain_format_flags explain_flags{};
412 
413  /**
414  JOIN::having_cond is initially equal to select_lex->having_cond, but may
415  later be changed by optimizations performed by JOIN.
416  The relationship between the JOIN::having_cond condition and the
417  associated variable select_lex->having_value is so that
418  having_value can be:
419  - COND_UNDEF if a having clause was not specified in the query or
420  if it has not been optimized yet
421  - COND_TRUE if the having clause is always true, in which case
422  JOIN::having_cond is set to NULL.
423  - COND_FALSE if the having clause is impossible, in which case
424  JOIN::having_cond is set to NULL
425  - COND_OK otherwise, meaning that the having clause needs to be
426  further evaluated
427  All of the above also applies to the where_cond/select_lex->cond_value
428  pair.
429  */
430  /**
431  Optimized WHERE clause item tree (valid for one single execution).
432  Used in JOIN execution if no tables. Otherwise, attached in pieces to
433  JOIN_TABs and then not used in JOIN execution.
434  Printed by EXPLAIN EXTENDED.
435  Initialized by SELECT_LEX::get_optimizable_conditions().
436  */
438  /**
439  Optimized HAVING clause item tree (valid for one single execution).
440  Used in JOIN execution, as last "row filtering" step. With one exception:
441  may be pushed to the JOIN_TABs of temporary tables used in DISTINCT /
442  GROUP BY (see JOIN::make_tmp_tables_info()); in that case having_cond is
443  set to NULL, but is first saved to having_for_explain so that EXPLAIN
444  EXTENDED can still print it.
445  Initialized by SELECT_LEX::get_optimizable_conditions().
446  */
448  Item *having_for_explain; ///< Saved optimized HAVING for EXPLAIN
449  /**
450  Pointer set to select_lex->get_table_list() at the start of
451  optimization. May be changed (to NULL) only if optimize_aggregated_query()
452  optimizes tables away.
453  */
455  COND_EQUAL *cond_equal{nullptr};
456  /*
457  Join tab to return to. Points to an element of join->join_tab array, or to
458  join->join_tab[-1].
459  This is used at execution stage to shortcut join enumeration. Currently
460  shortcutting is done to handle outer joins or handle semi-joins with
461  FirstMatch strategy.
462  */
463  plan_idx return_tab{0};
464 
465  /**
466  ref_items is an array of 5 slices, each containing an array of Item
467  pointers. ref_items is used in different phases of query execution.
468  - slice 0 is initially the same as SELECT_LEX::base_ref_items, ie it is
469  the set of items referencing fields from base tables. During optimization
470  and execution it may be temporarily overwritten by slice 1-3.
471  - slice 1 is a representation of the used items when being read from
472  the first temporary table.
473  - slice 2 is a representation of the used items when being read from
474  the second temporary table.
475  - slice 3 is a representation of the used items when used in
476  aggregation but no actual temporary table is needed.
477  - slice 4 is a copy of the original slice 0. It is created if
478  slice overwriting is necessary, and it is used to restore
479  original values in slice 0 after having been overwritten.
480  - slices 5 -> N are used by windowing:
481  first are all the window's out tmp tables,
482  the next indexes are reserved for the windows' frame buffers (in the same
483  order), if any, e.g.
484 
485  One window: 5: window 1's out table
486  6: window 1's FB
487 
488  Two windows: 5: window 1's out table
489  6: window 2's out table
490  7: window 1's FB
491  8: window 2's FB
492  and so on.
493 
494  Slice 0 is allocated for the lifetime of a statement, whereas slices 1-4
495  are associated with a single optimization. The size of slice 0 determines
496  the slice size used when allocating the other slices.
497  */
498  Ref_item_array *ref_items{
499  nullptr}; // cardinality: REF_SLICE_SAVED_BASE + 1 + #windows*2
500 
501  /**
502  If slice REF_SLICE_ORDERED_GROUP_BY has been created, this is the QEP_TAB
503  which is right before calculation of items in this slice.
504  */
505  QEP_TAB *ref_slice_immediately_before_group_by{nullptr};
506 
507  /**
508  The slice currently stored in ref_items[0].
509  Used to restore the base ref_items slice from the "save" slice after it
510  has been overwritten by another slice (1-3).
511  */
513 
514  /**
515  Used only if this query block is recursive. Contains count of
516  all executions of this recursive query block, since the last
517  this->reset().
518  */
519  uint recursive_iteration_count{0};
520 
521  /**
522  <> NULL if optimization has determined that execution will produce an
523  empty result before aggregation, contains a textual explanation on why
524  result is empty. Implicitly grouped queries may still produce an
525  aggregation row.
526  @todo - suggest to set to "Preparation determined that query is empty"
527  when SELECT_LEX::is_empty_query() is true.
528  */
529  const char *zero_result_cause{nullptr};
530 
531  /**
532  True if, at this stage of processing, subquery materialization is allowed
533  for children subqueries of this JOIN (those in the SELECT list, in WHERE,
534  etc). If false, and we have to evaluate a subquery at this stage, then we
535  must choose EXISTS.
536  */
537  bool child_subquery_can_materialize{false};
538  /**
539  True if plan search is allowed to use references to expressions outer to
540  this JOIN (for example may set up a 'ref' access looking up an outer
541  expression in the index, etc).
542  */
543  bool allow_outer_refs{false};
544 
545  /* Temporary tables used to weed-out semi-join duplicates */
546  List<TABLE> sj_tmp_tables{};
547  List<Semijoin_mat_exec> sjm_exec_list{};
548  /* end of allocation caching storage */
549 
550  /** Exec time only: true <=> current group has been sent */
551  bool group_sent{false};
552  /// If true, calculate found rows for this query block
553  bool calc_found_rows{false};
554 
555  /**
556  This will force tmp table to NOT use index + update for group
557  operation as it'll cause [de]serialization for each json aggregated
558  value and is very ineffective (times worse).
559  Server should use filesort, or tmp table + filesort to resolve GROUP BY
560  with JSON aggregate functions.
561  */
563 
564  /// True if plan is const, ie it will return zero or one rows.
565  bool plan_is_const() const { return const_tables == primary_tables; }
566 
567  /**
568  True if plan contains one non-const primary table (ie not including
569  tables taking part in semi-join materialization).
570  */
571  bool plan_is_single_table() { return primary_tables - const_tables == 1; }
572 
573  bool optimize();
574  void reset();
575  bool prepare_result();
576  void destroy();
577  bool alloc_func_list();
578  bool make_sum_func_list(const mem_root_deque<Item *> &fields,
579  bool before_group_by, bool recompute = false);
580 
581  /**
582  Overwrites one slice of ref_items with the contents of another slice.
583  In the normal case, dst and src have the same size().
584  However: the rollup slices may have smaller size than slice_sz.
585  */
586  void copy_ref_item_slice(uint dst_slice, uint src_slice) {
587  copy_ref_item_slice(ref_items[dst_slice], ref_items[src_slice]);
588  }
590  DBUG_ASSERT(dst_arr.size() >= src_arr.size());
591  void *dest = dst_arr.array();
592  const void *src = src_arr.array();
593  if (!src_arr.is_null())
594  memcpy(dest, src, src_arr.size() * src_arr.element_size());
595  }
596 
597  /**
598  Allocate a ref_item slice, assume that slice size is in ref_items[0]
599 
600  @param thd_arg thread handler
601  @param sliceno The slice number to allocate in JOIN::ref_items
602 
603  @returns false if success, true if error
604  */
605  bool alloc_ref_item_slice(THD *thd_arg, int sliceno);
606 
607  /**
608  Overwrite the base slice of ref_items with the slice supplied as argument.
609 
610  @param sliceno number to overwrite the base slice with, must be 1-4 or
611  4 + windowno.
612  */
613  void set_ref_item_slice(uint sliceno) {
614  DBUG_ASSERT((int)sliceno >= 1);
615  if (current_ref_item_slice != sliceno) {
616  copy_ref_item_slice(REF_SLICE_ACTIVE, sliceno);
617  DBUG_PRINT("info", ("JOIN %p ref slice %u -> %u", this,
618  current_ref_item_slice, sliceno));
619  current_ref_item_slice = sliceno;
620  }
621  }
622 
623  /// @note do also consider Switch_ref_item_slice
624  uint get_ref_item_slice() const { return current_ref_item_slice; }
625 
626  /**
627  Returns the clone of fields_list which is appropriate for evaluating
628  expressions at the current stage of execution; which stage is denoted by
629  the value of current_ref_item_slice.
630  */
631  mem_root_deque<Item *> *get_current_fields();
632 
633  bool optimize_rollup();
634  bool finalize_table_conditions();
635  /**
636  Release memory and, if possible, the open tables held by this execution
637  plan (and nested plans). It's used to release some tables before
638  the end of execution in order to increase concurrency and reduce
639  memory consumption.
640  */
641  void join_free();
642  /** Cleanup this JOIN. Not a full cleanup. reusable? */
643  void cleanup();
644 
645  bool clear_fields(table_map *save_nullinfo);
646  void restore_fields(table_map save_nullinfo);
647 
648  /**
649  Return whether the caller should send a row even if the join
650  produced no rows if:
651  - there is an aggregate function (sum_func_count!=0), and
652  - the query is not grouped, and
653  - a possible HAVING clause evaluates to TRUE.
654 
655  @note: if there is a having clause, it must be evaluated before
656  returning the row.
657  */
658  bool send_row_on_empty_set() const {
659  return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
660  group_list.empty() && !group_optimized_away &&
661  select_lex->having_value != Item::COND_FALSE);
662  }
663 
664  bool generate_derived_keys();
665  void finalize_derived_keys();
666  bool get_best_combination();
667  bool attach_join_conditions(plan_idx last_tab);
668 
669  private:
670  bool attach_join_condition_to_nest(plan_idx first_inner, plan_idx last_tab,
671  Item *join_cond, bool is_sj_mat_cond);
672 
673  public:
674  bool update_equalities_for_sjm();
675  bool add_sorting_to_table(uint idx, ORDER_with_src *order,
676  bool force_stable_sort, bool sort_before_group);
677  bool decide_subquery_strategy();
678  void refine_best_rowcount();
679  void recalculate_deps_of_remaining_lateral_derived_tables(
680  table_map plan_tables, uint idx);
681  bool clear_sj_tmp_tables();
682  bool clear_corr_derived_tmp_tables();
683 
684  void mark_const_table(JOIN_TAB *table, Key_use *key);
685  /// State of execution plan. Currently used only for EXPLAIN
687  NO_PLAN, ///< No plan is ready yet
688  ZERO_RESULT, ///< Zero result cause is set
689  NO_TABLES, ///< Plan has no tables
690  PLAN_READY ///< Plan is ready
691  };
692  /// See enum_plan_state
693  enum_plan_state get_plan_state() const { return plan_state; }
694  bool is_optimized() const { return optimized; }
695  void set_optimized() { optimized = true; }
696  bool is_executed() const { return executed; }
697  void set_executed() { executed = true; }
698 
699  /**
700  Retrieve the cost model object to be used for this join.
701 
702  @return Cost model object for the join
703  */
704 
705  const Cost_model_server *cost_model() const;
706 
707  /**
708  Check if FTS index only access is possible
709  */
710  bool fts_index_access(JOIN_TAB *tab);
711 
712  QEP_TAB::enum_op_type get_end_select_func();
713  /**
714  Propagate dependencies between tables due to outer join relations.
715 
716  @returns false if success, true if error
717  */
718  bool propagate_dependencies();
719 
720  /**
721  Handle offloading of query parts to the underlying engines, when
722  such is supported by their implementation.
723 
724  @returns false if success, true if error
725  */
726  bool push_to_engines();
727 
728  AccessPath *root_access_path() const { return m_root_access_path; }
729 
730  private:
731  bool optimized{false}; ///< flag to avoid double optimization in EXPLAIN
732 
733  /**
734  Set by exec(), reset by reset(). Note that this needs to be set
735  _during_ the query (not only when it's done executing), or the
736  dynamic range optimizer will not understand which tables have been
737  read.
738  */
739  bool executed{false};
740 
741  /// Final execution plan state. Currently used only for EXPLAIN
742  enum_plan_state plan_state{NO_PLAN};
743 
744  public:
745  /*
746  When join->select_count is set, tables will not be optimized away.
747  The call to records() will be delayed until the execution phase and
748  the counting will be done on an index of Optimizer's choice.
749  The index will be decided in find_shortest_key(), called from
750  optimize_aggregated_query().
751  */
752  bool select_count{false};
753 
754  private:
755  /**
756  Create a temporary table to be used for processing DISTINCT/ORDER
757  BY/GROUP BY.
758 
759  @note Will modify JOIN object wrt sort/group attributes
760 
761  @param tab the JOIN_TAB object to attach created table to
762  @param tmp_table_fields List of items that will be used to define
763  column types of the table.
764  @param tmp_table_group Group key to use for temporary table, empty if none.
765  @param save_sum_fields If true, do not replace Item_sum items in
766  @c tmp_fields list with Item_field items referring
767  to fields in temporary table.
768 
769  @returns false on success, true on failure
770  */
771  bool create_intermediate_table(QEP_TAB *tab,
772  const mem_root_deque<Item *> &tmp_table_fields,
773  ORDER_with_src &tmp_table_group,
774  bool save_sum_fields);
775 
776  /**
777  Optimize distinct when used on a subset of the tables.
778 
779  E.g.,: SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
780  In this case we can stop scanning t2 when we have found one t1.a
781  */
782  void optimize_distinct();
783 
784  /**
785  Function sets FT hints, initializes FT handlers and
786  checks if FT index can be used as covered.
787  */
788  bool optimize_fts_query();
789 
790  bool prune_table_partitions();
791  /**
792  Initialize key dependencies for join tables.
793 
794  TODO figure out necessity of this method. Current test
795  suite passed without this intialization.
796  */
798  JOIN_TAB *const tab_end = join_tab + tables;
799  for (JOIN_TAB *tab = join_tab; tab < tab_end; tab++)
800  tab->key_dependent = tab->dependent;
801  }
802 
803  private:
804  void set_prefix_tables();
805  void cleanup_item_list(const mem_root_deque<Item *> &items) const;
806  void set_semijoin_embedding();
807  bool make_join_plan();
808  bool init_planner_arrays();
809  bool extract_const_tables();
810  bool extract_func_dependent_tables();
811  void update_sargable_from_const(SARGABLE_PARAM *sargables);
812  bool estimate_rowcount();
813  void optimize_keyuse();
814  void set_semijoin_info();
815  /**
816  An utility function - apply heuristics and optimize access methods to tables.
817  @note Side effect - this function could set 'Impossible WHERE' zero
818  result.
819  */
820  void adjust_access_methods();
821  void update_depend_map();
822  void update_depend_map(ORDER *order);
823  /**
824  Fill in outer join related info for the execution plan structure.
825 
826  For each outer join operation left after simplification of the
827  original query the function set up the following pointers in the linear
828  structure join->join_tab representing the selected execution plan.
829  The first inner table t0 for the operation is set to refer to the last
830  inner table tk through the field t0->last_inner.
831  Any inner table ti for the operation are set to refer to the first
832  inner table ti->first_inner.
833  The first inner table t0 for the operation is set to refer to the
834  first inner table of the embedding outer join operation, if there is any,
835  through the field t0->first_upper.
836  The on expression for the outer join operation is attached to the
837  corresponding first inner table through the field t0->on_expr_ref.
838  Here ti are structures of the JOIN_TAB type.
839 
840  EXAMPLE. For the query:
841  @code
842  SELECT * FROM t1
843  LEFT JOIN
844  (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
845  ON (t1.a=t2.a AND t1.b=t3.b)
846  WHERE t1.c > 5,
847  @endcode
848 
849  given the execution plan with the table order t1,t2,t3,t4
850  is selected, the following references will be set;
851  t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
852  t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
853  on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
854  *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
855 
856  @note
857  The function assumes that the simplification procedure has been
858  already applied to the join query (see simplify_joins).
859  This function can be called only after the execution plan
860  has been chosen.
861  */
862  void make_outerjoin_info();
863 
864  /**
865  Initialize ref access for all tables that use it.
866 
867  @return False if success, True if error
868 
869  @note We cannot setup fields used for ref access before we have sorted
870  the items within multiple equalities according to the final order of
871  the tables involved in the join operation. Currently, this occurs in
872  @see substitute_for_best_equal_field().
873  */
874  bool init_ref_access();
875  bool alloc_qep(uint n);
876  void unplug_join_tabs();
877  bool setup_semijoin_materialized_table(JOIN_TAB *tab, uint tableno,
878  POSITION *inner_pos,
879  POSITION *sjm_pos);
880 
881  bool add_having_as_tmp_table_cond(uint curr_tmp_table);
882  bool make_tmp_tables_info();
883  void set_plan_state(enum_plan_state plan_state_arg);
884  bool compare_costs_of_subquery_strategies(Subquery_strategy *method);
885  ORDER *remove_const(ORDER *first_order, Item *cond, bool change_list,
886  bool *simple_order, bool group_by);
887 
888  /**
889  Check whether this is a subquery that can be evaluated by index look-ups.
890  If so, change subquery engine to subselect_indexsubquery_engine.
891 
892  @retval 1 engine was changed
893  @retval 0 engine wasn't changed
894  @retval -1 OOM or other error
895  */
896  int replace_index_subquery();
897 
898  /**
899  Optimize DISTINCT, GROUP BY, ORDER BY clauses
900 
901  @retval false ok
902  @retval true an error occurred
903  */
904  bool optimize_distinct_group_order();
905 
906  /**
907  Test if an index could be used to replace filesort for ORDER BY/GROUP BY
908 
909  @details
910  Investigate whether we may use an ordered index as part of either
911  DISTINCT, GROUP BY or ORDER BY execution. An ordered index may be
912  used for only the first of any of these terms to be executed. This
913  is reflected in the order which we check for test_if_skip_sort_order()
914  below. However we do not check for DISTINCT here, as it would have
915  been transformed to a GROUP BY at this stage if it is a candidate for
916  ordered index optimization.
917  If a decision was made to use an ordered index, the availability
918  if such an access path is stored in 'm_ordered_index_usage' for later
919  use by 'execute' or 'explain'
920  */
921  void test_skip_sort();
922 
923  bool alloc_indirection_slices();
924 
925  /**
926  Convert the executor structures to a set of access paths, storing
927  the result in m_root_access_path.
928  */
929  void create_access_paths();
930 
931  /**
932  Create access paths with the knowledge that there are going to be zero rows
933  coming from tables (before aggregation); typically because we know that
934  all of them would be filtered away by WHERE (e.g. SELECT * FROM t1
935  WHERE 1=2). This will normally yield no output rows, but if we have implicit
936  aggregation, it might yield a single one.
937  */
938  void create_access_paths_for_zero_rows();
939 
940  void create_access_paths_for_index_subquery();
941 
942  /** @{ Helpers for create_access_paths. */
943  AccessPath *create_root_access_path_for_join();
944  AccessPath *attach_access_paths_for_having_and_limit(AccessPath *path);
945  /** @} */
946 
947  /**
948  An access path you can read from to get all records for this query
949  (after you create an iterator from it).
950  */
951  AccessPath *m_root_access_path = nullptr;
952 };
953 
954 /**
955  Use this in a function which depends on best_ref listing tables in the
956  final join order. If 'tables==0', one is not expected to consult best_ref
957  cells, and best_ref may not even have been allocated.
958 */
959 #define ASSERT_BEST_REF_IN_JOIN_ORDER(join) \
960  do { \
961  DBUG_ASSERT((join)->tables == 0 || \
962  ((join)->best_ref && !(join)->join_tab)); \
963  } while (0)
964 
965 /**
966  RAII class to ease the temporary switching to a different slice of
967  the ref item array.
968 */
972 
973  public:
974  Switch_ref_item_slice(JOIN *join_arg, uint new_v)
975  : join(join_arg), saved(join->get_ref_item_slice()) {
976  if (!join->ref_items[new_v].is_null()) join->set_ref_item_slice(new_v);
977  }
979 };
980 
981 bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno,
982  bool other_tbls_ok);
983 bool remove_eq_conds(THD *thd, Item *cond, Item **retcond,
984  Item::cond_result *cond_value);
985 bool optimize_cond(THD *thd, Item **conds, COND_EQUAL **cond_equal,
986  mem_root_deque<TABLE_LIST *> *join_list,
987  Item::cond_result *cond_value);
989  COND_EQUAL *cond_equal,
990  JOIN_TAB **table_join_idx);
991 bool build_equal_items(THD *thd, Item *cond, Item **retcond,
992  COND_EQUAL *inherited, bool do_inherit,
993  mem_root_deque<TABLE_LIST *> *join_list,
994  COND_EQUAL **cond_equal_ref);
996  mem_root_deque<Item_field *> *out_args);
998  THD *thd, uint keyparts, Item_field **fields,
999  const mem_root_deque<Item *> &outer_exprs);
1000 Item_field *get_best_field(Item_field *item_field, COND_EQUAL *cond_equal);
1001 Item *make_cond_for_table(THD *thd, Item *cond, table_map tables,
1002  table_map used_table, bool exclude_expensive_cond);
1004  uint first_unused);
1005 
1006 /**
1007  Create an order list that consists of all non-const fields and items.
1008  This is usable for e.g. converting DISTINCT into GROUP or ORDER BY.
1009 
1010  Try to put the items in "order_list" first, to allow one to optimize away
1011  a later ORDER BY.
1012  */
1013 ORDER *create_order_from_distinct(THD *thd, Ref_item_array ref_item_array,
1014  ORDER *order_list,
1015  const mem_root_deque<Item *> &fields,
1016  bool skip_aggregates,
1017  bool convert_bit_fields_to_long,
1018  bool *all_order_by_fields_used);
1019 
1020 /**
1021  Returns true if arguments are a temporal Field having no date,
1022  part and a temporal expression having a date part.
1023  @param f Field
1024  @param v Expression
1025  */
1026 inline bool field_time_cmp_date(const Field *f, const Item *v) {
1027  const enum_field_types ft = f->type();
1028  return is_temporal_type(ft) && !is_temporal_type_with_date(ft) &&
1029  v->is_temporal_with_date();
1030 }
1031 
1032 bool substitute_gc(THD *thd, SELECT_LEX *select_lex, Item *where_cond,
1033  ORDER *group_list, ORDER *order);
1034 
1035 /// RAII class to manage JOIN::deps_of_remaining_lateral_derived_tables
1039  /// All lateral tables not part of this map should be ignored
1041 
1042  public:
1043  /**
1044  Constructor.
1045  @param j the JOIN
1046  @param plan_tables_arg table_map of derived tables @see
1047  JOIN::deps_of_remaining_lateral_derived_tables
1048  */
1050  : join(j),
1051  saved(join->deps_of_remaining_lateral_derived_tables),
1052  plan_tables(plan_tables_arg) {}
1057  }
1058  void recalculate(uint next_idx) {
1059  if (join->has_lateral)
1060  /*
1061  No cur_tab given, so assume we start from a place in the plan which
1062  may be backward or forward compared to where we were before:
1063  recalculate.
1064  */
1066  next_idx);
1067  }
1068 
1069  void recalculate(JOIN_TAB *cur_tab, uint next_idx) {
1070  /*
1071  We have just added cur_tab to the plan; if it's not lateral, the map
1072  doesn't change, no need to recalculate it.
1073  */
1074  if (join->has_lateral && cur_tab->table_ref->is_derived() &&
1075  cur_tab->table_ref->derived_unit()->m_lateral_deps)
1076  recalculate(next_idx);
1077  }
1078  void init() {
1079  // Normally done once in a run of JOIN::optimize().
1080  if (join->has_lateral) {
1081  recalculate(join->const_tables);
1082  // Forget stale value:
1084  }
1085  }
1086 };
1087 
1088 /**
1089  Estimates how many times a subquery will be executed as part of a
1090  query execution. If it is a cacheable subquery, the estimate tells
1091  how many times the subquery will be executed if it is not cached.
1092 
1093  @param[in] subquery the Item that represents the subquery
1094  @param[in,out] trace optimizer trace context
1095 
1096  @return the number of times the subquery is expected to be executed
1097 */
1098 double calculate_subquery_executions(const Item_subselect *subquery,
1099  Opt_trace_context *trace);
1100 
1101 extern const char *antijoin_null_cond;
1102 
1103 /**
1104  Checks if an Item, which is constant for execution, can be evaluated during
1105  optimization. It cannot be evaluated if it contains a subquery and the
1106  OPTION_NO_SUBQUERY_DURING_OPTIMIZATION query option is active.
1107 
1108  @param item the Item to check
1109  @param select the query block that contains the Item
1110  @return false if this Item contains a subquery and subqueries cannot be
1111  evaluated during optimization, or true otherwise
1112 */
1113 bool evaluate_during_optimization(const Item *item, const SELECT_LEX *select);
1114 
1115 /**
1116  Find the multiple equality predicate containing a field.
1117 
1118  The function retrieves the multiple equalities accessed through
1119  the cond_equal structure from current level and up looking for
1120  an equality containing a field. It stops retrieval as soon as the equality
1121  is found and set up inherited_fl to true if it's found on upper levels.
1122 
1123  @param cond_equal multiple equalities to search in
1124  @param item_field field to look for
1125  @param[out] inherited_fl set up to true if multiple equality is found
1126  on upper levels (not on current level of
1127  cond_equal)
1128 
1129  @return
1130  - Item_equal for the found multiple equality predicate if a success;
1131  - nullptr otherwise.
1132 */
1134  const Item_field *item_field, bool *inherited_fl);
1135 
1136 #endif /* SQL_OPTIMIZER_INCLUDED */
Definition: item.h:844
Key_use_array * create_keyuse_for_table(THD *thd, uint keyparts, Item_field **fields, const mem_root_deque< Item *> &outer_exprs)
Create a keyuse array for a table with a primary key.
Definition: sql_optimizer.cc:8151
Definition: item.h:3908
This file contains the field type.
Definition: sql_optimizer.h:125
Original query has this clause.
Definition: opt_explain_format.h:440
void restore()
Definition: sql_optimizer.h:1054
Definition: sql_optimizer.h:336
bool build_equal_items(THD *thd, Item *cond, Item **retcond, COND_EQUAL *inherited, bool do_inherit, mem_root_deque< TABLE_LIST *> *join_list, COND_EQUAL **cond_equal_ref)
Build multiple equalities for a WHERE condition and all join conditions that inherit these multiple e...
Definition: sql_optimizer.cc:4098
AccessPath * root_access_path() const
Definition: sql_optimizer.h:728
bool is_temporal_type_with_date(enum_field_types type)
Tests if field type is temporal and has date part, i.e.
Definition: field.h:454
bool has_lateral
If JOIN has lateral derived tables (is set at start of planning)
Definition: sql_optimizer.h:355
Item ** arg_value
Definition: sql_optimizer.h:84
Definition: table.h:272
~Switch_ref_item_slice()
Definition: sql_optimizer.h:978
enum_op_type
Definition: sql_executor.h:447
Deps_of_remaining_lateral_derived_tables(JOIN *j, table_map plan_tables_arg)
Constructor.
Definition: sql_optimizer.h:1049
bool is_temporal_with_date() const
Definition: item.h:2895
bool optimize_cond(THD *thd, Item **conds, COND_EQUAL **cond_equal, mem_root_deque< TABLE_LIST *> *join_list, Item::cond_result *cond_value)
Optimize conditions by.
Definition: sql_optimizer.cc:9926
TABLE_LIST * table_ref
points to table reference
Definition: sql_select.h:612
Plan has no tables.
Definition: sql_optimizer.h:689
#define bool
Definition: config_static.h:42
A wrapper class which provides array bounds checking.
Definition: sql_array.h:41
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
enum_plan_state get_plan_state() const
See enum_plan_state.
Definition: sql_optimizer.h:693
Ref_item_array * ref_items
ref_items is an array of 5 slices, each containing an array of Item pointers.
Definition: sql_optimizer.h:498
Item_field * get_best_field(Item_field *item_field, COND_EQUAL *cond_equal)
Get the best field substitution for a given field.
Definition: sql_optimizer.cc:3411
table_map found_const_table_map
Const tables which are either:
Definition: sql_optimizer.h:224
Definition: sql_optimizer.h:337
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1089
Definition: field.h:723
enum_plan_state
State of execution plan. Currently used only for EXPLAIN.
Definition: sql_optimizer.h:686
void recalculate_deps_of_remaining_lateral_derived_tables(table_map plan_tables, uint idx)
Updates JOIN::deps_of_remaining_lateral_derived_tables.
Definition: sql_optimizer.cc:3007
const char * antijoin_null_cond
Definition: sql_optimizer.cc:112
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:96
uint get_ref_item_slice() const
Definition: sql_optimizer.h:624
Represents the (explicit) window of a SQL 2003 section 7.11 <window clause>, or the implicit (inlined...
Definition: window.h:94
No plan is ready yet.
Definition: sql_optimizer.h:687
uint current_ref_item_slice
The slice currently stored in ref_items[0].
Definition: sql_optimizer.h:512
uint saved
Definition: sql_optimizer.h:971
bool select_distinct
At construction time, set if SELECT DISTINCT.
Definition: sql_optimizer.h:309
Item * make_cond_for_table(THD *thd, Item *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
Extract a condition that can be checked after reading given table.
Definition: sql_optimizer.cc:9106
RollupState
Definition: sql_optimizer.h:301
bool grouped
If query contains GROUP BY clause.
Definition: sql_optimizer.h:212
bool is_optimized() const
Definition: sql_optimizer.h:694
std::string join(Container cont, const std::string &delim)
join elements of an container into a string seperated by a delimiter.
Definition: string.h:144
THD *const thd
Thread handler.
Definition: sql_optimizer.h:138
void assert_unchanged()
Definition: sql_optimizer.h:1055
#define PSI_NOT_INSTRUMENTED
Definition: validate_password_imp.cc:39
A Key_use represents an equality predicate of the form (table.column = val), where the column is inde...
Definition: sql_select.h:169
MYSQL_LOCK * lock
Definition: sql_optimizer.h:299
bool send_row_on_empty_set() const
Return whether the caller should send a row even if the join produced no rows if: ...
Definition: sql_optimizer.h:658
Definition: item_subselect.h:78
Zero result cause is set.
Definition: sql_optimizer.h:688
table_map saved
Definition: sql_optimizer.h:1038
Common types of the Optimizer, used by optimization and execution.
Definition: table.h:1363
Definition: test_services.cc:49
bool is_derived() const
Return true if this represents a derived table (an unnamed view)
Definition: table.h:2806
bool remove_eq_conds(THD *thd, Item *cond, Item **retcond, Item::cond_result *cond_value)
Removes const and eq items.
Definition: sql_optimizer.cc:10039
int flags
bitmap of Explain_sort_property
Definition: sql_optimizer.h:101
RollupState rollup_state
Definition: sql_optimizer.h:302
cond_result
Definition: item.h:844
Explain_sort_clause src
origin of order list
Definition: sql_optimizer.h:98
bool evaluate_during_optimization(const Item *item, const SELECT_LEX *select)
Checks if an Item, which is constant for execution, can be evaluated during optimization.
Definition: sql_optimizer.cc:11006
int8 plan_idx
This represents the index of a JOIN_TAB/QEP_TAB in an array.
Definition: sql_opt_exec_shared.h:39
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:181
This file includes constants used by all storage engines.
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
void init()
Definition: sql_optimizer.h:1078
EXPLAIN FORMAT=<format> <command>.
Definition: item_cmpfunc.h:2430
bool is_indexed_agg_distinct(JOIN *join, mem_root_deque< Item_field *> *out_args)
Check for the presence of AGGFN(DISTINCT a) queries that may be subject to loose index scan...
Definition: sql_optimizer.cc:7755
bool field_time_cmp_date(const Field *f, const Item *v)
Returns true if arguments are a temporal Field having no date, part and a temporal expression having ...
Definition: sql_optimizer.h:1026
~JOIN()
Definition: sql_optimizer.h:131
Element_type * array() const
Definition: sql_array.h:105
bool substitute_gc(THD *thd, SELECT_LEX *select_lex, Item *where_cond, ORDER *group_list, ORDER *order)
Substitute all expressions in the WHERE condition and ORDER/GROUP lists that match generated columns ...
Definition: sql_optimizer.cc:985
void recalculate(JOIN_TAB *cur_tab, uint next_idx)
Definition: sql_optimizer.h:1069
void recalculate(uint next_idx)
Definition: sql_optimizer.h:1058
Class Item_sum is the base class used for special expressions that SQL calls &#39;set functions&#39;...
Definition: item_sum.h:394
void set_optimized()
Definition: sql_optimizer.h:695
JOIN * join
Definition: sql_optimizer.h:1037
Definition: opt_explain_format.h:447
Classes for query execution.
TABLE_LIST * tables_list
Pointer set to select_lex->get_table_list() at the start of optimization.
Definition: sql_optimizer.h:454
table_map m_lateral_deps
If &#39;this&#39; is body of lateral derived table: map of tables in the same FROM clause as this derived tab...
Definition: sql_lex.h:800
static char * path
Definition: mysqldump.cc:131
ORDER_with_src(ORDER *order_arg, Explain_sort_clause src_arg)
Definition: sql_optimizer.h:106
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
SELECT_LEX_UNIT * derived_unit() const
Return the query expression of a derived table or view.
Definition: table.h:3008
Definition: item.h:775
double calculate_subquery_executions(const Item_subselect *subquery, Opt_trace_context *trace)
Estimates how many times a subquery will be executed as part of a query execution.
Definition: sql_optimizer.cc:10811
unsigned int uint
Definition: uca-dump.cc:29
table_map const_table_map
Set of tables found to be const.
Definition: sql_optimizer.h:217
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, bool other_tbls_ok)
Check if given expression only uses fields covered by index keyno in the table tbl.
Definition: sql_optimizer.cc:6100
ORDER * order
ORDER expression that we are wrapping with this class.
Definition: sql_optimizer.h:97
uint const_tables
Number of primary tables deemed constant.
Definition: sql_optimizer.h:192
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:618
SELECT_LEX_UNIT *const unit
Query expression referring this query block.
Definition: sql_optimizer.h:136
bool plan_is_single_table()
True if plan contains one non-const primary table (ie not including tables taking part in semi-join m...
Definition: sql_optimizer.h:571
The slice which is used during evaluation of expressions; Item_ref::ref points there.
Definition: sql_opt_exec_shared.h:610
bool is_null() const
Definition: sql_array.h:97
Key_use_array keyuse_array
Used and updated by JOIN::make_join_plan() and optimize_keyuse()
Definition: sql_optimizer.h:358
uint sum_func_count
Number of fields in the query that have aggregate functions.
Definition: temp_table_param.h:128
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:416
bool implicit_grouping
True if aggregated but no GROUP BY.
Definition: sql_optimizer.h:303
size_t element_size() const
Definition: sql_array.h:94
bool with_json_agg
This will force tmp table to NOT use index + update for group operation as it&#39;ll cause [de]serializat...
Definition: sql_optimizer.h:562
mem_root_deque< Item * > * fields
Definition: sql_optimizer.h:285
std::string HARNESS_EXPORT reset()
get &#39;reset attributes&#39; ESC sequence.
Definition: vt100.cc:36
Definition: sql_executor.h:276
static const char * key
Definition: suite_stubs.c:14
bool plan_is_const() const
True if plan is const, ie it will return zero or one rows.
Definition: sql_optimizer.h:565
table_map plan_tables
All lateral tables not part of this map should be ignored.
Definition: sql_optimizer.h:1040
void copy_ref_item_slice(uint dst_slice, uint src_slice)
Overwrites one slice of ref_items with the contents of another slice.
Definition: sql_optimizer.h:586
uint num_values
Definition: sql_optimizer.h:85
Item * where_cond
JOIN::having_cond is initially equal to select_lex->having_cond, but may later be changed by optimiza...
Definition: sql_optimizer.h:437
Item::cond_result having_value
Definition: sql_lex.h:1960
Field * field
Definition: sql_optimizer.h:83
SELECT_LEX *const select_lex
Query block that is optimized and executed using this JOIN.
Definition: sql_optimizer.h:134
A per-session context which is always available at any point of execution, because in practice it&#39;s a...
Definition: opt_trace_context.h:88
A position of table within a join order.
Definition: sql_select.h:344
Query optimization plan node.
Definition: sql_select.h:580
mem_root_deque< Item * > * query_block_fields
List storing all expressions used in query block.
Definition: sql_optimizer.h:361
void init_key_dependencies()
Initialize key dependencies for join tables.
Definition: sql_optimizer.h:797
ORDER_with_src order
ORDER BY and GROUP BY lists, to transform with prepare,optimize and exec.
Definition: sql_optimizer.h:385
Wrapper for ORDER* pointer to trace origins of ORDER list.
Definition: sql_optimizer.h:95
void copy_ref_item_slice(Ref_item_array dst_arr, Ref_item_array src_arr)
Definition: sql_optimizer.h:589
int get_flags() const
Definition: sql_optimizer.h:119
Definition: item_cmpfunc.h:2521
size_t size() const
Definition: sql_array.h:95
void destroy(T *ptr)
Definition: my_alloc.h:382
int n
Definition: xcom_base.cc:442
void set_executed()
Definition: sql_optimizer.h:697
Definition: sql_optimizer.h:82
#define HA_POS_ERROR
Definition: my_base.h:1138
uint build_bitmap_for_nested_joins(mem_root_deque< TABLE_LIST *> *join_list, uint first_unused)
Assign each nested join structure a bit in nested_join_map.
Definition: sql_optimizer.cc:4656
Item_equal * find_item_equal(COND_EQUAL *cond_equal, const Item_field *item_field, bool *inherited_fl)
Find the multiple equality predicate containing a field.
Definition: sql_optimizer.cc:3378
RAII class to manage JOIN::deps_of_remaining_lateral_derived_tables.
Definition: sql_optimizer.h:1036
ORDER_with_src()
Definition: sql_optimizer.h:104
Item * substitute_for_best_equal_field(THD *thd, Item *cond, COND_EQUAL *cond_equal, JOIN_TAB **table_join_idx)
Substitute every field reference in a condition by the best equal field and eliminate all multiple eq...
Definition: sql_optimizer.cc:4404
bool is_executed() const
Definition: sql_optimizer.h:696
table_map deps_of_remaining_lateral_derived_tables
Used in some loops which scan the JOIN&#39;s tables: it is the bitmap of all tables which are dependencie...
Definition: sql_optimizer.h:230
uint64_t table_map
Definition: my_table_map.h:29
bool(*)(JOIN *) Override_executor_func
A hook that secondary storage engines can use to override the executor completely.
Definition: sql_optimizer.h:268
List< Window > m_windows
Any window definitions.
Definition: sql_optimizer.h:397
JOIN * join
Definition: sql_optimizer.h:970
Definition: opt_explain_format.h:439
Item * having_for_explain
Saved optimized HAVING for EXPLAIN.
Definition: sql_optimizer.h:448
~Deps_of_remaining_lateral_derived_tables()
Definition: sql_optimizer.h:1053
Definition: opt_explain_format.h:425
Explain_sort_clause
Enumeration of ORDER BY, GROUP BY and DISTINCT clauses for array indexing.
Definition: opt_explain_format.h:424
Switch_ref_item_slice(JOIN *join_arg, uint new_v)
Definition: sql_optimizer.h:974
virtual enum_field_types type() const =0
Definition: table.h:2540
static int flags[50]
Definition: hp_test1.cc:39
bool is_temporal_type(enum_field_types type)
Tests if field type is temporal, i.e.
Definition: field.h:394
ORDER * create_order_from_distinct(THD *thd, Ref_item_array ref_item_array, ORDER *order_list, const mem_root_deque< Item *> &fields, bool skip_aggregates, bool convert_bit_fields_to_long, bool *all_order_by_fields_used)
Create an order list that consists of all non-const fields and items.
Definition: sql_optimizer.cc:10287
RAII class to ease the temporary switching to a different slice of the ref item array.
Definition: sql_optimizer.h:969
void set_ref_item_slice(uint sliceno)
Overwrite the base slice of ref_items with the slice supplied as argument.
Definition: sql_optimizer.h:613
void clean()
Definition: sql_optimizer.h:113
Definition: lock.h:38
Temp_table_param tmp_table_param
Describes a temporary table.
Definition: sql_optimizer.h:298
Item * having_cond
Optimized HAVING clause item tree (valid for one single execution).
Definition: sql_optimizer.h:447
API for getting cost estimates for server operations that are not directly related to a table object...
Definition: opt_costmodel.h:41
Subquery_strategy
Strategy which will be used to handle this subquery: flattening to a semi-join, conversion to a deriv...
Definition: item_subselect.h:385
my_off_t ha_rows
Definition: my_base.h:1136
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:74
Definition: items.h:34
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:803
bool empty() const
Definition: sql_optimizer.h:111