Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mysql-test/include/get_rec_idx_ranges_from_opt_ctx.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ select *from json_table(
num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
3 changes: 3 additions & 0 deletions mysql-test/include/opt_context_schema.inc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ let $opt_context_schema=
"copy_cost"
]
}
},
"exact_row_count": {
"type": "number"
}
},
"required": [
Expand Down
46 changes: 41 additions & 5 deletions mysql-test/main/opt_context_load_stats_basic.result
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ index_name ranges num_rows max_index_blocks max_row_blocks
t1_idx_a ["(NULL) < (a) < (3)"] 12 1 1
t1_idx_b ["(6) < (b)"] 2 1 1
t1_idx_ab ["(NULL) < (a) < (3)"] 12 1 1
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t1 NULL
set @saved_opt_context_1=@opt_context;
set @saved_records_1=@records;
set @saved_indexes_1=@indexes;
Expand Down Expand Up @@ -355,11 +363,6 @@ select * from t1 where a > 10;
a b
Warnings:
Warning 4253 Failed to parse saved optimizer context: "file_stat_records" element not present at offset 1380.
set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].file_stat_records');
select * from t1 where a > 10;
a b
Warnings:
Warning 4253 Failed to parse saved optimizer context: "file_stat_records" element not present at offset 1380.
set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].indexes[0].index_name');
select * from t1 where a > 10;
a b
Expand Down Expand Up @@ -488,4 +491,37 @@ a b
Warnings:
Warning 4253 Failed to parse saved optimizer context: "copy_cost" element not present at offset 514.
drop table t1;
set optimizer_replay_context="";
create table t1 (a int primary key, b int not null, c varchar(10));
insert into t1 select seq, seq%5, concat('a-', seq) from seq_1_to_10;
set optimizer_record_context=1;
explain
select count(a), count(b), count(c) from t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 10
set @opt_context=
(select REGEXP_SUBSTR(
context,
'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)')
from information_schema.optimizer_context);
set @saved_opt_context_1= @opt_context;
set optimizer_replay_context=@opt_context_var_name;
set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].exact_row_count');
explain
select count(a) from t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
Warnings:
Warning 4254 Failed to match the stats from replay context with the optimizer stats: db1.t1 with exact_row_count: doesn't exist in list_contexts
explain
select count(b) from t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
Warnings:
Warning 4254 Failed to match the stats from replay context with the optimizer stats: db1.t1 with exact_row_count: doesn't exist in list_contexts
explain
select count(c) from t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 10
drop table t1;
drop database db1;
28 changes: 25 additions & 3 deletions mysql-test/main/opt_context_load_stats_basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ select * from t1 where a > 10;
set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].file_stat_records');
select * from t1 where a > 10;

set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].file_stat_records');
select * from t1 where a > 10;

set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].indexes[0].index_name');
select * from t1 where a > 10;

Expand Down Expand Up @@ -300,5 +297,30 @@ select * from t1 where a > 10;
set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].list_index_read_costs[0].copy_cost');
select * from t1 where a > 10;

drop table t1;
set optimizer_replay_context="";

create table t1 (a int primary key, b int not null, c varchar(10));
insert into t1 select seq, seq%5, concat('a-', seq) from seq_1_to_10;

set optimizer_record_context=1;

explain
select count(a), count(b), count(c) from t1;

--source include/get_opt_context.inc
set @saved_opt_context_1= @opt_context;

set optimizer_replay_context=@opt_context_var_name;
set @opt_context=json_remove(@saved_opt_context_1, '$.list_contexts[0].exact_row_count');
explain
select count(a) from t1;

explain
select count(b) from t1;

explain
select count(c) from t1;

