MySQL  8.0.19
Source Code Documentation
opt_explain.h
Go to the documentation of this file.
1 /* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef OPT_EXPLAIN_INCLUDED
24 #define OPT_EXPLAIN_INCLUDED
25 
26 /**
27  @file sql/opt_explain.h
28  EXPLAIN @<command@>.
29 
30 Single table UPDATE/DELETE commands are explained by the
31 explain_single_table_modification() function.
32 
33 A query expression (complete SELECT query possibly including
34 subqueries and unions), INSERT...SELECT and multitable UPDATE/DELETE
35 commands are explained like this:
36 
37 (1) explain_query_expression()
38 
39 Is the entry point. Forwards the job to explain_unit().
40 
41 (2) explain_unit()
42 
43 Is for a SELECT_LEX_UNIT, prepares, optimizes, explains one JOIN for
44 each "top-level" SELECT_LEXs of the unit (like: all SELECTs of a
45 UNION; but not subqueries), and one JOIN for the fake SELECT_LEX of
46 UNION); each JOIN explain (JOIN::exec()) calls explain_query_specification()
47 
48 (3) explain_query_specification()
49 
50 Is for a single SELECT_LEX (fake or not). It needs a prepared and
51 optimized JOIN, for which it builds the EXPLAIN rows. But it also
52 launches the EXPLAIN process for "inner units" (==subqueries of this
53 SELECT_LEX), by calling explain_unit() for each of them.
54 */
55 
56 #include <string>
57 #include <vector>
58 
59 #include "my_base.h"
60 #include "my_sqlcommand.h"
61 #include "my_thread_local.h"
62 #include "sql/opt_explain_format.h"
64 #include "sql/query_result.h" // Query_result_send
65 #include "sql/row_iterator.h"
66 #include "sql/sql_cmd.h" // Sql_cmd
67 #include "sql/sql_lex.h"
68 #include "sys/types.h"
69 
70 #include <functional>
71 #include <string>
72 
73 class Item;
74 class JOIN;
75 class QEP_TAB;
76 class SELECT_LEX;
77 class SELECT_LEX_UNIT;
78 class THD;
79 struct TABLE;
80 template <class T>
81 class List;
82 
83 extern const char *join_type_str[];
84 
85 /** Table modification plan for JOIN-less statements (update/delete) */
87  public:
88  THD *const thd; ///< Owning thread
89  const enum_mod_type
90  mod_type; ///< Modification type - MT_INSERT/MT_UPDATE/etc
91  TABLE *table; ///< Table to modify
92 
93  QEP_TAB *tab; ///< QUICK access method + WHERE clause
94  uint key; ///< Key to use
95  ha_rows limit; ///< Limit
96  bool need_tmp_table; ///< Whether tmp table needs to be used
97  bool need_sort; ///< Whether to use filesort
98  bool used_key_is_modified; ///< Whether the key used to scan is modified
99  const char *message; ///< Arbitrary message
100  bool zero_result; ///< true <=> plan will not be executed
101  ha_rows examined_rows; ///< # of rows expected to be examined in the table
102 
103  Modification_plan(THD *thd_arg, enum_mod_type mt, QEP_TAB *qep_tab,
104  uint key_arg, ha_rows limit_arg, bool need_tmp_table_arg,
105  bool need_sort_arg, bool used_key_is_modified_arg,
106  ha_rows rows);
107 
108  Modification_plan(THD *thd_arg, enum_mod_type mt, TABLE *table_arg,
109  const char *message_arg, bool zero_result_arg,
110  ha_rows rows);
111 
113 
114  private:
115  void register_in_thd();
116 };
117 
118 /**
119  EXPLAIN functionality for Query_result_insert, Query_result_update and
120  Query_result_delete.
121 
122  This class objects substitute Query_result_insert, Query_result_update and
123  Query_result_delete data interceptor objects to implement EXPLAIN for INSERT,
124  REPLACE and multi-table UPDATE and DELETE queries.
125  Query_result_explain class object initializes tables like Query_result_insert,
126  Query_result_update or Query_result_delete data interceptor do, but it
127  suppresses table data modification by the underlying interceptor object.
128  Thus, we can use Query_result_explain object in the context of EXPLAIN INSERT/
129  REPLACE/UPDATE/DELETE query like we use Query_result_send in the context of
130  EXPLAIN SELECT command:
131  1) in presence of lex->describe flag, pass Query_result_explain object to
132  execution function,
133  2) it calls prepare(), optimize() and start_execution() functions
134  to mark modified tables etc.
135 */
136 
138  protected:
139  /**
140  Pointer to underlying Query_result_insert, Query_result_update or
141  Query_result_delete object.
142  */
144 
145  public:
146  Query_result_explain(SELECT_LEX_UNIT *unit_arg, Query_result *interceptor_arg)
147  : Query_result_send(), interceptor(interceptor_arg) {
148  unit = unit_arg;
149  }
150 
151  protected:
152  bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override {
153  return Query_result_send::prepare(thd, list, u) ||
154  interceptor->prepare(thd, list, u);
155  }
156 
157  bool start_execution(THD *thd) override {
160  }
161 
162  bool optimize() override {
164  }
165 
166  void cleanup(THD *thd) override {
168  interceptor->cleanup(thd);
169  }
170 };
171 
172 bool explain_no_table(THD *explain_thd, const THD *query_thd,
173  SELECT_LEX *select_lex, const char *message,
175 bool explain_single_table_modification(THD *explain_thd, const THD *query_thd,
176  const Modification_plan *plan,
177  SELECT_LEX *select);
178 bool explain_query(THD *explain_thd, const THD *query_thd,
179  SELECT_LEX_UNIT *unit);
180 bool explain_query_specification(THD *explain_thd, const THD *query_thd,
181  SELECT_LEX *select_lex,
183 
185  public:
187  : m_thread_id(thread_id) {}
188 
190  return SQLCOM_EXPLAIN_OTHER;
191  }
192 
193  bool execute(THD *thd) override;
194 
195  private:
196  /// connection_id in EXPLAIN FOR CONNECTION <connection_id>
198 };
199 
200 // Print out an iterator and all of its children (if any) in a tree.
201 // "level" is the current indenting level, as this is called recursively.
202 std::string PrintQueryPlan(int level, RowIterator *iterator);
203 
204 // For each subselect within the given item, call the given functor
205 // with its SELECT number, dependent/cacheable status and an iterator
206 // (or nullptr if none; this may happen if the query is not executable
207 // by the iterator executor).
208 void ForEachSubselect(
209  Item *parent_item,
210  const std::function<void(int select_number, bool is_dependent,
211  bool is_cacheable, RowIterator *iterator)>
212  &callback);
213 
214 // For the given join, return a list of pseudo-children corresponding to
215 // subselects in the SELECT list (if any).
216 std::vector<RowIterator::Child> GetIteratorsFromSelectList(JOIN *join);
217 
218 #endif /* OPT_EXPLAIN_INCLUDED */
Query_result_explain::optimize
bool optimize() override
Optimize the result processing of a query expression, applicable to data change operation (not simple...
Definition: opt_explain.h:162
Modification_plan::key
uint key
Key to use.
Definition: opt_explain.h:94
Item
Definition: item.h:665
THD
Definition: sql_class.h:764
Modification_plan::limit
ha_rows limit
Limit.
Definition: opt_explain.h:95
join_type_str
const char * join_type_str[]
Definition: opt_explain.cc:111
my_base.h
ForEachSubselect
void ForEachSubselect(Item *parent_item, const std::function< void(int select_number, bool is_dependent, bool is_cacheable, RowIterator *iterator)> &callback)
explain_query
bool explain_query(THD *explain_thd, const THD *query_thd, SELECT_LEX_UNIT *unit)
EXPLAIN handling for SELECT, INSERT/REPLACE SELECT, and multi-table UPDATE/DELETE queries.
Definition: opt_explain.cc:2203
thread_id
static my_thread_id thread_id
Definition: my_thr_init.cc:62
Modification_plan::mod_type
const enum_mod_type mod_type
Modification type - MT_INSERT/MT_UPDATE/etc.
Definition: opt_explain.h:90
enum_parsing_context
enum_parsing_context
Names for different query parse tree parts.
Definition: parse_tree_node_base.h:55
Modification_plan::examined_rows
ha_rows examined_rows
Definition: opt_explain.h:101
sql_lex.h
Modification_plan
Table modification plan for JOIN-less statements (update/delete)
Definition: opt_explain.h:86
my_sqlcommand.h
row_iterator.h
Query_result
Definition: query_result.h:48
Query_result::prepare
virtual bool prepare(THD *, List< Item > &, SELECT_LEX_UNIT *u)
Perform preparation specific to the query expression or DML statement.
Definition: query_result.h:89
Sql_cmd_explain_other_thread::sql_command_code
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: opt_explain.h:189
TABLE
Definition: table.h:1305
QEP_TAB
Definition: sql_executor.h:417
Modification_plan::need_sort
bool need_sort
Whether to use filesort.
Definition: opt_explain.h:97
explain_query_specification
bool explain_query_specification(THD *explain_thd, const THD *query_thd, SELECT_LEX *select_lex, enum_parsing_context ctx)
Explain select_lex's join.
Definition: opt_explain.cc:1917
Query_result_send::cleanup
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:203
query_result.h
Modification_plan::~Modification_plan
~Modification_plan()
Definition: opt_explain.cc:2579
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
Query_result::unit
SELECT_LEX_UNIT * unit
Definition: query_result.h:50
Modification_plan::tab
QEP_TAB * tab
QUICK access method + WHERE clause.
Definition: opt_explain.h:93
SELECT_LEX_UNIT
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:569
sql_cmd.h
Modification_plan::Modification_plan
Modification_plan(THD *thd_arg, enum_mod_type mt, QEP_TAB *qep_tab, uint key_arg, ha_rows limit_arg, bool need_tmp_table_arg, bool need_sort_arg, bool used_key_is_modified_arg, ha_rows rows)
Modification_plan's constructor, to represent that we will use an access method on the table.
Definition: opt_explain.cc:2520
Query_result::cleanup
virtual void cleanup(THD *)
Cleanup after this execution.
Definition: query_result.h:151
Query_result_send
Definition: query_result.h:187
JOIN
Definition: sql_optimizer.h:174
Modification_plan::register_in_thd
void register_in_thd()
Definition: opt_explain.cc:2489
opt_explain_format.h
Query_result_explain::interceptor
Query_result * interceptor
Pointer to underlying Query_result_insert, Query_result_update or Query_result_delete object.
Definition: opt_explain.h:143
Modification_plan::need_tmp_table
bool need_tmp_table
Whether tmp table needs to be used.
Definition: opt_explain.h:96
enum_sql_command
enum_sql_command
Definition: my_sqlcommand.h:45
Sql_cmd
Representation of an SQL command.
Definition: sql_cmd.h:63
RowIterator
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:61
Query_result_explain::prepare
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Perform preparation specific to the query expression or DML statement.
Definition: opt_explain.h:152
uint
unsigned int uint
Definition: uca-dump.cc:29
list
class udf_list * list
enum_mod_type
enum_mod_type
Definition: opt_explain_format.h:132
Query_result::start_execution
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:111
SQLCOM_EXPLAIN_OTHER
@ SQLCOM_EXPLAIN_OTHER
Definition: my_sqlcommand.h:181
Sql_cmd_explain_other_thread::m_thread_id
my_thread_id m_thread_id
connection_id in EXPLAIN FOR CONNECTION <connection_id>
Definition: opt_explain.h:197
Modification_plan::zero_result
bool zero_result
true <=> plan will not be executed
Definition: opt_explain.h:100
Query_result_explain::Query_result_explain
Query_result_explain(SELECT_LEX_UNIT *unit_arg, Query_result *interceptor_arg)
Definition: opt_explain.h:146
Modification_plan::table
TABLE * table
Table to modify.
Definition: opt_explain.h:91
my_thread_id
uint32 my_thread_id
Definition: my_thread_local.h:33
Modification_plan::used_key_is_modified
bool used_key_is_modified
Whether the key used to scan is modified.
Definition: opt_explain.h:98
Modification_plan::thd
THD *const thd
Owning thread.
Definition: opt_explain.h:88
PrintQueryPlan
std::string PrintQueryPlan(int level, RowIterator *iterator)
Definition: opt_explain.cc:2021
Modification_plan::message
const char * message
Arbitrary message.
Definition: opt_explain.h:99
Sql_cmd_explain_other_thread::Sql_cmd_explain_other_thread
Sql_cmd_explain_other_thread(my_thread_id thread_id)
Definition: opt_explain.h:186
Sql_cmd_explain_other_thread
Definition: opt_explain.h:184
Query_result::optimize
virtual bool optimize()
Optimize the result processing of a query expression, applicable to data change operation (not simple...
Definition: query_result.h:100
parse_tree_node_base.h
Query_result_explain::start_execution
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: opt_explain.h:157
List
Definition: aggregate_check.h:523
ha_rows
my_off_t ha_rows
Definition: my_base.h:1132
Query_result_explain
EXPLAIN functionality for Query_result_insert, Query_result_update and Query_result_delete.
Definition: opt_explain.h:137
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
final
#define final(a, b, c)
Definition: hash.c:109
GetIteratorsFromSelectList
std::vector< RowIterator::Child > GetIteratorsFromSelectList(JOIN *join)
Definition: opt_explain.cc:2635
Query_result_explain::cleanup
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: opt_explain.h:166
my_thread_local.h
explain_single_table_modification
bool explain_single_table_modification(THD *explain_thd, const THD *query_thd, const Modification_plan *plan, SELECT_LEX *select)
EXPLAIN handling for single-table UPDATE and DELETE queries.
Definition: opt_explain.cc:1826
explain_no_table
bool explain_no_table(THD *explain_thd, const THD *query_thd, SELECT_LEX *select_lex, const char *message, enum_parsing_context ctx)
Send a message as an "extra" column value.
Definition: opt_explain.cc:1771
Sql_cmd_explain_other_thread::execute
bool execute(THD *thd) override
Entry point for EXPLAIN CONNECTION: locates the connection by its ID, takes proper locks,...
Definition: opt_explain.cc:2376