@@ -132,6 +132,7 @@ static my_bool disable_info= 1;
132
132
static my_bool abort_on_error= 1 , opt_continue_on_error= 0 ;
133
133
static my_bool server_initialized= 0 ;
134
134
static my_bool is_windows= 0 ;
135
+ static my_bool optimizer_trace_active= 0 ;
135
136
static char **default_argv;
136
137
static const char *load_default_groups[]=
137
138
{ " mysqltest" , " mariadb-test" , " client" , " client-server" , " client-mariadb" ,
@@ -388,6 +389,7 @@ enum enum_commands {
388
389
Q_MOVE_FILE, Q_REMOVE_FILES_WILDCARD, Q_SEND_EVAL,
389
390
Q_ENABLE_PREPARE_WARNINGS, Q_DISABLE_PREPARE_WARNINGS,
390
391
Q_RESET_CONNECTION,
392
+ Q_OPTIMIZER_TRACE,
391
393
Q_UNKNOWN, /* Unknown command. */
392
394
Q_COMMENT, /* Comments, ignored. */
393
395
Q_COMMENT_WITH_COMMAND,
@@ -498,7 +500,7 @@ const char *command_names[]=
498
500
" enable_prepare_warnings" ,
499
501
" disable_prepare_warnings" ,
500
502
" reset_connection" ,
501
-
503
+ " optimizer_trace " ,
502
504
0
503
505
};
504
506
@@ -643,6 +645,9 @@ void free_all_replace(){
643
645
}
644
646
645
647
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);
646
651
647
652
648
653
class LogFile {
@@ -1551,6 +1556,8 @@ static void die(const char *fmt, ...)
1551
1556
{
1552
1557
char buff[DIE_BUFF_SIZE];
1553
1558
va_list args;
1559
+ DBUG_ENTER (" die" );
1560
+
1554
1561
va_start (args, fmt);
1555
1562
make_error_message (buff, sizeof (buff), fmt, args);
1556
1563
really_die (buff);
@@ -7901,7 +7908,7 @@ static void handle_no_active_connection(struct st_command *command,
7901
7908
*/
7902
7909
7903
7910
void run_query_normal (struct st_connection *cn, struct st_command *command,
7904
- int flags, char *query, size_t query_len,
7911
+ int flags, const char *query, size_t query_len,
7905
7912
DYNAMIC_STRING *ds, DYNAMIC_STRING *ds_warnings)
7906
7913
{
7907
7914
MYSQL_RES *res= 0 ;
@@ -8020,6 +8027,7 @@ void run_query_normal(struct st_connection *cn, struct st_command *command,
8020
8027
8021
8028
/* If we come here the query is both executed and read successfully */
8022
8029
handle_no_error (command);
8030
+ display_optimizer_trace (cn, ds);
8023
8031
revert_properties ();
8024
8032
8025
8033
end:
@@ -9678,6 +9686,9 @@ int main(int argc, char **argv)
9678
9686
case Q_RESET_CONNECTION:
9679
9687
do_reset_connection ();
9680
9688
break ;
9689
+ case Q_OPTIMIZER_TRACE:
9690
+ enable_optimizer_trace (cur_con);
9691
+ break ;
9681
9692
case Q_SEND_SHUTDOWN:
9682
9693
handle_command_error (command,
9683
9694
mysql_shutdown (cur_con->mysql ,
@@ -11358,3 +11369,90 @@ char *mysql_authentication_dialog_ask(MYSQL *mysql, int type,
11358
11369
11359
11370
return buf;
11360
11371
}
11372
+
11373
+ /*
11374
+ Enable optimizer trace for the next command
11375
+ */
11376
+
11377
+ LEX_CSTRING enable_optimizer_trace_query=
11378
+ {
11379
+ STRING_WITH_LEN (" set @mysqltest_save_optimzer_trace=@@optimizer_trace,@@optimizer_trace=\" enabled=on\" " )
11380
+ };
11381
+
11382
+ LEX_CSTRING restore_optimizer_trace_query=
11383
+ {
11384
+ STRING_WITH_LEN (" set @@optimizer_trace=@mysqltest_save_optimzer_trace" )
11385
+ };
11386
+
11387
+
11388
+ LEX_CSTRING display_optimizer_trace_query
11389
+ {
11390
+ STRING_WITH_LEN (" SELECT * from information_schema.optimizer_trace" )
11391
+ };
11392
+
11393
+
11394
+ void enable_optimizer_trace (struct st_connection *con)
11395
+ {
11396
+ MYSQL *mysql= con->mysql ;
11397
+ my_bool save_ps_protocol_enabled= ps_protocol_enabled;
11398
+ my_bool save_view_protocol_enabled= view_protocol_enabled;
11399
+ DYNAMIC_STRING ds_result;
11400
+ DYNAMIC_STRING ds_warnings;
11401
+ struct st_command command;
11402
+ DBUG_ENTER (" enable_optimizer_trace" );
11403
+
11404
+ if (!mysql)
11405
+ DBUG_VOID_RETURN;
11406
+ ps_protocol_enabled= view_protocol_enabled= 0 ;
11407
+
11408
+ init_dynamic_string (&ds_result, NULL , 0 , 256 );
11409
+ init_dynamic_string (&ds_warnings, NULL , 0 , 256 );
11410
+ bzero (&command, sizeof (command));
11411
+
11412
+ run_query_normal (con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
11413
+ enable_optimizer_trace_query.str ,
11414
+ enable_optimizer_trace_query.length ,
11415
+ &ds_result, &ds_warnings);
11416
+ dynstr_free (&ds_result);
11417
+ dynstr_free (&ds_warnings);
11418
+ ps_protocol_enabled= save_ps_protocol_enabled;
11419
+ view_protocol_enabled= save_view_protocol_enabled;
11420
+ optimizer_trace_active= 1 ;
11421
+ DBUG_VOID_RETURN;
11422
+ }
11423
+
11424
+
11425
+ void display_optimizer_trace (struct st_connection *con,
11426
+ DYNAMIC_STRING *ds)
11427
+ {
11428
+ my_bool save_ps_protocol_enabled= ps_protocol_enabled;
11429
+ my_bool save_view_protocol_enabled= view_protocol_enabled;
11430
+ DYNAMIC_STRING ds_result;
11431
+ DYNAMIC_STRING ds_warnings;
11432
+ struct st_command command;
11433
+ DBUG_ENTER (" display_optimizer_trace" );
11434
+
11435
+ if (!optimizer_trace_active)
11436
+ DBUG_VOID_RETURN;
11437
+
11438
+ optimizer_trace_active= 0 ;
11439
+ ps_protocol_enabled= view_protocol_enabled= 0 ;
11440
+
11441
+ init_dynamic_string (&ds_result, NULL , 0 , 256 );
11442
+ init_dynamic_string (&ds_warnings, NULL , 0 , 256 );
11443
+ bzero (&command, sizeof (command));
11444
+
11445
+ run_query_normal (con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
11446
+ display_optimizer_trace_query.str ,
11447
+ display_optimizer_trace_query.length ,
11448
+ ds, &ds_warnings);
11449
+ run_query_normal (con, &command, QUERY_SEND_FLAG | QUERY_REAP_FLAG,
11450
+ restore_optimizer_trace_query.str ,
11451
+ restore_optimizer_trace_query.length ,
11452
+ ds, &ds_warnings);
11453
+ dynstr_free (&ds_result);
11454
+ dynstr_free (&ds_warnings);
11455
+ ps_protocol_enabled= save_ps_protocol_enabled;
11456
+ view_protocol_enabled= save_view_protocol_enabled;
11457
+ DBUG_VOID_RETURN;
11458
+ }
0 commit comments