Commit 55597a48 authored by Sergey Petrunya's avatar Sergey Petrunya

MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used

- Make Item_subselect::fix_fields() ignore UNCACHEABLE_EXPLAIN flag when deciding whether 
  the subquery item should be marked as constant.
parent a742d49c
...@@ -1838,7 +1838,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra ...@@ -1838,7 +1838,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 range a a 4 NULL 4 100.00 Using where; Using index 1 PRIMARY t1 range a a 4 NULL 4 100.00 Using where; Using index
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings: Warnings:
Note 1003 select max(`test`.`t1`.`a`) AS `MAX(a)` from `test`.`t1` where (<expr_cache><<cache>(1),<cache>(2)>(<in_optimizer>(<cache>((1,2)),<exists>(select 3,4 having (((1 = 3) or isnull(3)) and ((2 = 4) or isnull(4)) and <is_not_null_test>(3) and <is_not_null_test>(4))))) and (`test`.`t1`.`a` < 10)) Note 1003 select max(`test`.`t1`.`a`) AS `MAX(a)` from `test`.`t1` where (<cache>(<in_optimizer>((1,2),<exists>(select 3,4 having (((1 = 3) or isnull(3)) and ((2 = 4) or isnull(4)) and <is_not_null_test>(3) and <is_not_null_test>(4))))) and (`test`.`t1`.`a` < 10))
SELECT MAX(a) FROM t1 WHERE (1,2) IN (SELECT 3,4) AND a<10; SELECT MAX(a) FROM t1 WHERE (1,2) IN (SELECT 3,4) AND a<10;
MAX(a) MAX(a)
NULL NULL
......
...@@ -6768,5 +6768,43 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -6768,5 +6768,43 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
a a
drop table t1,t2; drop table t1,t2;
#
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
#
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('USA');
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
INSERT INTO t3 VALUES (12),(22),(9),(45);
create table t4 like t3;
insert into t4 select * from t3;
# This should not show range access for table t2
explain
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
flush table_statistics;
flush index_statistics;
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
show index_statistics;
Table_schema Table_name Index_name Rows_read
test t2 b 1
set global userstat=@tmp_mdev410;
DROP TABLE t1,t2,t3,t4;
# return optimizer switch changed in the beginning of this test # return optimizer switch changed in the beginning of this test
set optimizer_switch=@subselect_tmp; set optimizer_switch=@subselect_tmp;
...@@ -161,18 +161,18 @@ SET optimizer_switch='materialization=on,in_to_exists=on'; ...@@ -161,18 +161,18 @@ SET optimizer_switch='materialization=on,in_to_exists=on';
EXPLAIN EXPLAIN
SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM t1) OR a = b; SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM t1) OR a = b;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 index a a 5 NULL 2 Using index 1 PRIMARY t2 index a a 5 NULL 2 Using where; Using index
1 PRIMARY t3 index b b 5 NULL 4 Using where; Using index; Using join buffer (flat, BNL join) 1 PRIMARY t3 ref b b 5 test.t2.a 2 Using index
2 MATERIALIZED t1 index PRIMARY,a a 5 NULL 2 Using index 2 SUBQUERY t1 index_subquery PRIMARY,a a 5 const 0 Using index; Using where
SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM t1) OR a = b; SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM t1) OR a = b;
pk a b pk a b
0 4 4 0 4 4
EXPLAIN EXPLAIN
SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM v1) OR a = b; SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM v1) OR a = b;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 index a a 5 NULL 2 Using index 1 PRIMARY t2 index a a 5 NULL 2 Using where; Using index
1 PRIMARY t3 index b b 5 NULL 4 Using where; Using index; Using join buffer (flat, BNL join) 1 PRIMARY t3 ref b b 5 test.t2.a 2 Using index
2 MATERIALIZED t1 index PRIMARY,a a 5 NULL 2 Using index 2 SUBQUERY t1 index_subquery PRIMARY,a a 5 const 0 Using index; Using where
SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM v1) OR a = b; SELECT * FROM t2,t3 WHERE (2,9) IN (SELECT DISTINCT a,pk FROM v1) OR a = b;
pk a b pk a b
0 4 4 0 4 4
......
This diff is collapsed.
...@@ -2183,7 +2183,7 @@ NULL ...@@ -2183,7 +2183,7 @@ NULL
EXPLAIN EXPLAIN
SELECT * FROM t2 WHERE (SELECT f3a, f3b FROM t3) NOT IN (SELECT f1a, f1b FROM t1); SELECT * FROM t2 WHERE (SELECT f3a, f3b FROM t3) NOT IN (SELECT f1a, f1b FROM t1);
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
3 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 3 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
SELECT * FROM t2 WHERE (SELECT f3a, f3b FROM t3) NOT IN (SELECT f1a, f1b FROM t1); SELECT * FROM t2 WHERE (SELECT f3a, f3b FROM t3) NOT IN (SELECT f1a, f1b FROM t1);
......
...@@ -162,7 +162,7 @@ EXPLAIN ...@@ -162,7 +162,7 @@ EXPLAIN
SELECT * FROM (SELECT * FROM t2) AS a2 SELECT * FROM (SELECT * FROM t2) AS a2
WHERE (SELECT distinct SUM(distinct f3 ) FROM t1); WHERE (SELECT distinct SUM(distinct f3 ) FROM t1);
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1 PRIMARY <derived2> system NULL NULL NULL NULL 0 const row not found
3 SUBQUERY t1 index NULL f3 5 NULL 2 Using index 3 SUBQUERY t1 index NULL f3 5 NULL 2 Using index
2 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table 2 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
insert into t2 values (1),(2); insert into t2 values (1),(2);
......
...@@ -6766,6 +6766,44 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -6766,6 +6766,44 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
a a
drop table t1,t2; drop table t1,t2;
#
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
#
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('USA');
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
INSERT INTO t3 VALUES (12),(22),(9),(45);
create table t4 like t3;
insert into t4 select * from t3;
# This should not show range access for table t2
explain
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
flush table_statistics;
flush index_statistics;
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
show index_statistics;
Table_schema Table_name Index_name Rows_read
test t2 b 1
set global userstat=@tmp_mdev410;
DROP TABLE t1,t2,t3,t4;
# return optimizer switch changed in the beginning of this test # return optimizer switch changed in the beginning of this test
set optimizer_switch=@subselect_tmp; set optimizer_switch=@subselect_tmp;
set optimizer_switch=default; set optimizer_switch=default;
......
...@@ -6763,6 +6763,44 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -6763,6 +6763,44 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
a a
drop table t1,t2; drop table t1,t2;
#
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
#
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('USA');
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
INSERT INTO t3 VALUES (12),(22),(9),(45);
create table t4 like t3;
insert into t4 select * from t3;
# This should not show range access for table t2
explain
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
flush table_statistics;
flush index_statistics;
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
show index_statistics;
Table_schema Table_name Index_name Rows_read
test t2 b 1
set global userstat=@tmp_mdev410;
DROP TABLE t1,t2,t3,t4;
# return optimizer switch changed in the beginning of this test # return optimizer switch changed in the beginning of this test
set optimizer_switch=@subselect_tmp; set optimizer_switch=@subselect_tmp;
set @optimizer_switch_for_subselect_test=null; set @optimizer_switch_for_subselect_test=null;
...@@ -6774,6 +6774,44 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -6774,6 +6774,44 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
a a
drop table t1,t2; drop table t1,t2;
#
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
#
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('USA');
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
INSERT INTO t3 VALUES (12),(22),(9),(45);
create table t4 like t3;
insert into t4 select * from t3;
# This should not show range access for table t2
explain
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
flush table_statistics;
flush index_statistics;
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
show index_statistics;
Table_schema Table_name Index_name Rows_read
test t2 b 1
set global userstat=@tmp_mdev410;
DROP TABLE t1,t2,t3,t4;
# return optimizer switch changed in the beginning of this test # return optimizer switch changed in the beginning of this test
set optimizer_switch=@subselect_tmp; set optimizer_switch=@subselect_tmp;
set optimizer_switch=default; set optimizer_switch=default;
......
...@@ -6763,6 +6763,44 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -6763,6 +6763,44 @@ id select_type table type possible_keys key key_len ref rows Extra
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
a a
drop table t1,t2; drop table t1,t2;
#
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
#
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('USA');
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
INSERT INTO t3 VALUES (12),(22),(9),(45);
create table t4 like t3;
insert into t4 select * from t3;
# This should not show range access for table t2
explain
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 system NULL NULL NULL NULL 1
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
flush table_statistics;
flush index_statistics;
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
MIN(b)
NULL
# The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
test t1 2 0 0
test t2 3 0 0
show index_statistics;
Table_schema Table_name Index_name Rows_read
test t2 b 1
set global userstat=@tmp_mdev410;
DROP TABLE t1,t2,t3,t4;
# return optimizer switch changed in the beginning of this test # return optimizer switch changed in the beginning of this test
set optimizer_switch=@subselect_tmp; set optimizer_switch=@subselect_tmp;
set @optimizer_switch_for_subselect_test=null; set @optimizer_switch_for_subselect_test=null;
......
...@@ -5686,5 +5686,40 @@ SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t ...@@ -5686,5 +5686,40 @@ SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t
drop table t1,t2; drop table t1,t2;
--echo #
--echo # MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
--echo #
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
INSERT INTO t1 VALUES ('USA');
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
INSERT INTO t3 VALUES (12),(22),(9),(45);
create table t4 like t3;
insert into t4 select * from t3;
--echo # This should not show range access for table t2
explain
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
set @tmp_mdev410=@@global.userstat;
set global userstat=on;
flush table_statistics;
flush index_statistics;
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
--echo # The following shows that t2 was indeed scanned with a full scan.
show table_statistics;
show index_statistics;
set global userstat=@tmp_mdev410;
DROP TABLE t1,t2,t3,t4;
--echo # return optimizer switch changed in the beginning of this test --echo # return optimizer switch changed in the beginning of this test
set optimizer_switch=@subselect_tmp; set optimizer_switch=@subselect_tmp;
...@@ -287,7 +287,7 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref) ...@@ -287,7 +287,7 @@ bool Item_subselect::fix_fields(THD *thd_param, Item **ref)
else else
goto end; goto end;
if ((uncacheable= engine->uncacheable())) if ((uncacheable= engine->uncacheable() & ~UNCACHEABLE_EXPLAIN))
{ {
const_item_cache= 0; const_item_cache= 0;
if (uncacheable & UNCACHEABLE_RAND) if (uncacheable & UNCACHEABLE_RAND)
......
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