Skip to content

Commit 9cb5514

Browse files
author
Varun Gupta
committed
Minor cleanup in the optimizer trace code.
More test coverage added for the optimizer trace.
1 parent 7d2138d commit 9cb5514

17 files changed

+3042
-474
lines changed

mysql-test/main/opt_trace.result

Lines changed: 2708 additions & 195 deletions
Large diffs are not rendered by default.

mysql-test/main/opt_trace.test

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ set optimizer_trace='enabled=off';
323323
--echo # MDEV-18528: Optimizer trace support for multi-table UPDATE and DELETE
324324
--echo #
325325

326+
set optimizer_trace=1;
326327
create table ten(a int);
327328
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
328329
create table t0 (a int, b int);
@@ -333,3 +334,43 @@ insert into t1 select * from t0;
333334
explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3;
334335
select * from information_schema.optimizer_trace;
335336
drop table ten,t0,t1;
337+
set optimizer_trace='enabled=off';
338+
339+
--echo #
340+
--echo # Merged to Materialized for derived tables
341+
--echo #
342+
343+
set optimizer_trace=1;
344+
create table t1 (a int);
345+
insert into t1 values (1),(2),(3);
346+
explain select * from (select rand() from t1)q;
347+
select * from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
348+
drop table t1;
349+
set optimizer_trace='enabled=off';
350+
351+
--echo #
352+
--echo # Semi-join nest
353+
--echo #
354+
355+
set optimizer_trace=1;
356+
create table t1 (a int);
357+
insert into t1 values (1),(2),(3);
358+
create table t2(a int);
359+
insert into t2 values (1),(2),(3),(1),(2),(3),(1),(2),(3);
360+
set @save_optimizer_switch= @@optimizer_switch;
361+
explain select * from t1 where a in (select t_inner_1.a from t1 t_inner_1, t1 t_inner_2);
362+
select * from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
363+
364+
--echo # with Firstmatch, mostly for tracing fix_semijoin_strategies_for_picked_join_order
365+
366+
set optimizer_switch='materialization=off';
367+
explain select * from t1 t_outer_1,t2 t_outer_2 where t_outer_1.a in (select t_inner_1.a from t2 t_inner_2, t1 t_inner_1) and
368+
t_outer_2.a in (select t_inner_3.a from t2 t_inner_3, t1 t_inner_4);
369+
select * from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
370+
set optimizer_switch='materialization=on';
371+
explain select * from t1 t_outer_1,t2 t_outer_2 where t_outer_1.a in (select t_inner_1.a from t2 t_inner_2, t1 t_inner_1) and
372+
t_outer_2.a in (select t_inner_3.a from t2 t_inner_3, t1 t_inner_4);
373+
select * from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
374+
set @@optimizer_switch= @save_optimizer_switch;
375+
drop table t1,t2;
376+
set optimizer_trace='enabled=off';

