Commit 607b14c4 authored by Monty's avatar Monty

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.
parent 3691cc15
...@@ -132,6 +132,7 @@ static my_bool disable_info= 1; ...@@ -132,6 +132,7 @@ static my_bool disable_info= 1;
static my_bool abort_on_error= 1, opt_continue_on_error= 0; static my_bool abort_on_error= 1, opt_continue_on_error= 0;
static my_bool server_initialized= 0; static my_bool server_initialized= 0;
static my_bool is_windows= 0; static my_bool is_windows= 0;
static my_bool optimizer_trace_active= 0;
static char **default_argv; static char **default_argv;
static const char *load_default_groups[]= static const char *load_default_groups[]=
{ "mysqltest", "mariadb-test", "client", "client-server", "client-mariadb", { "mysqltest", "mariadb-test", "client", "client-server", "client-mariadb",
...@@ -388,6 +389,7 @@ enum enum_commands { ...@@ -388,6 +389,7 @@ enum enum_commands {
Q_MOVE_FILE, Q_REMOVE_FILES_WILDCARD, Q_SEND_EVAL, Q_MOVE_FILE, Q_REMOVE_FILES_WILDCARD, Q_SEND_EVAL,
Q_ENABLE_PREPARE_WARNINGS, Q_DISABLE_PREPARE_WARNINGS, Q_ENABLE_PREPARE_WARNINGS, Q_DISABLE_PREPARE_WARNINGS,
Q_RESET_CONNECTION, Q_RESET_CONNECTION,
Q_OPTIMIZER_TRACE,
Q_UNKNOWN, /* Unknown command. */ Q_UNKNOWN, /* Unknown command. */
Q_COMMENT, /* Comments, ignored. */ Q_COMMENT, /* Comments, ignored. */
Q_COMMENT_WITH_COMMAND, Q_COMMENT_WITH_COMMAND,
...@@ -498,7 +500,7 @@ const char *command_names[]= ...@@ -498,7 +500,7 @@ const char *command_names[]=
"enable_prepare_warnings", "enable_prepare_warnings",
"disable_prepare_warnings", "disable_prepare_warnings",
"reset_connection", "reset_connection",
"optimizer_trace",
0 0
}; };
...@@ -643,6 +645,9 @@ void free_all_replace(){ ...@@ -643,6 +645,9 @@ void free_all_replace(){
} }
void var_set_int(const char* name, int value); void var_set_int(const char* name, int value);
void enable_optimizer_trace(struct st_connection *con);
void display_optimizer_trace(struct st_connection *con,
DYNAMIC_STRING *ds);
class LogFile { class LogFile {
...@@ -1541,6 +1546,8 @@ static void die(const char *fmt, ...) ...@@ -1541,6 +1546,8 @@ static void die(const char *fmt, ...)
{ {
char buff[DIE_BUFF_SIZE]; char buff[DIE_BUFF_SIZE];
va_list args; va_list args;
DBUG_ENTER("die");
va_start(args, fmt); va_start(args, fmt);
make_error_message(buff, sizeof(buff), fmt, args); make_error_message(buff, sizeof(buff), fmt, args);
really_die(buff); really_die(buff);
...@@ -7841,7 +7848,7 @@ static void handle_no_active_connection(struct st_command *command, ...@@ -7841,7 +7848,7 @@ static void handle_no_active_connection(struct st_command *command,
*/ */
void run_query_normal(struct st_connection *cn, struct st_command *command, void run_query_normal(struct st_connection *cn, struct st_command *command,
int flags, char *query, size_t query_len, int flags, const char *query, size_t query_len,
DYNAMIC_STRING *ds, DYNAMIC_STRING *ds_warnings) DYNAMIC_STRING *ds, DYNAMIC_STRING *ds_warnings)
{ {
MYSQL_RES *res= 0; MYSQL_RES *res= 0;
...@@ -7960,6 +7967,7 @@ void run_query_normal(struct st_connection *cn, struct st_command *command, ...@@ -7960,6 +7967,7 @@ void run_query_normal(struct st_connection *cn, struct st_command *command,
/* If we come here the query is both executed and read successfully */ /* If we come here the query is both executed and read successfully */
handle_no_error(command); handle_no_error(command);
display_optimizer_trace(cn, ds);
revert_properties(); revert_properties();
end: end:
...@@ -9580,6 +9588,9 @@ int main(int argc, char **argv) ...@@ -9580,6 +9588,9 @@ int main(int argc, char **argv)
case Q_RESET_CONNECTION: case Q_RESET_CONNECTION:
do_reset_connection(); do_reset_connection();
break; break;
case Q_OPTIMIZER_TRACE:
enable_optimizer_trace(cur_con);
break;
case Q_SEND_SHUTDOWN: case Q_SEND_SHUTDOWN:
handle_command_error(command, handle_command_error(command,
mysql_shutdown(cur_con->mysql, mysql_shutdown(cur_con->mysql,
...@@ -11238,3 +11249,90 @@ char *mysql_authentication_dialog_ask(MYSQL *mysql, int type, ...@@ -11238,3 +11249,90 @@ char *mysql_authentication_dialog_ask(MYSQL *mysql, int type,
return buf; return buf;
} }
/*
Enable optimizer trace for the next command
*/
LEX_CSTRING enable_optimizer_trace_query=
{
STRING_WITH_LEN("set @mysqltest_save_optimzer_trace=@@optimizer_trace,@@optimizer_trace=\"enabled=on\"")
};
LEX_CSTRING restore_optimizer_trace_query=
{
STRING_WITH_LEN("set @@optimizer_trace=@mysqltest_save_optimzer_trace")
};
LEX_CSTRING display_optimizer_trace_query
{
STRING_WITH_LEN("SELECT * from information_schema.optimizer_trace")
};
void enable_optimizer_trace(struct st_connection *con)
{
MYSQL *mysql= con->mysql;
my_bool save_ps_protocol_enabled= ps_protocol_enabled;
my_bool save_view_protocol_enabled= view_protocol_enabled;
DYNAMIC_STRING ds_result;
DYNAMIC_STRING ds_warnings;
struct st_command command;
DBUG_ENTER("enable_optimizer_trace");
if (!mysql)
DBUG_VOID_RETURN;
ps_protocol_enabled= view_protocol_enabled= 0;
init_dynamic_string(&ds_result, NULL, 0, 256);
init_dynamic_string(&ds_warnings, NULL, 0, 256);
bzero(&command, sizeof(command));
run_query_normal(con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
enable_optimizer_trace_query.str,
enable_optimizer_trace_query.length,
&ds_result, &ds_warnings);
dynstr_free(&ds_result);
dynstr_free(&ds_warnings);
ps_protocol_enabled= save_ps_protocol_enabled;
view_protocol_enabled= save_view_protocol_enabled;
optimizer_trace_active= 1;
DBUG_VOID_RETURN;
}
void display_optimizer_trace(struct st_connection *con,
DYNAMIC_STRING *ds)
{
my_bool save_ps_protocol_enabled= ps_protocol_enabled;
my_bool save_view_protocol_enabled= view_protocol_enabled;
DYNAMIC_STRING ds_result;
DYNAMIC_STRING ds_warnings;
struct st_command command;
DBUG_ENTER("display_optimizer_trace");
if (!optimizer_trace_active)
DBUG_VOID_RETURN;
optimizer_trace_active= 0;
ps_protocol_enabled= view_protocol_enabled= 0;
init_dynamic_string(&ds_result, NULL, 0, 256);
init_dynamic_string(&ds_warnings, NULL, 0, 256);
bzero(&command, sizeof(command));
run_query_normal(con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
display_optimizer_trace_query.str,
display_optimizer_trace_query.length,
ds, &ds_warnings);
run_query_normal(con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
restore_optimizer_trace_query.str,
restore_optimizer_trace_query.length,
ds, &ds_warnings);
dynstr_free(&ds_result);
dynstr_free(&ds_warnings);
ps_protocol_enabled= save_ps_protocol_enabled;
view_protocol_enabled= save_view_protocol_enabled;
DBUG_VOID_RETURN;
}
...@@ -571,7 +571,6 @@ create view v2 as select a from t2; ...@@ -571,7 +571,6 @@ create view v2 as select a from t2;
explain select * from v2 ; explain select * from v2 ;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 10 1 SIMPLE t2 ALL NULL NULL NULL NULL 10
select * from information_schema.OPTIMIZER_TRACE;
QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES
explain select * from v2 { explain select * from v2 {
"steps": [ "steps": [
...@@ -685,7 +684,6 @@ explain select * from v1 ; ...@@ -685,7 +684,6 @@ explain select * from v1 ;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10
2 DERIVED t1 ALL NULL NULL NULL NULL 10 Using temporary; Using filesort 2 DERIVED t1 ALL NULL NULL NULL NULL 10 Using temporary; Using filesort
select * from information_schema.OPTIMIZER_TRACE;
QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES QUERY TRACE MISSING_BYTES_BEYOND_MAX_MEM_SIZE INSUFFICIENT_PRIVILEGES
explain select * from v1 { explain select * from v1 {
"steps": [ "steps": [
......
...@@ -48,12 +48,12 @@ create view v1 as select a from t1 group by b; ...@@ -48,12 +48,12 @@ create view v1 as select a from t1 group by b;
create view v2 as select a from t2; create view v2 as select a from t2;
--echo # Mergeable view --echo # Mergeable view
--optimizer_trace
explain select * from v2 ; explain select * from v2 ;
select * from information_schema.OPTIMIZER_TRACE;
--echo # Non-Mergeable view --echo # Non-Mergeable view
--optimizer_trace
explain select * from v1 ; explain select * from v1 ;
select * from information_schema.OPTIMIZER_TRACE;
drop table t1,t2; drop table t1,t2;
drop view v1,v2; drop view v1,v2;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment