Skip to content

Commit 607b14c

Browse files
committed
Add --optimizer_trace option to mysqltest
This enables optimizer_trace output for the next SQL command. Identical as if one would have done: - Store value of @@optimizer_trace - Set @optimizer_trace="enabled=on" - Run query - SELECT * from OPTIMIZER_TRACE - Restore value of @@optimizer_trace This is a great time saver when one wants to quickly check the optimizer trace for a query in a mtr test.
1 parent 3691cc1 commit 607b14c

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

client/mysqltest.cc

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static my_bool disable_info= 1;
132132
static my_bool abort_on_error= 1, opt_continue_on_error= 0;
133133
static my_bool server_initialized= 0;
134134
static my_bool is_windows= 0;
135+
static my_bool optimizer_trace_active= 0;
135136
static char **default_argv;
136137
static const char *load_default_groups[]=
137138
{ "mysqltest", "mariadb-test", "client", "client-server", "client-mariadb",
@@ -388,6 +389,7 @@ enum enum_commands {
388389
Q_MOVE_FILE, Q_REMOVE_FILES_WILDCARD, Q_SEND_EVAL,
389390
Q_ENABLE_PREPARE_WARNINGS, Q_DISABLE_PREPARE_WARNINGS,
390391
Q_RESET_CONNECTION,
392+
Q_OPTIMIZER_TRACE,
391393
Q_UNKNOWN, /* Unknown command. */
392394
Q_COMMENT, /* Comments, ignored. */
393395
Q_COMMENT_WITH_COMMAND,
@@ -498,7 +500,7 @@ const char *command_names[]=
498500
"enable_prepare_warnings",
499501
"disable_prepare_warnings",
500502
"reset_connection",
501-
503+
"optimizer_trace",
502504
0
503505
};
504506

@@ -643,6 +645,9 @@ void free_all_replace(){
643645
}
644646

645647
void var_set_int(const char* name, int value);
648+
void enable_optimizer_trace(struct st_connection *con);
649+
void display_optimizer_trace(struct st_connection *con,
650+
DYNAMIC_STRING *ds);
646651

647652

648653
class LogFile {
@@ -1541,6 +1546,8 @@ static void die(const char *fmt, ...)
15411546
{
15421547
char buff[DIE_BUFF_SIZE];
15431548
va_list args;
1549+
DBUG_ENTER("die");
1550+
15441551
va_start(args, fmt);
15451552
make_error_message(buff, sizeof(buff), fmt, args);
15461553
really_die(buff);
@@ -7841,7 +7848,7 @@ static void handle_no_active_connection(struct st_command *command,
78417848
*/
78427849

78437850
void run_query_normal(struct st_connection *cn, struct st_command *command,
7844-
int flags, char *query, size_t query_len,
7851+
int flags, const char *query, size_t query_len,
78457852
DYNAMIC_STRING *ds, DYNAMIC_STRING *ds_warnings)
78467853
{
78477854
MYSQL_RES *res= 0;
@@ -7960,6 +7967,7 @@ void run_query_normal(struct st_connection *cn, struct st_command *command,
79607967

79617968
/* If we come here the query is both executed and read successfully */
79627969
handle_no_error(command);
7970+
display_optimizer_trace(cn, ds);
79637971
revert_properties();
79647972

79657973
end:
@@ -9580,6 +9588,9 @@ int main(int argc, char **argv)
95809588
case Q_RESET_CONNECTION:
95819589
do_reset_connection();
95829590
break;
9591+
case Q_OPTIMIZER_TRACE:
9592+
enable_optimizer_trace(cur_con);
9593+
break;
95839594
case Q_SEND_SHUTDOWN:
95849595
handle_command_error(command,
95859596
mysql_shutdown(cur_con->mysql,
@@ -11238,3 +11249,90 @@ char *mysql_authentication_dialog_ask(MYSQL *mysql, int type,
1123811249

1123911250
return buf;
1124011251
}
11252+
11253+
/*
11254+
Enable optimizer trace for the next command
11255+
*/
11256+
11257+
LEX_CSTRING enable_optimizer_trace_query=
11258+
{
11259+
STRING_WITH_LEN("set @mysqltest_save_optimzer_trace=@@optimizer_trace,@@optimizer_trace=\"enabled=on\"")
11260+
};
11261+
11262+
LEX_CSTRING restore_optimizer_trace_query=
11263+
{
11264+
STRING_WITH_LEN("set @@optimizer_trace=@mysqltest_save_optimzer_trace")
11265+
};
11266+
11267+
11268+
LEX_CSTRING display_optimizer_trace_query
11269+
{
11270+
STRING_WITH_LEN("SELECT * from information_schema.optimizer_trace")
11271+
};
11272+
11273+
11274+
void enable_optimizer_trace(struct st_connection *con)
11275+
{
11276+
MYSQL *mysql= con->mysql;
11277+
my_bool save_ps_protocol_enabled= ps_protocol_enabled;
11278+
my_bool save_view_protocol_enabled= view_protocol_enabled;
11279+
DYNAMIC_STRING ds_result;
11280+
DYNAMIC_STRING ds_warnings;
11281+
struct st_command command;
11282+
DBUG_ENTER("enable_optimizer_trace");
11283+
11284+
if (!mysql)
11285+
DBUG_VOID_RETURN;
11286+
ps_protocol_enabled= view_protocol_enabled= 0;
11287+
11288+
init_dynamic_string(&ds_result, NULL, 0, 256);
11289+
init_dynamic_string(&ds_warnings, NULL, 0, 256);
11290+
bzero(&command, sizeof(command));
11291+
11292+
run_query_normal(con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
11293+
enable_optimizer_trace_query.str,
11294+
enable_optimizer_trace_query.length,
11295+
&ds_result, &ds_warnings);
11296+
dynstr_free(&ds_result);
11297+
dynstr_free(&ds_warnings);
11298+
ps_protocol_enabled= save_ps_protocol_enabled;
11299+
view_protocol_enabled= save_view_protocol_enabled;
11300+
optimizer_trace_active= 1;
11301+
DBUG_VOID_RETURN;
11302+
}
11303+
11304+
11305+
void display_optimizer_trace(struct st_connection *con,
11306+
DYNAMIC_STRING *ds)
11307+
{
11308+
my_bool save_ps_protocol_enabled= ps_protocol_enabled;
11309+
my_bool save_view_protocol_enabled= view_protocol_enabled;
11310+
DYNAMIC_STRING ds_result;
11311+
DYNAMIC_STRING ds_warnings;
11312+
struct st_command command;
11313+
DBUG_ENTER("display_optimizer_trace");
11314+
11315+
if (!optimizer_trace_active)
11316+
DBUG_VOID_RETURN;
11317+
11318+
optimizer_trace_active= 0;
11319+
ps_protocol_enabled= view_protocol_enabled= 0;
11320+
11321+
init_dynamic_string(&ds_result, NULL, 0, 256);
11322+
init_dynamic_string(&ds_warnings, NULL, 0, 256);
11323+
bzero(&command, sizeof(command));
11324+
11325+
run_query_normal(con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
11326+
display_optimizer_trace_query.str,
11327+
display_optimizer_trace_query.length,
11328+
ds, &ds_warnings);
11329+
run_query_normal(con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
11330+
restore_optimizer_trace_query.str,
11331+
restore_optimizer_trace_query.length,
11332+
ds, &ds_warnings);
11333+
dynstr_free(&ds_result);
11334+
dynstr_free(&ds_warnings);
11335+
ps_protocol_enabled= save_ps_protocol_enabled;
11336+
view_protocol_enabled= save_view_protocol_enabled;
11337+
DBUG_VOID_RETURN;
11338+
}

mysql-test/main/opt_trace.result

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ create view v2 as select a from t2;
571571
explain select * from v2 ;
572572
id select_type table type possible_keys key key_len ref rows Extra
573573
1 SIMPLE t2 ALL NULL NULL NULL NULL 10
574-
select * from information_schema.OPTIMIZER_TRACE;
575574
QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES
576575
explain select * from v2 {
577576
"steps": [
@@ -685,7 +684,6 @@ explain select * from v1 ;
685684
id select_type table type possible_keys key key_len ref rows Extra
686685
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10
687686
2 DERIVED t1 ALL NULL NULL NULL NULL 10 Using temporary; Using filesort
688-
select * from information_schema.OPTIMIZER_TRACE;
689687
QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES
690688
explain select * from v1 {
691689
"steps": [

mysql-test/main/opt_trace.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ create view v1 as select a from t1 group by b;
4848
create view v2 as select a from t2;
4949

5050
--echo # Mergeable view
51+
--optimizer_trace
5152
explain select * from v2 ;
52-
select * from information_schema.OPTIMIZER_TRACE;
5353

5454
--echo # Non-Mergeable view
55+
--optimizer_trace
5556
explain select * from v1 ;
56-
select * from information_schema.OPTIMIZER_TRACE;
5757
drop table t1,t2;
5858
drop view v1,v2;
5959

0 commit comments

Comments
 (0)