MySQL  8.0.27
Source Code Documentation
abstract_query_plan.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, 2021, Oracle and/or its affiliates.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation. The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 
25 #ifndef ABSTRACT_QUERY_PLAN_H_INCLUDED
26 #define ABSTRACT_QUERY_PLAN_H_INCLUDED
27 
28 #include <assert.h>
29 #include <sys/types.h>
30 
31 #include "sql/item_cmpfunc.h" // Item_equal_iterator
32 
33 class Item;
34 class Item_field;
35 class JOIN;
36 class KEY_PART_INFO;
37 class QEP_TAB;
38 struct TABLE;
39 
40 /**
41  Abstract query plan (AQP) is an interface for examining certain aspects of
42  query plans without accessing mysqld internal classes (JOIN_TAB, QEP_TAB,
43  etc.) directly.
44 
45  AQP maps join execution plans, as represented by mysqld internals, to a set
46  of facade classes. Non-join operations such as sorting and aggregation is
47  currently *not* modelled in the AQP.
48 
49  The AQP models an n-way join as a sequence of the n table access operations
50  that the MySQL server would execute as part of its nested loop join
51  execution. (Each such table access operation is a scan of a table or index,
52  or an index lookup.) For each lookup operation, it is possible to examine
53  the expression that represents each field in the key.
54 
55  A storage enging will typically use the AQP for finding sections of a join
56  execution plan that may be executed in the engine rather than in mysqld. By
57  using the AQP rather than the mysqld internals directly, the coupling between
58  the engine and mysqld is reduced.
59 
60  The AQP also provides functions which allows the storage engine
61  to change the query execution plan for the part of the join which
62  it will handle. Thus be aware that although the QEP_TAB*'s are const
63  they may be modified.
64 */
65 namespace AQP {
66 class Table_access;
67 
68 /**
69  This class represents a query plan for an n-way join, in the form a
70  sequence of n table access operations that will execute as a nested loop
71  join.
72 */
73 class Join_plan {
74  friend class Equal_set_iterator;
75  friend class Table_access;
76 
77  public:
78  explicit Join_plan(const JOIN *join);
79 
80  ~Join_plan();
81 
82  Table_access *get_table_access(uint access_no) const;
83 
84  uint get_access_count() const;
85 
86  private:
87  /**
88  Array of the QEP_TABs that are the internal representation of table
89  access operations.
90  */
91  const QEP_TAB *const m_qep_tabs;
92 
93  /** Number of table access operations. */
96 
97  const QEP_TAB *get_qep_tab(uint qep_tab_no) const;
98 
99  // No copying.
102 };
103 // class Join_plan
104 
105 /**
106  This class is an iterator for iterating over sets of fields (columns) that
107  should have the same value. For example, if the query is
108  SELECT * FROM T1, T2, T3 WHERE T1.b = T2.a AND T2.a = T3.a
109  then there would be such a set of {T1.b, T2.a, T3.a}.
110 */
112  public:
113  explicit Equal_set_iterator(const Item_equal &item_equal)
114  : m_iterator(item_equal.get_fields().begin()),
115  m_end(item_equal.get_fields().end()) {}
116 
117  const Item_field *next() {
118  if (m_iterator == m_end) {
119  return nullptr;
120  } else {
121  return &*m_iterator++;
122  }
123  }
124 
125  private:
127 };
128 // class Equal_set_iterator
129 
130 /** The type of a table access operation. */
132  /** For default initialization.*/
134  /** Value has already been fetched / determined by optimizer.*/
136  /** Do a lookup of a single primary key.*/
138  /** Do a lookup of a single unique index key.*/
140  /** Scan an ordered index with a single upper and lower bound pair.*/
142  /** Do a multi range read for a set of primary keys.*/
144  /** Do a multi range read for a set of unique index keys.*/
146  /**
147  Do a multi range read for a mix of ranges (for which there is an
148  ordered index), and either primary keys or unique index keys.
149  */
151  /** Scan a table. (No index is assumed to be used.) */
153  /** Access method will not be chosen before the execution phase.*/
155  /**
156  The access method has properties that prevents it from being pushed to a
157  storage engine.
158  */
159  AT_OTHER
160 };
161 
162 /**
163  This class represents an access operation on a table, such as a table
164  scan, or a scan or lookup via an index. A Table_access object is always
165  owned by a Join_plan object, such that the life time of the Table_access
166  object ends when the life time of the owning Join_plan object ends.
167  */
169  friend class Join_plan;
170  friend inline bool equal(const Table_access *, const Table_access *);
171 
172  public:
173  const Join_plan *get_join_plan() const;
174 
176 
177  const char *get_other_access_reason() const;
178 
179  uint get_no_of_key_fields() const;
180 
181  const Item *get_key_field(uint field_no) const;
182 
183  const KEY_PART_INFO *get_key_part_info(uint field_no) const;
184 
185  uint get_access_no() const;
186 
187  int get_index_no() const;
188 
189  TABLE *get_table() const;
190 
191  Item_equal *get_item_equal(const Item_field *field_item) const;
192 
193  void dbug_print() const;
194 
195  bool uses_join_cache() const;
196 
197  bool filesort_before_join() const;
198 
199  Item *get_condition() const;
200  void set_condition(Item *cond);
201 
202  uint get_first_inner() const;
203  uint get_last_inner() const;
204  int get_first_upper() const;
205 
206  int get_first_sj_inner() const;
207  int get_last_sj_inner() const;
208 
209  // Is member of a firstMatch sj_nest?
210  bool is_sj_firstmatch() const;
211 
212  bool is_antijoin() const;
213 
214  /**
215  Getter and setters for an opaque object for each table.
216  Used by the handler's to persist 'pushability-flags' to avoid
217  overhead by recalculating it for each ::engine_push()
218  */
219  uint get_table_properties() const;
221 
222  private:
223  /** Backref. to the Join_plan which this Table_access is part of */
225 
226  /** This operation corresponds to m_root_tab[m_tab_no].*/
228 
229  /** The type of this operation.*/
231 
232  /**
233  The reason for getting m_access_type==AT_OTHER. Used for explain extended.
234  */
235  mutable const char *m_other_access_reason;
236 
237  /** The index to use for this operation (if applicable )*/
238  mutable int m_index_no;
239 
240  /** May store an opaque property / flag */
242 
243  explicit Table_access();
244 
245  const QEP_TAB *get_qep_tab() const;
246 
247  void compute_type_and_index() const;
248 
249  /** No copying*/
252 };
253 // class Table_access
254 
255 /**
256  Get the n'th table access operation.
257  @param access_no The index of the table access operation to fetch.
258  @return The access_no'th table access operation.
259 */
261  assert(access_no < m_access_count);
262  return m_table_accesses + access_no;
263 }
264 
265 /**
266  @return The number of table access operations in the nested loop join.
267 */
269 
270 /** Get the Join_plan that this Table_access belongs to.*/
271 inline const Join_plan *Table_access::get_join_plan() const {
272  return m_join_plan;
273 }
274 
275 /** Get the type of this operation.*/
278  return m_access_type;
279 }
280 
281 /**
282  Get a description of the reason for getting access_type==AT_OTHER. To be
283  used for informational messages.
284  @return A string that should be assumed to have the same life time as the
285  Table_access object.
286 */
287 inline const char *Table_access::get_other_access_reason() const {
289  return m_other_access_reason;
290 }
291 
292 /**
293  @return The number of the index to use for this access operation (
294  or -1 for non-index operations).
295 */
296 inline int Table_access::get_index_no() const {
298 
299  return m_index_no;
300 }
301 
302 /**
303  Get the number of this Table_access within the enclosing Join_plan.
304  (This number will be in the range 0 to Join_plan::get_access_count() - 1.)
305 */
306 inline uint Table_access::get_access_no() const { return m_tab_no; }
307 
309 
311 
312 } // namespace AQP
313 // namespace AQP
314 
315 #endif
This class is an iterator for iterating over sets of fields (columns) that should have the same value...
Definition: abstract_query_plan.h:111
Equal_set_iterator(const Item_equal &item_equal)
Definition: abstract_query_plan.h:113
const Item_field * next()
Definition: abstract_query_plan.h:117
List_STL_Iterator< const Item_field > m_iterator
Definition: abstract_query_plan.h:126
List_STL_Iterator< const Item_field > m_end
Definition: abstract_query_plan.h:126
This class represents a query plan for an n-way join, in the form a sequence of n table access operat...
Definition: abstract_query_plan.h:73
~Join_plan()
Definition: abstract_query_plan.cc:69
Table_access * get_table_access(uint access_no) const
Get the n'th table access operation.
Definition: abstract_query_plan.h:260
Join_plan(const Join_plan &)
uint m_access_count
Number of table access operations.
Definition: abstract_query_plan.h:94
Table_access * m_table_accesses
Definition: abstract_query_plan.h:95
const QEP_TAB * get_qep_tab(uint qep_tab_no) const
Get the QEP_TAB of the n'th table access operation.
Definition: abstract_query_plan.cc:75
const QEP_TAB *const m_qep_tabs
Array of the QEP_TABs that are the internal representation of table access operations.
Definition: abstract_query_plan.h:91
uint get_access_count() const
Definition: abstract_query_plan.h:268
Join_plan(const JOIN *join)
Definition: abstract_query_plan.cc:46
Join_plan & operator=(const Join_plan &)
This class represents an access operation on a table, such as a table scan, or a scan or lookup via a...
Definition: abstract_query_plan.h:168
uint get_first_inner() const
Returns the first/last table in the join-nest this table is a member of.
Definition: abstract_query_plan.cc:373
void compute_type_and_index() const
Compute the access type and index (if apliccable) of this operation .
Definition: abstract_query_plan.cc:175
uint get_access_no() const
Get the number of this Table_access within the enclosing Join_plan.
Definition: abstract_query_plan.h:306
TABLE * get_table() const
Get the table that this operation accesses.
Definition: abstract_query_plan.cc:117
bool filesort_before_join() const
Check if this table will be presorted to an intermediate record storage before it is joined with its ...
Definition: abstract_query_plan.cc:356
const Join_plan * m_join_plan
Backref.
Definition: abstract_query_plan.h:224
void set_condition(Item *cond)
Definition: abstract_query_plan.cc:361
uint get_table_properties() const
Getter and setters for an opaque object for each table.
Definition: abstract_query_plan.h:308
friend bool equal(const Table_access *, const Table_access *)
const Join_plan * get_join_plan() const
Get the Join_plan that this Table_access belongs to.
Definition: abstract_query_plan.h:271
bool uses_join_cache() const
Check if the results from this operation will joined with results from the next operation using a joi...
Definition: abstract_query_plan.cc:347
int get_index_no() const
Definition: abstract_query_plan.h:296
void set_table_properties(uint)
Definition: abstract_query_plan.h:310
const Item * get_key_field(uint field_no) const
Get the field_no'th key values for this operation.
Definition: abstract_query_plan.cc:98
bool is_sj_firstmatch() const
Definition: abstract_query_plan.cc:404
uint m_tab_no
This operation corresponds to m_root_tab[m_tab_no].
Definition: abstract_query_plan.h:227
const KEY_PART_INFO * get_key_part_info(uint field_no) const
Get the field_no'th KEY_PART_INFO for this operation.
Definition: abstract_query_plan.cc:108
enum_access_type m_access_type
The type of this operation.
Definition: abstract_query_plan.h:230
bool is_antijoin() const
Definition: abstract_query_plan.cc:409
int get_last_sj_inner() const
Definition: abstract_query_plan.cc:399
const QEP_TAB * get_qep_tab() const
Get the QEP_TAB object that corresponds to this operation.
Definition: abstract_query_plan.cc:120
Table_access()
Definition: abstract_query_plan.cc:334
void dbug_print() const
Write an entry in the trace file about the contents of this object.
Definition: abstract_query_plan.cc:140
enum_access_type get_access_type() const
Get the type of this operation.
Definition: abstract_query_plan.h:276
Table_access(const Table_access &)
No copying.
int get_first_sj_inner() const
Returns the first/last table in a semi-join nest.
Definition: abstract_query_plan.cc:395
uint m_properties
May store an opaque property / flag.
Definition: abstract_query_plan.h:241
uint get_last_inner() const
Definition: abstract_query_plan.cc:378
Table_access & operator=(const Table_access &)
int m_index_no
The index to use for this operation (if applicable )
Definition: abstract_query_plan.h:238
Item * get_condition() const
Definition: abstract_query_plan.cc:360
int get_first_upper() const
Definition: abstract_query_plan.cc:383
const char * get_other_access_reason() const
Get a description of the reason for getting access_type==AT_OTHER.
Definition: abstract_query_plan.h:287
const char * m_other_access_reason
The reason for getting m_access_type==AT_OTHER.
Definition: abstract_query_plan.h:235
uint get_no_of_key_fields() const
Get the number of key values for this operation.
Definition: abstract_query_plan.cc:85
Item_equal * get_item_equal(const Item_field *field_item) const
Get the Item_equal's set relevant for the specified 'Item_field'.
Definition: abstract_query_plan.cc:125
Definition: item_cmpfunc.h:2474
Definition: item.h:4027
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_optimizer.h:125
Definition: key.h:56
Definition: sql_executor.h:256
struct Table_access_imp * Table_access
Table_access.
Definition: table_access_bits.h:97
Abstract query plan (AQP) is an interface for examining certain aspects of query plans without access...
Definition: abstract_query_plan.cc:45
enum_access_type
The type of a table access operation.
Definition: abstract_query_plan.h:131
@ AT_UNIQUE_KEY
Do a lookup of a single unique index key.
Definition: abstract_query_plan.h:139
@ AT_ORDERED_INDEX_SCAN
Scan an ordered index with a single upper and lower bound pair.
Definition: abstract_query_plan.h:141
@ AT_MULTI_PRIMARY_KEY
Do a multi range read for a set of primary keys.
Definition: abstract_query_plan.h:143
@ AT_MULTI_MIXED
Do a multi range read for a mix of ranges (for which there is an ordered index), and either primary k...
Definition: abstract_query_plan.h:150
@ AT_MULTI_UNIQUE_KEY
Do a multi range read for a set of unique index keys.
Definition: abstract_query_plan.h:145
@ AT_PRIMARY_KEY
Do a lookup of a single primary key.
Definition: abstract_query_plan.h:137
@ AT_UNDECIDED
Access method will not be chosen before the execution phase.
Definition: abstract_query_plan.h:154
@ AT_TABLE_SCAN
Scan a table.
Definition: abstract_query_plan.h:152
@ AT_FIXED
Value has already been fetched / determined by optimizer.
Definition: abstract_query_plan.h:135
@ AT_OTHER
The access method has properties that prevents it from being pushed to a storage engine.
Definition: abstract_query_plan.h:159
@ AT_VOID
For default initialization.
Definition: abstract_query_plan.h:133
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:144
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
Definition: table.h:1394
unsigned int uint
Definition: uca-dump.cc:29