MySQL 8.0.30
Source Code Documentation
opt_explain.h
Go to the documentation of this file.
1/* Copyright (c) 2011, 2022, Oracle and/or its affiliates.
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
30Single table UPDATE/DELETE commands are explained by the
31explain_single_table_modification() function.
32
33A query expression (complete SELECT query possibly including
34subqueries and unions), INSERT...SELECT and multitable UPDATE/DELETE
35commands are explained like this:
36
37(1) explain_query_expression()
38
39Is the entry point. Forwards the job to explain_query_expression().
40
41(2) explain_query_expression()
42
43Is for a Query_expression, prepares, optimizes, explains one JOIN for
44each "top-level" Query_blocks of the unit (like: all SELECTs of a
45UNION; but not subqueries), and one JOIN for the fake Query_block of
46UNION); each JOIN explain (JOIN::exec()) calls explain_query_specification()
47
48(3) explain_query_specification()
49
50Is for a single Query_block (fake or not). It needs a prepared and
51optimized JOIN, for which it builds the EXPLAIN rows. But it also
52launches the EXPLAIN process for "inner units" (==subqueries of this
53Query_block), by calling explain_query_expression() for each of them.
54*/
55
56#include "my_base.h"
57#include "my_sqlcommand.h"
58#include "my_thread_local.h"
62#include "sql/query_result.h" // Query_result_send
63#include "sql/sql_cmd.h" // Sql_cmd
65#include "sys/types.h"
66
67class Item;
68class JOIN;
69class QEP_TAB;
70class Query_block;
72class THD;
73struct AccessPath;
74struct TABLE;
75template <class T>
76class List;
77
78extern const char *join_type_str[];
79
80/** Table modification plan for JOIN-less statements (update/delete) */
82 public:
83 THD *const thd; ///< Owning thread
84 const enum_mod_type
85 mod_type; ///< Modification type - MT_INSERT/MT_UPDATE/etc
86 TABLE *table; ///< Table to modify
87
90 Item *condition{nullptr};
91 uint key; ///< Key to use
92 ha_rows limit; ///< Limit
93 bool need_tmp_table; ///< Whether tmp table needs to be used
94 bool need_sort; ///< Whether to use filesort
95 bool used_key_is_modified; ///< Whether the key used to scan is modified
96 const char *message; ///< Arbitrary message
97 bool zero_result; ///< true <=> plan will not be executed
98 ha_rows examined_rows; ///< # of rows expected to be examined in the table
99
100 Modification_plan(THD *thd_arg, enum_mod_type mt, TABLE *table_arg,
101 enum join_type type_arg, AccessPath *quick_arg,
102 Item *condition_arg, uint key_arg, ha_rows limit_arg,
103 bool need_tmp_table_arg, bool need_sort_arg,
104 bool used_key_is_modified_arg, ha_rows rows);
105
106 Modification_plan(THD *thd_arg, enum_mod_type mt, TABLE *table_arg,
107 const char *message_arg, bool zero_result_arg,
108 ha_rows rows);
109
111
112 private:
113 void register_in_thd();
114};
115
116/**
117 EXPLAIN functionality for Query_result_insert, Query_result_update and
118 Query_result_delete.
119
120 This class objects substitute Query_result_insert, Query_result_update and
121 Query_result_delete data interceptor objects to implement EXPLAIN for INSERT,
122 REPLACE and multi-table UPDATE and DELETE queries.
123 Query_result_explain class object initializes tables like Query_result_insert,
124 Query_result_update or Query_result_delete data interceptor do, but it
125 suppresses table data modification by the underlying interceptor object.
126 Thus, we can use Query_result_explain object in the context of EXPLAIN INSERT/
127 REPLACE/UPDATE/DELETE query like we use Query_result_send in the context of
128 EXPLAIN SELECT command:
129 1) in presence of lex->describe flag, pass Query_result_explain object to
130 execution function,
131 2) it calls prepare(), optimize() and start_execution() functions
132 to mark modified tables etc.
133*/
134
136 protected:
137 /**
138 Pointer to underlying Query_result_insert, Query_result_update or
139 Query_result_delete object.
140 */
142
143 public:
145 Query_result *interceptor_arg)
146 : Query_result_send(), interceptor(interceptor_arg) {
147 unit = unit_arg;
148 }
149
150 protected:
152 Query_expression *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 void cleanup(THD *thd) override {
164 interceptor->cleanup(thd);
165 }
166};
167
168bool explain_no_table(THD *explain_thd, const THD *query_thd,
169 Query_block *query_block, const char *message,
171bool explain_single_table_modification(THD *explain_thd, const THD *query_thd,
172 const Modification_plan *plan,
173 Query_block *select);
174bool explain_query(THD *explain_thd, const THD *query_thd,
175 Query_expression *unit);
176bool explain_query_specification(THD *explain_thd, const THD *query_thd,
177 Query_block *query_block,
179
181 public:
184
187 }
188
189 bool execute(THD *thd) override;
190
191 private:
192 /// connection_id in EXPLAIN FOR CONNECTION <connection_id>
194};
195
196#endif /* OPT_EXPLAIN_INCLUDED */
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: sql_list.h:433
Table modification plan for JOIN-less statements (update/delete)
Definition: opt_explain.h:81
TABLE * table
Table to modify.
Definition: opt_explain.h:86
ha_rows limit
Limit.
Definition: opt_explain.h:92
THD *const thd
Owning thread.
Definition: opt_explain.h:83
const enum_mod_type mod_type
Modification type - MT_INSERT/MT_UPDATE/etc.
Definition: opt_explain.h:85
uint key
Key to use.
Definition: opt_explain.h:91
bool need_sort
Whether to use filesort.
Definition: opt_explain.h:94
Modification_plan(THD *thd_arg, enum_mod_type mt, TABLE *table_arg, enum join_type type_arg, AccessPath *quick_arg, Item *condition_arg, 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:2483
Item * condition
Definition: opt_explain.h:90
bool zero_result
true <=> plan will not be executed
Definition: opt_explain.h:97
void register_in_thd()
Definition: opt_explain.cc:2449
enum join_type type
Definition: opt_explain.h:88
bool used_key_is_modified
Whether the key used to scan is modified.
Definition: opt_explain.h:95
ha_rows examined_rows
Definition: opt_explain.h:98
~Modification_plan()
Definition: opt_explain.cc:2542
bool need_tmp_table
Whether tmp table needs to be used.
Definition: opt_explain.h:93
const char * message
Arbitrary message.
Definition: opt_explain.h:96
AccessPath * range_scan
Definition: opt_explain.h:89
Definition: sql_executor.h:259
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1124
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:629
EXPLAIN functionality for Query_result_insert, Query_result_update and Query_result_delete.
Definition: opt_explain.h:135
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: opt_explain.h:151
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: opt_explain.h:157
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: opt_explain.h:162
Query_result_explain(Query_expression *unit_arg, Query_result *interceptor_arg)
Definition: opt_explain.h:144
Query_result * interceptor
Pointer to underlying Query_result_insert, Query_result_update or Query_result_delete object.
Definition: opt_explain.h:141
Definition: query_result.h:175
void cleanup(THD *) override
Cleanup after this execution.
Definition: query_result.h:191
Definition: query_result.h:51
virtual bool prepare(THD *, const mem_root_deque< Item * > &, Query_expression *u)
Perform preparation specific to the query expression or DML statement.
Definition: query_result.h:92
virtual void cleanup(THD *)
Cleanup after this execution.
Definition: query_result.h:147
Query_expression * unit
Definition: query_result.h:53
virtual bool start_execution(THD *)
Prepare for execution of the query expression or DML statement.
Definition: query_result.h:107
Definition: opt_explain.h:180
my_thread_id m_thread_id
connection_id in EXPLAIN FOR CONNECTION <connection_id>
Definition: opt_explain.h:193
bool execute(THD *thd) override
Entry point for EXPLAIN CONNECTION: locates the connection by its ID, takes proper locks,...
Definition: opt_explain.cc:2342
Sql_cmd_explain_other_thread(my_thread_id thread_id)
Definition: opt_explain.h:182
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: opt_explain.h:185
Representation of an SQL command.
Definition: sql_cmd.h:64
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1139
enum_sql_command
Definition: my_sqlcommand.h:45
@ SQLCOM_EXPLAIN_OTHER
Definition: my_sqlcommand.h:181
static my_thread_id thread_id
Definition: my_thr_init.cc:62
uint32 my_thread_id
Definition: my_thread_local.h:33
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2859
const char * join_type_str[]
Definition: opt_explain.cc:118
bool explain_no_table(THD *explain_thd, const THD *query_thd, Query_block *query_block, const char *message, enum_parsing_context ctx)
Send a message as an "extra" column value.
Definition: opt_explain.cc:1782
bool explain_query_specification(THD *explain_thd, const THD *query_thd, Query_block *query_block, enum_parsing_context ctx)
Explain query_block's join.
Definition: opt_explain.cc:1938
bool explain_query(THD *explain_thd, const THD *query_thd, Query_expression *unit)
EXPLAIN handling for SELECT, INSERT/REPLACE SELECT, and multi-table UPDATE/DELETE queries.
Definition: opt_explain.cc:2159
bool explain_single_table_modification(THD *explain_thd, const THD *query_thd, const Modification_plan *plan, Query_block *select)
EXPLAIN handling for single-table INSERT VALUES, UPDATE, and DELETE queries.
Definition: opt_explain.cc:1838
EXPLAIN FORMAT=<format> <command>.
enum_mod_type
Definition: opt_explain_format.h:134
enum_parsing_context
Names for different query parse tree parts.
Definition: parse_tree_node_base.h:55
Representation of an SQL command.
Common types of the Optimizer, used by optimization and execution.
join_type
Definition: sql_opt_exec_shared.h:177
@ JT_UNKNOWN
Definition: sql_opt_exec_shared.h:179
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:188
Definition: table.h:1394
unsigned int uint
Definition: uca-dump.cc:29