Commit 9a7b3bf4 authored by Sergey Petrunya's avatar Sergey Petrunya

MDEV-299: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN changes back and forth during query execution

- Make SHOW EXPLAIN ignore range accesses when printing "Range checked for each record" plans.
parent 13e98578
...@@ -550,4 +550,50 @@ test ...@@ -550,4 +550,50 @@ test
test test
set debug_dbug=''; set debug_dbug='';
DROP TABLE t1; DROP TABLE t1;
#
# MDEV-299: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN changes back and forth during query execution
#
create table t1 (key1 int, col1 int, col2 int, filler char(100), key(key1));
insert into t1 select A.a+ 10 * B.a, 10, 10, 'filler-data' from t0 A, t0 B;
update t1 set col1=3, col2=10 where key1=1;
update t1 set col1=3, col2=1000 where key1=2;
update t1 set col1=3, col2=10 where key1=3;
update t1 set col1=3, col2=1000 where key1=4;
set @tmp_mdev299_jcl= @@join_cache_level;
set join_cache_level=0;
explain select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1)
set @show_explain_probe_select_id=1;
set debug_dbug='d,show_explain_probe_test_if_quick_select';
select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100;
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1)
Warnings:
Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1)
Warnings:
Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1)
Warnings:
Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE A ALL NULL NULL NULL NULL 100 Using where
1 SIMPLE B ALL key1 NULL NULL NULL 100 Range checked for each record (index map: 0x1)
Warnings:
Note 1003 select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100
count(*)
212
set debug_dbug='';
drop table t1;
drop table t0; drop table t0;
...@@ -542,6 +542,44 @@ set debug_dbug=''; ...@@ -542,6 +542,44 @@ set debug_dbug='';
DROP TABLE t1; DROP TABLE t1;
--echo #
--echo # MDEV-299: SHOW EXPLAIN: Plan produced by SHOW EXPLAIN changes back and forth during query execution
--echo #
create table t1 (key1 int, col1 int, col2 int, filler char(100), key(key1));
insert into t1 select A.a+ 10 * B.a, 10, 10, 'filler-data' from t0 A, t0 B;
# Make matches 3 records
update t1 set col1=3, col2=10 where key1=1; # small range
update t1 set col1=3, col2=1000 where key1=2; # big range
update t1 set col1=3, col2=10 where key1=3; # small range again
update t1 set col1=3, col2=1000 where key1=4; # big range
set @tmp_mdev299_jcl= @@join_cache_level;
set join_cache_level=0;
explain select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100;
set @show_explain_probe_select_id=1;
set debug_dbug='d,show_explain_probe_test_if_quick_select';
send
select count(*) from t1 A, t1 B where B.key1 < A.col2 and A.col1=3 AND B.col2 + 1 < 100;
connection default;
--source include/wait_condition.inc
evalp show explain for $thr2;
--source include/wait_condition.inc
evalp show explain for $thr2;
--source include/wait_condition.inc
evalp show explain for $thr2;
--source include/wait_condition.inc
evalp show explain for $thr2;
connection con1;
reap;
set debug_dbug='';
drop table t1;
## TODO: Test this: have several SHOW EXPLAIN requests be queued up for a ## TODO: Test this: have several SHOW EXPLAIN requests be queued up for a
......
...@@ -16912,6 +16912,14 @@ int read_first_record_seq(JOIN_TAB *tab) ...@@ -16912,6 +16912,14 @@ int read_first_record_seq(JOIN_TAB *tab)
static int static int
test_if_quick_select(JOIN_TAB *tab) test_if_quick_select(JOIN_TAB *tab)
{ {
DBUG_EXECUTE_IF("show_explain_probe_test_if_quick_select",
if (dbug_user_var_equals_int(tab->join->thd,
"show_explain_probe_select_id",
tab->join->select_lex->select_number))
dbug_serve_apcs(tab->join->thd, 1);
);
delete tab->select->quick; delete tab->select->quick;
tab->select->quick=0; tab->select->quick=0;
return tab->select->test_quick_select(tab->join->thd, tab->keys, return tab->select->test_quick_select(tab->join->thd, tab->keys,
...@@ -21410,6 +21418,7 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags, ...@@ -21410,6 +21418,7 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
tmp3.length(0); tmp3.length(0);
tmp4.length(0); tmp4.length(0);
quick_type= -1; quick_type= -1;
QUICK_SELECT_I *quick= NULL;
/* Don't show eliminated tables */ /* Don't show eliminated tables */
if (table->map & join->eliminated_tables) if (table->map & join->eliminated_tables)
...@@ -21428,8 +21437,9 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags, ...@@ -21428,8 +21437,9 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
enum join_type tab_type= tab->type; enum join_type tab_type= tab->type;
if ((tab->type == JT_ALL || tab->type == JT_HASH) && if ((tab->type == JT_ALL || tab->type == JT_HASH) &&
tab->select && tab->select->quick) tab->select && tab->select->quick && tab->use_quick != 2)
{ {
quick= tab->select->quick;
quick_type= tab->select->quick->get_type(); quick_type= tab->select->quick->get_type();
if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) || if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
(quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_INTERSECT) || (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_INTERSECT) ||
...@@ -21568,9 +21578,9 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags, ...@@ -21568,9 +21578,9 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
tab->select && tab->select->quick) tab->select && tab->select->quick)
=======*/ =======*/
} }
if (tab->type != JT_CONST && tab->select && tab->select->quick) if (tab->type != JT_CONST && tab->select && quick)
tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3); tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
if (key_info || (tab->select && tab->select->quick)) if (key_info || (tab->select && quick))
{ {
if (tmp2.length()) if (tmp2.length())
item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs)); item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
...@@ -21631,8 +21641,8 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags, ...@@ -21631,8 +21641,8 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
else else
{ {
ha_rows examined_rows; ha_rows examined_rows;
if (tab->select && tab->select->quick) if (tab->select && quick)
examined_rows= tab->select->quick->records; examined_rows= quick->records;
else if (tab_type == JT_NEXT || tab_type == JT_ALL || is_hj) else if (tab_type == JT_NEXT || tab_type == JT_ALL || is_hj)
{ {
if (tab->limit) if (tab->limit)
...@@ -21676,7 +21686,7 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags, ...@@ -21676,7 +21686,7 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
table->covering_keys.is_set(tab->index)) table->covering_keys.is_set(tab->index))
key_read=1; key_read=1;
if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT && if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
!((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row) !((QUICK_ROR_INTERSECT_SELECT*)quick)->need_to_fetch_row)
key_read=1; key_read=1;
if (tab->info) if (tab->info)
...@@ -21704,8 +21714,8 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags, ...@@ -21704,8 +21714,8 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
uint keyno= MAX_KEY; uint keyno= MAX_KEY;
if (tab->ref.key_parts) if (tab->ref.key_parts)
keyno= tab->ref.key; keyno= tab->ref.key;
else if (tab->select && tab->select->quick) else if (tab->select && quick)
keyno = tab->select->quick->index; keyno = quick->index;
if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno && if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
table->file->pushed_idx_cond) table->file->pushed_idx_cond)
......
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