@@ -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 {
@@ -1541,6 +1546,8 @@ static void die(const char *fmt, ...)
1541
1546
{
1542
1547
char buff[DIE_BUFF_SIZE];
1543
1548
va_list args;
1549
+ DBUG_ENTER (" die" );
1550
+
1544
1551
va_start (args, fmt);
1545
1552
make_error_message (buff, sizeof (buff), fmt, args);
1546
1553
really_die (buff);
@@ -7841,7 +7848,7 @@ static void handle_no_active_connection(struct st_command *command,
7841
7848
*/
7842
7849
7843
7850
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,
7845
7852
DYNAMIC_STRING *ds, DYNAMIC_STRING *ds_warnings)
7846
7853
{
7847
7854
MYSQL_RES *res= 0 ;
@@ -7960,6 +7967,7 @@ void run_query_normal(struct st_connection *cn, struct st_command *command,
7960
7967
7961
7968
/* If we come here the query is both executed and read successfully */
7962
7969
handle_no_error (command);
7970
+ display_optimizer_trace (cn, ds);
7963
7971
revert_properties ();
7964
7972
7965
7973
end:
@@ -9580,6 +9588,9 @@ int main(int argc, char **argv)
9580
9588
case Q_RESET_CONNECTION:
9581
9589
do_reset_connection ();
9582
9590
break ;
9591
+ case Q_OPTIMIZER_TRACE:
9592
+ enable_optimizer_trace (cur_con);
9593
+ break ;
9583
9594
case Q_SEND_SHUTDOWN:
9584
9595
handle_command_error (command,
9585
9596
mysql_shutdown (cur_con->mysql ,
@@ -11238,3 +11249,90 @@ char *mysql_authentication_dialog_ask(MYSQL *mysql, int type,
11238
11249
11239
11250
return buf;
11240
11251
}
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
+ }
0 commit comments