mysql-test/main/opt_trace_index_merge.result

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ explain select * from t1 where a=1 or b=1 {
2424
"select_id": 1,
2525
"steps": [
2626
{
27-
"expanded_query": "select `t1`.`a` AS `a`,`t1`.`b` AS `b`,`t1`.`c` AS `c`,`t1`.`filler` AS `filler` from `t1` where `t1`.`a` = 1 or `t1`.`b` = 1"
27+
"expanded_query": "select t1.a AS a,t1.b AS b,t1.c AS c,t1.filler AS filler from t1 where t1.a = 1 or t1.b = 1"
2828
}
2929
]
3030
}
@@ -194,15 +194,11 @@ explain select * from t1 where a=1 or b=1 {
194194
},
195195
{
196196
"selectivity_for_indexes": [],
197-
"selectivity_for_columns": []
197+
"selectivity_for_columns": [],
198+
"cond_selectivity": 0.002
198199
}
199200
]
200201
},
201-
{
202-
"execution_plan_for_potential_materialization": {
203-
"steps": []
204-
}
205-
},
206202
{
207203
"considered_execution_plans": [
208204
{

mysql-test/main/opt_trace_index_merge_innodb.result

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
3232
"select_id": 1,
3333
"steps": [
3434
{
35-
"expanded_query": "select `t1`.`pk1` AS `pk1`,`t1`.`pk2` AS `pk2`,`t1`.`key1` AS `key1`,`t1`.`key2` AS `key2` from `t1` where `t1`.`pk1` <> 0 and `t1`.`key1` = 1"
35+
"expanded_query": "select t1.pk1 AS pk1,t1.pk2 AS pk2,t1.key1 AS key1,t1.key2 AS key2 from t1 where t1.pk1 <> 0 and t1.key1 = 1"
3636
}
3737
]
3838
}
@@ -183,15 +183,11 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
183183
"selectivity_from_index": 0.001
184184
}
185185
],
186-
"selectivity_for_columns": []
186+
"selectivity_for_columns": [],
187+
"cond_selectivity": 0.001
187188
}
188189
]
189190
},
190-
{
191-
"execution_plan_for_potential_materialization": {
192-
"steps": []
193-
}
194-
},
195191
{
196192
"considered_execution_plans": [
197193
{

mysql-test/main/opt_trace_security.result

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ select * from db1.t1 {
5555
"select_id": 1,
5656
"steps": [
5757
{
58-
"expanded_query": "select `db1`.`t1`.`a` AS `a` from `t1`"
58+
"expanded_query": "select db1.t1.a AS a from t1"
5959
}
6060
]
6161
}
@@ -85,11 +85,6 @@ select * from db1.t1 {
8585
}
8686
]
8787
},
88-
{
89-
"execution_plan_for_potential_materialization": {
90-
"steps": []
91-
}
92-
},
9388
{
9489
"considered_execution_plans": [
9590
{
@@ -157,21 +152,21 @@ select * from db1.v1 {
157152
"view": {
158153
"table": "v1",
159154
"select_id": 2,
160-
"merged": true
155+
"algorithm": "merged"
161156
}
162157
},
163158
{
164159
"join_preparation": {
165160
"select_id": 2,
166161
"steps": [
167162
{
168-
"expanded_query": "/* select#2 */ select `db1`.`t1`.`a` AS `a` from `t1`"
163+
"expanded_query": "/* select#2 */ select db1.t1.a AS a from t1"
169164
}
170165
]
171166
}
172167
},
173168
{
174-
"expanded_query": "/* select#1 */ select `db1`.`t1`.`a` AS `a` from `v1`"
169+
"expanded_query": "/* select#1 */ select db1.t1.a AS a from v1"
175170
}
176171
]
177172
}
@@ -201,11 +196,6 @@ select * from db1.v1 {
201196
}
202197
]
203198
},
204-
{
205-
"execution_plan_for_potential_materialization": {
206-
"steps": []
207-
}
208-
},
209199
{
210200
"considered_execution_plans": [
211201
{

sql/my_json_writer.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ void Json_writer::add_str(const String &str)
219219
add_str(str.ptr(), str.length());
220220
}
221221

222-
Json_writer_object::Json_writer_object(THD *thd) :
222+
Json_writer_object::Json_writer_object(THD *thd) :
223223
Json_writer_struct(thd)
224224
{
225225
if (my_writer)
226226
my_writer->start_object();
227227
}
228228

229-
Json_writer_object::Json_writer_object(THD* thd, const char *str)
230-
: Json_writer_struct(thd)
229+
Json_writer_object::Json_writer_object(THD* thd, const char *str) :
230+
Json_writer_struct(thd)
231231
{
232232
if (my_writer)
233233
my_writer->add_member(str).start_object();
@@ -247,8 +247,8 @@ Json_writer_array::Json_writer_array(THD *thd) :
247247
my_writer->start_array();
248248
}
249249

250-
Json_writer_array::Json_writer_array(THD *thd, const char *str)
251-
:Json_writer_struct(thd)
250+
Json_writer_array::Json_writer_array(THD *thd, const char *str) :
251+
Json_writer_struct(thd)
252252
{
253253
if (my_writer)
254254
my_writer->add_member(str).start_array();
@@ -263,6 +263,16 @@ Json_writer_array::~Json_writer_array()
263263
}
264264
}
265265

266+
Json_writer_temp_disable::Json_writer_temp_disable(THD *thd_arg)
267+
{
268+
thd= thd_arg;
269+
thd->opt_trace.disable_tracing_if_required();
270+
}
271+
Json_writer_temp_disable::~Json_writer_temp_disable()
272+
{
273+
thd->opt_trace.enable_tracing_if_required();
274+
}
275+
266276
bool Single_line_formatting_helper::on_add_member(const char *name)
267277
{
268278
DBUG_ASSERT(state== INACTIVE || state == DISABLED);

sql/my_json_writer.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,11 @@ class Json_writer
215215
*/
216216
void set_size_limit(size_t mem_size) { output.set_size_limit(mem_size); }
217217

218-
// psergey: return how many bytes would be required to store everything
219218
size_t get_truncated_bytes() { return output.get_truncated_bytes(); }
220219

221220
Json_writer() :
222221
indent_level(0), document_start(true), element_started(false),
223-
first_child(true), allowed_mem_size(0)
222+
first_child(true)
224223
{
225224
fmt_helper.init(this);
226225
}
@@ -235,12 +234,6 @@ class Json_writer
235234
bool element_started;
236235
bool first_child;
237236

238-
/*
239-
True when we are using the optimizer trace
240-
FALSE otherwise
241-
*/
242-
size_t allowed_mem_size;
243-
244237
Single_line_formatting_helper fmt_helper;
245238

246239
void append_indent();
@@ -566,6 +559,17 @@ class Json_writer_array : public Json_writer_struct
566559
~Json_writer_array();
567560
};
568561

562+
/*
563+
RAII-based class to disable writing into the JSON document
564+
*/
565+
566+
class Json_writer_temp_disable
567+
{
568+
public:
569+
Json_writer_temp_disable(THD *thd_arg);
570+
~Json_writer_temp_disable();
571+
THD *thd;
572+
};
569573

570574
/*
571575
RAII-based helper class to detect incorrect use of Json_writer.

0 commit comments

Comments
 (0)