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