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