Commit a8b2e831 authored by Sergey Petrunya's avatar Sergey Petrunya

MDEV-305: SHOW EXPLAIN: ref returned by SHOW EXPLAIN is different from the...

MDEV-305: SHOW EXPLAIN: ref returned by SHOW EXPLAIN is different from the normal EXPLAIN ('const' vs empty string)
- The problem was that create_ref_for_key() would act differently, depending on 
  whether we're running EXPLAIN or the actual query.
- As the first step, fixed the EXPLAIN printout not to depend on actions in create_ref_for_key().
parent 6f199f7c
......@@ -448,7 +448,7 @@ WHERE pk= (SELECT MAX(pk) FROM t2 WHERE pk = 3)
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 20 Using where
2 SUBQUERY t2 const PRIMARY PRIMARY 4 1 Using where
2 SUBQUERY t2 const PRIMARY PRIMARY 4 const 1 Using where
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
Warnings:
Note 1003 SELECT * FROM t2 WHERE a =
......@@ -521,9 +521,7 @@ INSERT INTO t2 VALUES
(1,5),(2,4),(3,6),(4,9),(5,2),(6,8),(7,4),(8,8),(9,0),(10,43),
(11,23),(12,3),(13,45),(14,16),(15,2),(16,33),(17,2),(18,5),(19,9),(20,2);
set @show_explain_probe_select_id=1;
set debug='d,show_explain_probe_join_exec_end';
Warnings:
Warning 1287 The syntax '@@debug' is deprecated and will be removed in MariaDB 5.6. Please use '@@debug_dbug' instead
set debug_dbug='d,show_explain_probe_join_exec_end';
SELECT * FROM t2 WHERE (5, 78) IN (SELECT `a1`, MAX(`a1`) FROM t2 GROUP BY `a1`);
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
......@@ -531,8 +529,25 @@ id select_type table type possible_keys key key_len ref rows Extra
Warnings:
Note 1003 SELECT * FROM t2 WHERE (5, 78) IN (SELECT `a1`, MAX(`a1`) FROM t2 GROUP BY `a1`)
pk a1
set debug='';
Warnings:
Warning 1287 The syntax '@@debug' is deprecated and will be removed in MariaDB 5.6. Please use '@@debug_dbug' instead
set debug_dbug='';
DROP TABLE t2;
drop table t0,t1;
DROP TABLE t1;
#
# MDEV-305: SHOW EXPLAIN: ref returned by SHOW EXPLAIN is different from the normal EXPLAIN ('const' vs empty string)
#
CREATE TABLE t1(a INT, KEY(a));
INSERT INTO t1 VALUES (3),(1),(5),(1);
set @show_explain_probe_select_id=1;
set debug_dbug='d,show_explain_probe_join_exec_start';
SELECT 'test' FROM t1 WHERE a=1;
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref a a 5 const 1 Using index
Warnings:
Note 1003 SELECT 'test' FROM t1 WHERE a=1
test
test
test
set debug_dbug='';
DROP TABLE t1;
drop table t0;
......@@ -508,7 +508,7 @@ INSERT INTO t2 VALUES
(11,23),(12,3),(13,45),(14,16),(15,2),(16,33),(17,2),(18,5),(19,9),(20,2);
set @show_explain_probe_select_id=1;
set debug='d,show_explain_probe_join_exec_end';
set debug_dbug='d,show_explain_probe_join_exec_end';
send
SELECT * FROM t2 WHERE (5, 78) IN (SELECT `a1`, MAX(`a1`) FROM t2 GROUP BY `a1`);
connection default;
......@@ -516,10 +516,33 @@ connection default;
evalp show explain for $thr2;
connection con1;
reap;
set debug='';
set debug_dbug='';
DROP TABLE t2;
DROP TABLE t1;
--echo #
--echo # MDEV-305: SHOW EXPLAIN: ref returned by SHOW EXPLAIN is different from the normal EXPLAIN ('const' vs empty string)
--echo #
CREATE TABLE t1(a INT, KEY(a));
INSERT INTO t1 VALUES (3),(1),(5),(1);
set @show_explain_probe_select_id=1;
set debug_dbug='d,show_explain_probe_join_exec_start';
send SELECT 'test' FROM t1 WHERE a=1;
connection default;
--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
## thread and served together.
......@@ -527,4 +550,4 @@ DROP TABLE t2;
## TODO: SHOW EXPLAIN while the primary query is running EXPLAIN EXTENDED/PARTITIONS
##
drop table t0,t1;
drop table t0;
......@@ -7905,6 +7905,7 @@ static bool create_ref_for_key(JOIN *join, JOIN_TAB *j,
j->ref.null_rejecting= 0;
j->ref.disable_cache= FALSE;
j->ref.null_ref_part= NO_REF_PART;
j->ref.const_ref_part_map= 0;
keyuse=org_keyuse;
store_key **ref_key= j->ref.key_copy;
......@@ -7952,6 +7953,7 @@ static bool create_ref_for_key(JOIN *join, JOIN_TAB *j,
if (thd->is_fatal_error)
DBUG_RETURN(TRUE);
tmp.copy();
j->ref.const_ref_part_map |= key_part_map(1) << i ;
}
else
*ref_key++= get_store_key(thd,
......@@ -21521,13 +21523,21 @@ int JOIN::print_explain(select_result_sink *result, uint8 explain_flags,
length= (longlong10_to_str(key_len, keylen_str_buf, 10) -
keylen_str_buf);
tmp3.append(keylen_str_buf, length, cs);
if (tab->ref.key_parts)
if (tab->ref.key_parts && tab_type != JT_FT)
{
for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
store_key **ref=tab->ref.key_copy;
for (uint kp= 0; kp < tab->ref.key_parts; kp++)
{
if (tmp4.length())
tmp4.append(',');
tmp4.append((*ref)->name(), strlen((*ref)->name()), cs);
if ((key_part_map(1) << kp) & tab->ref.const_ref_part_map)
tmp4.append("const");
else
{
tmp4.append((*ref)->name(), strlen((*ref)->name()), cs);
ref++;
}
}
}
}
......
......@@ -101,6 +101,13 @@ typedef struct st_table_ref
uchar *key_buff; ///< value to look for with key
uchar *key_buff2; ///< key_buff+key_length
store_key **key_copy; //
/*
Bitmap of key parts which refer to constants. key_copy only has copiers for
non-const key parts.
*/
key_part_map const_ref_part_map;
Item **items; ///< val()'s for each keypart
/*
Array of pointers to trigger variables. Some/all of the pointers may be
......
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