diff --git a/mysql-test/main/opt_context_load_stats_innodb.result b/mysql-test/main/opt_context_load_stats_innodb.result index eec2e4a28df..8341be2ec0d 100644 --- a/mysql-test/main/opt_context_load_stats_innodb.result +++ b/mysql-test/main/opt_context_load_stats_innodb.result @@ -1359,4 +1359,41 @@ select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]'); json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]') "10" drop table t1; +# +# replay does not apply to tables opened by INFORMATION_SCHEMA +# +create table t1 (a int, b int, key(a)) engine=innodb; +insert into t1 select seq%10, seq from seq_1_to_100; +analyze table t1 persistent for all; +Table Op Msg_type Msg_text +test.t1 analyze status Engine-independent statistics collected +test.t1 analyze status OK +select count(*) from t1 where a=3; +count(*) +10 +set @opt_context= +(select REGEXP_SUBSTR( +context, +'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)') +from information_schema.optimizer_context); +set @opt_context_rpk= json_replace(@opt_context, +'$.tables[0].indexes[0].rec_per_key[0]', 50); +set optimizer_replay_context='opt_context_rpk'; +select index_name, cardinality from information_schema.statistics +where table_schema='test' and table_name='t1'; +index_name cardinality +a 10 +set optimizer_replay_context=''; +select count(*) from t1 where a=3; +count(*) +10 +set @opt_context= +(select REGEXP_SUBSTR( +context, +'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)') +from information_schema.optimizer_context); +select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]'); +json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]') +"10" +drop table t1; # End of 13.1 tests diff --git a/mysql-test/main/opt_context_load_stats_innodb.test b/mysql-test/main/opt_context_load_stats_innodb.test index 5d4ab3da62c..5b956f8b2ec 100644 --- a/mysql-test/main/opt_context_load_stats_innodb.test +++ b/mysql-test/main/opt_context_load_stats_innodb.test @@ -401,4 +401,23 @@ select count(*) from t1 where a=3; select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]'); drop table t1; +--echo # +--echo # replay does not apply to tables opened by INFORMATION_SCHEMA +--echo # +create table t1 (a int, b int, key(a)) engine=innodb; +insert into t1 select seq%10, seq from seq_1_to_100; +analyze table t1 persistent for all; +select count(*) from t1 where a=3; +--source include/opt_context_save_in_var.inc +set @opt_context_rpk= json_replace(@opt_context, + '$.tables[0].indexes[0].rec_per_key[0]', 50); +set optimizer_replay_context='opt_context_rpk'; +select index_name, cardinality from information_schema.statistics + where table_schema='test' and table_name='t1'; +set optimizer_replay_context=''; +select count(*) from t1 where a=3; +--source include/opt_context_save_in_var.inc +select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]'); +drop table t1; + --echo # End of 13.1 tests diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 2cebfd440dc..51dd96b00fb 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -4865,6 +4865,7 @@ fill_schema_table_by_open(THD *thd, MEM_ROOT *mem_root, LEX *old_lex= thd->lex, temp_lex, *lex; LEX_CSTRING db_name, table_name; TABLE_LIST *table_list; + Optimizer_context_replay *old_opt_ctx_replay; bool result= true, open_result, run, ext_error_handling; DBUG_ENTER("fill_schema_table_by_open"); @@ -4885,6 +4886,13 @@ fill_schema_table_by_open(THD *thd, MEM_ROOT *mem_root, thd->stmt_arena= &i_s_arena; thd->set_n_backup_active_arena(&i_s_arena, &backup_arena); + /* + Tables opened here are closed before the end of the statement, so + the optimizer context replay could not restore their statistics. + */ + old_opt_ctx_replay= thd->opt_ctx_replay; + thd->opt_ctx_replay= NULL; + /* Prepare temporary LEX. */ thd->lex= lex= &temp_lex; lex_start(thd); @@ -5017,6 +5025,7 @@ end: thd->lex= old_lex; + thd->opt_ctx_replay= old_opt_ctx_replay; thd->stmt_arena= old_arena; thd->restore_active_arena(&i_s_arena, &backup_arena);