drop table t1;
drop database db1;
190 changes: 190 additions & 0 deletions mysql-test/main/opt_context_store_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t1 NULL
#
# simple query using join of two tables
#
Expand Down Expand Up @@ -121,6 +129,15 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t2 NULL
db1.t1 NULL
#
# negative test
# simple query using join of two tables
Expand Down Expand Up @@ -161,6 +178,13 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
set optimizer_record_context=ON;
#
# there should be no duplicate information
Expand Down Expand Up @@ -215,6 +239,15 @@ t1_idx_a ["(5) <= (a) <= (5)"] 1 1 1
t1_idx_ab ["(5) <= (a) <= (5)"] 1 1 1
t1_idx_a ["(5) <= (a) <= (5)"] 1 1 1
t1_idx_ab ["(5) <= (a) <= (5)"] 1 1 1
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t2 NULL
db1.t1 NULL
#
# test for update
#
Expand Down Expand Up @@ -258,6 +291,14 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t1 NULL
#
# test for insert as select
#
Expand Down Expand Up @@ -304,6 +345,15 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t2 NULL
db1.t1 NULL
analyze table t1 persistent for all;
Table Op Msg_type Msg_text
db1.t1 analyze status Engine-independent statistics collected
Expand Down Expand Up @@ -359,6 +409,14 @@ index_name ranges num_rows max_index_blocks max_row_blocks
t1_idx_a ["(1) <= (a) <= (5)"] 35 1 1
t1_idx_ab ["(1) <= (a) <= (5)"] 35 1 1
t1_idx_b ["(6) <= (b) <= (10)"] 1 1 1
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t1 NULL
#
# simple query with or condition on the same column
#
Expand Down Expand Up @@ -412,6 +470,14 @@ t1_idx_ab [
"(1) <= (a) <= (5)",
"(6) <= (a) <= (10)"
] 36 2 1
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t1 NULL
#
# negative test on the simple query with or condition on 2 columns
#
Expand Down Expand Up @@ -450,6 +516,13 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
set optimizer_record_context=ON;
#
# simple query with or condition on 2 columns
Expand Down Expand Up @@ -500,6 +573,14 @@ index_name ranges num_rows max_index_blocks max_row_blocks
t1_idx_a ["(1) <= (a) <= (5)"] 35 1 1
t1_idx_ab ["(1) <= (a) <= (5)"] 35 1 1
t1_idx_b ["(6) <= (b) <= (10)"] 1 1 1
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
db1.t1 NULL
drop view view1;
drop table t1;
drop table t2;
Expand Down Expand Up @@ -900,6 +981,115 @@ num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
test.t1 NULL
drop function add1;
DROP TABLE t1;
set session use_stat_tables=@saved_use_stat_tables;
#
# MDEV-39791: Handle count aggregate optimization for replay purpose
#
create table t1 (a int primary key, b int not null, c varchar(10));
insert into t1 select seq, seq%5, concat('a-', seq) from seq_1_to_10;
set optimizer_record_context=1;
explain
select count(a), count(b), count(c) from t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 10
set @opt_context=
(select REGEXP_SUBSTR(
context,
'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)')
from information_schema.optimizer_context);
set @records=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.file_stat_records')));
select *from json_table(@records,
'$[*]' columns(file_stat_records text path '$')) as jt;
file_stat_records
10
set @file_stat_records=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.file_stat_records')));
select *from json_table(@file_stat_records,
'$[*]' columns(file_stat_records text path '$')) as jt;
file_stat_records
10
set @indexes=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.indexes')));
select *from json_table(
@indexes, '$[*][*]' columns(index_name text path '$.index_name',
rec_per_key json path '$.rec_per_key')) as jt;
index_name rec_per_key
PRIMARY ["1"]
set @list_ranges=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_ranges')));
select *from json_table(
@list_ranges,
'$[*][*]' columns(index_name text path '$.index_name',
ranges json path '$.ranges',
num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
test.t1 10
explain
select count(c) from t1 where b = (select count(b) from t1) or a = (select count(a) from t1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 10 Using where
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @opt_context=
(select REGEXP_SUBSTR(
context,
'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)')
from information_schema.optimizer_context);
set @records=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.file_stat_records')));
select *from json_table(@records,
'$[*]' columns(file_stat_records text path '$')) as jt;
file_stat_records
10
set @file_stat_records=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.file_stat_records')));
select *from json_table(@file_stat_records,
'$[*]' columns(file_stat_records text path '$')) as jt;
file_stat_records
10
set @indexes=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.indexes')));
select *from json_table(
@indexes, '$[*][*]' columns(index_name text path '$.index_name',
rec_per_key json path '$.rec_per_key')) as jt;
index_name rec_per_key
PRIMARY ["1"]
set @list_ranges=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_ranges')));
select *from json_table(
@list_ranges,
'$[*][*]' columns(index_name text path '$.index_name',
ranges json path '$.ranges',
num_rows int path '$.num_rows',
max_index_blocks int path '$.max_index_blocks',
max_row_blocks int path '$.max_row_blocks')) as jt;
index_name ranges num_rows max_index_blocks max_row_blocks
set @table_contexts=
(select JSON_DETAILED(JSON_EXTRACT(@opt_context, '$**.list_contexts')));
select *from json_table(
@table_contexts,
'$[*][*]' columns(table_name text path '$.name',
exact_row_count int path '$.exact_row_count')) as jt;
table_name exact_row_count
test.t1 10
drop table t1;
Loading
Loading