summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <timour@askmonty.org>2010-10-20 15:43:55 +0300
committerunknown <timour@askmonty.org>2010-10-20 15:43:55 +0300
commite85a4cb6b58b92f324f661d7bb78e6a5f90da883 (patch)
tree5e00d55b8f1d2ff203956ee163b40bbb0ef61ce0
parentaddd57828d5702349cf052e964c6bbc6e0371f9a (diff)
downloadmariadb-git-e85a4cb6b58b92f324f661d7bb78e6a5f90da883.tar.gz
MWL#89: Cost-based choice between Materialization and IN->EXISTS transformation
- Added more tests to the MWL#89 specific test, and made the test more modular. - Updated test files. - Fixed a memory leak. - More comments. mysql-test/r/subselect_mat.result: - Updated the test file to reflect the new optimizer switches related to materialized subquery execution. - Added one extra test to test all cases that expose BUG#40037 (this is an old bug from 5.x). - Updated the test result with correct results that expose BUG#40037. mysql-test/t/subselect_mat.test: - Updated the test file to reflect the new optimizer switches related to materialized subquery execution. - Added one extra test to test all cases that expose BUG#40037 (this is an old bug from 5.x). - Updated the test result with correct results that expose BUG#40037. sql/sql_select.cc: Fixed a memory leak reported by Valgrind.
-rw-r--r--mysql-test/include/subselect_mat_cost.inc152
-rw-r--r--mysql-test/r/subselect_mat.result49
-rw-r--r--mysql-test/r/subselect_mat_cost.result3664
-rw-r--r--mysql-test/t/subselect_mat.test45
-rw-r--r--mysql-test/t/subselect_mat_cost.test304
-rw-r--r--sql/opt_subselect.cc13
-rw-r--r--sql/sql_select.cc6
7 files changed, 3940 insertions, 293 deletions
diff --git a/mysql-test/include/subselect_mat_cost.inc b/mysql-test/include/subselect_mat_cost.inc
new file mode 100644
index 00000000000..4ccbd08693a
--- /dev/null
+++ b/mysql-test/include/subselect_mat_cost.inc
@@ -0,0 +1,152 @@
+-- echo
+-- echo /* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+-- echo
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+-- echo
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+
+-- echo
+-- echo /*
+-- echo B. "Natural" examples of subqueries without grouping that
+-- echo cannot be flattened into semijoin.
+-- echo */
+
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+-- echo
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+-- echo UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+-- echo
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+-- echo
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+
+-- echo
+-- echo /* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+-- echo
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+-- echo
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+-- echo
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+-- echo
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+-- echo
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+
+-- echo
+-- echo /*
+-- echo D. Subqueries for which materialization is not possible, and the
+-- echo optimizer reverts to in-to-exists.
+-- echo */
+# The first two cases are rejected during the prepare phase by the procedure
+# subquery_types_allow_materialization().
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+-- echo
+# The following two subqueries return the result of a string function with a
+# blob argument, where the return type may be != blob. These are rejected during
+# cost-based optimization when attempting to create a temporary table.
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+-- echo
+
+
+-- echo
+-- echo /* E. Edge cases. */
+-- echo
+
+-- echo /* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+--error ER_ILLEGAL_SUBQUERY_OPTIMIZER_SWITCHES
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+set @@optimizer_switch = @save_optimizer_switch;
+
+-- echo /* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+
+-- echo /* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+-- echo UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+
+-- echo /* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+
+-- echo /* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+-- echo TODO this test produces wrong result due to missing logic to handle the case
+-- echo when JOIN::optimize detects an empty subquery result.
+#explain
+#select a1 from t1 where a1 in (select max(b1) from t2);
+#select a1 from t1 where a1 in (select max(b1) from t2);
+-- echo
+#explain
+#select a1 from t1 where a1 in (select max(b1) from t2 where b1 = '7 - 02');
+#select a1 from t1 where a1 in (select max(b1) from t2 where b1 = '7 - 02');
+
+-- echo /* E.6 make_join_select detects impossible WHERE. *
+
+-- echo TODO
+
+-- echo /* E.7 constant optimization detects "no matching row in const table". */
+
+-- echo TODO
+
+-- echo /* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+
+-- echo
+-- echo /* F. UPDATE/DELETE with subqueries. */
+-- echo
+
+-- echo TODO
+-- echo
diff --git a/mysql-test/r/subselect_mat.result b/mysql-test/r/subselect_mat.result
index a4a69bb2f83..ca511caaabe 100644
--- a/mysql-test/r/subselect_mat.result
+++ b/mysql-test/r/subselect_mat.result
@@ -30,7 +30,7 @@ create index it3i3 on t3i (c1, c2);
insert into t1i select * from t1;
insert into t2i select * from t2;
insert into t3i select * from t3;
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
/******************************************************************************
* Simple tests.
******************************************************************************/
@@ -176,33 +176,33 @@ a1 a2
1 - 02 2 - 02
select * from t1 where (a1, a2) in (select b1, min(b2) from t2i limit 1,1);
ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
prepare st1 from
"select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1)";
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=on';
execute st1;
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
execute st1;
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=on';
prepare st1 from
"select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1)";
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
execute st1;
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=on';
execute st1;
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain extended
select * from t1 where (a1, a2) in (select b1, b2 from t2 order by b1, b2);
id select_type table type possible_keys key key_len ref rows filtered Extra
@@ -549,7 +549,7 @@ a1 a2
Test that BLOBs are not materialized (except when arguments of some functions).
*/
# force materialization to be always considered
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
set @prefix_len = 6;
set @blob_len = 16;
set @suffix_len = @blob_len - @prefix_len;
@@ -951,7 +951,7 @@ insert into t1bit values (b'010', b'110');
insert into t2bit values (b'001', b'101');
insert into t2bit values (b'010', b'110');
insert into t2bit values (b'110', b'111');
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain extended select bin(a1), bin(a2)
from t1bit
where (a1, a2) in (select b1, b2 from t2bit);
@@ -994,7 +994,7 @@ drop table t1, t2, t3, t1i, t2i, t3i, columns;
/******************************************************************************
* Test the cache of the left operand of IN.
******************************************************************************/
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
create table t1 (s1 int);
create table t2 (s2 int);
insert into t1 values (5),(1),(0);
@@ -1136,27 +1136,40 @@ drop table t2;
create table t1 (a1 int key);
create table t2 (b1 int);
insert into t1 values (5);
+Only the last query returns correct result. Filed as BUG#40037.
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not found
select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
min(a1)
-set @@optimizer_switch='default,materialization=off';
+NULL
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
explain select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 0 const row not found
select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
min(a1)
-set @@optimizer_switch='default,semijoin=off';
+NULL
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain select min(a1) from t1 where 7 in (select b1 from t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not found
select min(a1) from t1 where 7 in (select b1 from t2);
min(a1)
-set @@optimizer_switch='default,materialization=off';
+NULL
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
+explain select min(a1) from t1 where 7 in (select b1 from t2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select min(a1) from t1 where 7 in (select b1 from t2);
+min(a1)
+NULL
+set @@optimizer_switch='materialization=off,in_to_exists=off,semijoin=on';
explain select min(a1) from t1 where 7 in (select b1 from t2);
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
@@ -1167,7 +1180,7 @@ drop table t1,t2;
create table t1 (a char(2), b varchar(10));
insert into t1 values ('a', 'aaa');
insert into t1 values ('aa', 'aaaa');
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain select a,b from t1 where b in (select a from t1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
@@ -1187,7 +1200,7 @@ INSERT INTO t1 (f1, f2) VALUES (10, 1.668);
CREATE TABLE t2 LIKE t1;
INSERT INTO t2 VALUES (1, 1.789);
INSERT INTO t2 VALUES (13, 1.454);
-SET @@optimizer_switch='default,semijoin=on,materialization=on';
+SET @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
EXPLAIN SELECT COUNT(*) FROM t1 WHERE (f1,f2) IN (SELECT f1,f2 FROM t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY subselect2 ALL unique_key NULL NULL NULL 2
@@ -1208,7 +1221,7 @@ PRIMARY KEY (pk)
INSERT INTO t1 VALUES (1,'o','ffff','ffff','ffoo'),(2,'f','ffff','ffff','ffff');
CREATE TABLE t2 LIKE t1;
INSERT INTO t2 VALUES (1,'i','iiii','iiii','iiii'),(2,'f','ffff','ffff','ffff');
-SET @@optimizer_switch='default,semijoin=on,materialization=on';
+SET @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
EXPLAIN SELECT pk FROM t1 WHERE (a) IN (SELECT a FROM t2 WHERE pk > 0);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
@@ -1237,7 +1250,7 @@ i
3
4
set @save_optimizer_switch=@@optimizer_switch;
-set session optimizer_switch='materialization=off';
+set session optimizer_switch='materialization=off,in_to_exists=on';
select * from t1 where t1.i in (select t2.i from t2 join t3 where t2.i + t3.i = 5);
i
1
diff --git a/mysql-test/r/subselect_mat_cost.result b/mysql-test/r/subselect_mat_cost.result
index bfa4589bdd2..c6cb7685f4a 100644
--- a/mysql-test/r/subselect_mat_cost.result
+++ b/mysql-test/r/subselect_mat_cost.result
@@ -1,191 +1,3597 @@
-drop table if exists t1, t2, t3, t1i, t2i, t3i;
-create table t1 (a1 char(8), a2 char(8));
-create table t2 (b1 char(8), b2 char(8));
-create table t3 (c1 char(8), c2 char(8));
-Unindexed tables
-insert into t1 values ('1 - 00', '2 - 00');
-insert into t1 values ('1 - 01', '2 - 01');
-insert into t1 values ('1 - 02', '2 - 02');
-insert into t2 values ('1 - 01', '2 - 01');
-insert into t2 values ('1 - 01', '2 - 01');
-insert into t2 values ('1 - 02', '2 - 02');
-insert into t2 values ('1 - 02', '2 - 02');
-insert into t2 values ('1 - 03', '2 - 03');
-insert into t3 values ('1 - 01', '2 - 01');
-insert into t3 values ('1 - 02', '2 - 02');
-insert into t3 values ('1 - 03', '2 - 03');
-insert into t3 values ('1 - 04', '2 - 04');
-Indexed tables
-create table t1i (a1 char(8), a2 char(8));
-create table t2i (b1 char(8), b2 char(8));
-create table t3i (c1 char(8), c2 char(8));
-create index it1i1 on t1i (a1);
-create index it1i2 on t1i (a2);
-create index it1i3 on t1i (a1, a2);
-create index it2i1 on t2i (b1);
-create index it2i2 on t2i (b2);
-create index it2i3 on t2i (b1, b2);
-create index it3i1 on t3i (c1);
-create index it3i2 on t3i (c2);
-create index it3i3 on t3i (c1, c2);
-insert into t1i select * from t1;
-insert into t2i select * from t2;
-insert into t3i select * from t3;
-
-1. Both materialization and in-to-exists are possible to execute
+drop table if exists t1, t2, t1_1024, t2_1024;
+drop procedure if exists make_t1_indexes;
+drop procedure if exists make_t2_indexes;
+drop procedure if exists remove_t1_indexes;
+drop procedure if exists remove_t2_indexes;
+drop procedure if exists add_materialization_data;
+drop procedure if exists delete_materialization_data;
+drop procedure if exists set_all_columns_not_null;
+drop procedure if exists set_all_columns_nullable;
+create table t1 (a1 char(8), a2 char(8), a3 char(8), a4 int);
+insert into t1 values ('1 - 00', '2 - 00', '3 - 00', 0);
+insert into t1 values ('1 - 01', '2 - 01', '3 - 01', 1);
+insert into t1 values ('1 - 02', '2 - 02', '3 - 02', 2);
+create table t2 (b1 char(8), b2 char(8), b3 char(8), b4 int);
+insert into t2 values ('1 - 01', '2 - 01', '3 - 01', 1);
+insert into t2 values ('1 - 01', '2 - 01', '3 - 02', 2);
+insert into t2 values ('1 - 02', '2 - 02', '3 - 03', 3);
+insert into t2 values ('1 - 02', '2 - 02', '3 - 04', 4);
+insert into t2 values ('1 - 03', '2 - 03', '3 - 05', 5);
+create table t1_1024 (a1 blob(1024), a2 blob(1024));
+insert into t1_1024 values (concat('1 - 00', repeat('x', 1018)), concat('2 - 00', repeat('x', 1018)));
+insert into t1_1024 values (concat('1 - 01', repeat('x', 1018)), concat('2 - 01', repeat('x', 1018)));
+create table t2_1024 (b1 blob(1024), b2 blob(1024));
+insert into t2_1024 values (concat('1 - 01', repeat('x', 1018)), concat('2 - 01', repeat('x', 1018)));
+insert into t2_1024 values (concat('1 - 02', repeat('x', 1018)), concat('2 - 02', repeat('x', 1018)));
+insert into t2_1024 values (concat('1 - 03', repeat('x', 1018)), concat('2 - 03', repeat('x', 1018)));
+insert into t2_1024 values (concat('1 - 04', repeat('x', 1018)), concat('2 - 04', repeat('x', 1018)));
+create procedure make_t1_indexes()
+begin
+create index it1i1 on t1 (a1);
+create index it1i2 on t1 (a2);
+create index it1i3 on t1 (a1, a2);
+create index it1_1024i1 on t1_1024 (a1(6));
+create index it1_1024i2 on t1_1024 (a2(6));
+create index it1_1024i3 on t1_1024 (a1(6), a2(6));
+end|
+create procedure make_t2_indexes()
+begin
+create index it2i1 on t2 (b1);
+create index it2i2 on t2 (b2);
+create index it2i3 on t2 (b1, b2);
+create unique index it2i4 on t2 (b1, b2, b3);
+create index it2_1024i1 on t2_1024 (b1(6));
+create index it2_1024i2 on t2_1024 (b2(6));
+create index it2_1024i3 on t2_1024 (b1(6), b2(6));
+end|
+create procedure remove_t1_indexes()
+begin
+drop index it1i1 on t1;
+drop index it1i2 on t1;
+drop index it1i3 on t1;
+drop index it1_1024i1 on t1_1024;
+drop index it1_1024i2 on t1_1024;
+drop index it1_1024i3 on t1_1024;
+end|
+create procedure remove_t2_indexes()
+begin
+drop index it2i1 on t2;
+drop index it2i2 on t2;
+drop index it2i3 on t2;
+drop index it2i4 on t2;
+drop index it2_1024i1 on t2_1024;
+drop index it2_1024i2 on t2_1024;
+drop index it2_1024i3 on t2_1024;
+end|
+create procedure add_materialization_data()
+begin
+insert into t1 values ('1 - 03', '2 - 03', '3 - 03', 3);
+insert into t1 values ('1 - 04', '2 - 04', '3 - 04', 4);
+insert into t1 values ('1 - 05', '2 - 05', '3 - 05', 5);
+insert into t1 values ('1 - 06', '2 - 06', '3 - 06', 6);
+insert into t1 values ('1 - 07', '2 - 07', '3 - 07', 7);
+insert into t1_1024 values (concat('1 - 03', repeat('x', 1018)), concat('2 - 03', repeat('x', 1018)));
+end|
+create procedure delete_materialization_data()
+begin
+delete from t1 where a1 >= '1 - 03';
+delete from t1_1024 where a1 >= '1 - 03';
+end|
+create procedure set_all_columns_not_null()
+begin
+alter table t1 modify a1 char(8) not null, modify a2 char(8) not null, modify a3 char(8) not null;
+alter table t2 modify b1 char(8) not null, modify b2 char(8) not null, modify b3 char(8) not null;
+end|
+create procedure set_all_columns_nullable()
+begin
+alter table t1 modify a1 char(8) null, modify a2 char(8) null, modify a3 char(8) null;
+alter table t2 modify b1 char(8) null, modify b2 char(8) null, modify b3 char(8) null;
+end|
+/******************************************************************************
+1. Both materialization and in-to-exists are ON, make a cost-based choice.
+******************************************************************************/
set @@optimizer_switch='materialization=on,in_to_exists=on';
-1.1 In-to-exists is cheaper
-set @@optimizer_switch='semijoin=off';
-explain extended
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,<exists>(select `test`.`t2`.`b1` from `test`.`t2` where ((`test`.`t2`.`b1` > '0') and (<cache>(`test`.`t1`.`a1`) = `test`.`t2`.`b1`))))
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
+/* 1.1 In-to-exists is cheaper */
+call make_t1_indexes();
+/* 1.1.1 non-indexed table access */
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
a1 a2
+1 - 00 2 - 00
1 - 01 2 - 01
1 - 02 2 - 02
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),<exists>(select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where ((`test`.`t2`.`b1` > '0') and (<cache>(`test`.`t1`.`a1`) = `test`.`t2`.`b1`) and (<cache>(`test`.`t1`.`a2`) = `test`.`t2`.`b2`))))
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i2 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+3 DEPENDENT UNION t3 ALL NULL NULL NULL NULL 5 Using where
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref it1i1,it1i3 it1i1 9 const 1 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
-set @@optimizer_switch='semijoin=on';
-explain extended
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; Using temporary; Using filesort
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,<exists>(select `test`.`t2`.`b1` from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1` having (<cache>(`test`.`t1`.`a1`) = <ref_null_helper>(`test`.`t2`.`b1`))))
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i1,it1i2,it1i3 it1i3 18 NULL 3 Using where; Using index
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 1.1.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 9 func 2 Using index; Using where; Full scan on NULL key
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i3 9 func 2 Using index; Using where; Full scan on NULL key
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i3 9 func 2 Using index; Using where; Full scan on NULL key
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i2 it2i2 9 func 2 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 18 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+a1 a2
+1 - 00 2 - 00
+1 - 01 2 - 01
+1 - 02 2 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i2 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 9 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 27 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref it1i1,it1i3 it1i1 9 const 1 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 9 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 18 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
-explain extended
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 9 NULL 1 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; Using temporary; Using filesort
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),<exists>(select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1`,`test`.`t2`.`b2` having (((<cache>(`test`.`t1`.`a1`) = `test`.`t2`.`b1`) or isnull(`test`.`t2`.`b1`)) and ((<cache>(`test`.`t1`.`a2`) = `test`.`t2`.`b2`) or isnull(`test`.`t2`.`b2`)) and <is_not_null_test>(`test`.`t2`.`b1`) and <is_not_null_test>(`test`.`t2`.`b2`))))
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 1 Using where; Using index
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 1 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 27 NULL 1 Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 27 NULL 1 Using where; Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 27 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 9 const 2 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i1,it1i2,it1i3 it1i3 18 NULL 3 Using where; Using index
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 1.1.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 8 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i4 8 func 1 Using index; Using where
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 16 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 ref it2i4,it2i1,it2i2,it2i3 it2i4 8 func 2 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 16 NULL 3 Using index
+2 DEPENDENT SUBQUERY t2 ref it2i4,it2i1,it2i2,it2i3 it2i4 8 func 2 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 16 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i2 it2i2 8 func 2 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 16 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 16 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
a1 a2
+1 - 00 2 - 00
1 - 01 2 - 01
1 - 02 2 - 02
-explain extended
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i2 8 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 8 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 24 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref it1i1,it1i3 it1i1 8 const 1 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 8 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 16 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 16 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 8 NULL 1 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 1 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
-2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; Using temporary; Using filesort
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),<exists>(select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1`,`test`.`t2`.`b2` having ((`test`.`t2`.`b2` < '2 - 04') and ((<cache>(`test`.`t1`.`a1`) = `test`.`t2`.`b1`) or isnull(`test`.`t2`.`b1`)) and ((<cache>(`test`.`t1`.`a2`) = `test`.`t2`.`b2`) or isnull(`test`.`t2`.`b2`)) and <is_not_null_test>(`test`.`t2`.`b1`) and <is_not_null_test>(`test`.`t2`.`b2`))))
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 1 Using where; Using index
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 24 NULL 1 Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 24 NULL 1 Using where; Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 24 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 8 const 5 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i1,it1i2,it1i3 it1i3 16 NULL 3 Using where; Using index
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 8 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 16 NULL 3 Using where; Using index
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+call set_all_columns_nullable();
+
+/* 1.2 Materialization is cheaper */
+call add_materialization_data();
+call remove_t1_indexes();
+/* 1.2.1 non-indexed table access */
+call remove_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+1 - 03 1
+1 - 04 0
+1 - 05 0
+1 - 06 0
+1 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
a1 a2
+1 - 00 2 - 00
1 - 01 2 - 01
1 - 02 2 - 02
+1 - 03 2 - 03
+1 - 04 2 - 04
+1 - 05 2 - 05
+1 - 06 2 - 06
+1 - 07 2 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+3 DEPENDENT UNION t3 ALL NULL NULL NULL NULL 5 Using where
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+2 - 03
-1.2 Materialization is cheaper
-1.2.1 Materialization is executable
-insert into t1 values ('1 - 03', '2 - 03');
-insert into t1 values ('1 - 04', '2 - 04');
-insert into t1 values ('1 - 05', '2 - 05');
-set @@optimizer_switch='semijoin=off';
-explain extended
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 6 100.00 Using where
-2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,`test`.`t1`.`a1` in ( <materialize> (select `test`.`t2`.`b1` from `test`.`t2` where (`test`.`t2`.`b1` > '0') ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`)))))
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
1 - 03 2 - 03
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 6 100.00 Using where
-2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),(`test`.`t1`.`a1`,`test`.`t1`.`a2`) in ( <materialize> (select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (`test`.`t2`.`b1` > '0') ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`) and (`test`.`t1`.`a2` = `materialized subselect`.`b2`)))))
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+1 - 03
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 1.2.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 9 NULL 5 Using where; Using index
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+1 - 03 1
+1 - 04 0
+1 - 05 0
+1 - 06 0
+1 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 index it2i4,it2i1,it2i2,it2i3 it2i3 18 NULL 5 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 range it2i4,it2i1,it2i2,it2i3 it2i3 9 NULL 4 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i2 it2i2 9 NULL 5 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i2,it2i3 it2i3 18 NULL 5 Using where; Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+a1 a2
+1 - 00 2 - 00
+1 - 01 2 - 01
+1 - 02 2 - 02
+1 - 03 2 - 03
+1 - 04 2 - 04
+1 - 05 2 - 05
+1 - 06 2 - 06
+1 - 07 2 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 9 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 27 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+2 - 03
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ref it2i2 it2i2 9 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i2,it2i3 it2i4 27 NULL 5 Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
a1 a2
1 - 01 2 - 01
1 - 02 2 - 02
1 - 03 2 - 03
-set @@optimizer_switch='semijoin=on';
-explain extended
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 6 100.00 Using where
-2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; Using temporary; Using filesort
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a1`,`test`.`t1`.`a1` in ( <materialize> (select `test`.`t2`.`b1` from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1` ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`)))))
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 9 NULL 5 Using where; Using index
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 range NULL it2i4 27 NULL 6 Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 range NULL it2i4 27 NULL 6 Using where; Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 27 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 9 const 2 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+1 - 03
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 1.2.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 8 NULL 5 Using where; Using index
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+1 - 03 1
+1 - 04 0
+1 - 05 0
+1 - 06 0
+1 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 index it2i4,it2i1,it2i2,it2i3 it2i3 16 NULL 5 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 SUBQUERY t2 range it2i4,it2i1,it2i2,it2i3 it2i3 8 NULL 4 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i2 it2i2 8 NULL 5 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i2,it2i3 it2i3 16 NULL 5 Using where; Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
a1 a2
+1 - 00 2 - 00
1 - 01 2 - 01
1 - 02 2 - 02
1 - 03 2 - 03
-explain extended
+1 - 04 2 - 04
+1 - 05 2 - 05
+1 - 06 2 - 06
+1 - 07 2 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 8 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 24 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+2 - 03
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 ref it2i2 it2i2 8 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i2,it2i3 it2i4 24 NULL 5 Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+1 - 03 2 - 03
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 8 NULL 5 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 6 100.00 Using where
-2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; Using temporary; Using filesort
-Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),(`test`.`t1`.`a1`,`test`.`t1`.`a2`) in ( <materialize> (select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1`,`test`.`t2`.`b2` ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`) and (`test`.`t1`.`a2` = `materialized subselect`.`b2`)))))
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 5 Using where; Using index
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 range NULL it2i4 24 NULL 6 Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 range NULL it2i4 24 NULL 6 Using where; Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 24 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 8 const 5 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+1 - 03
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+call set_all_columns_nullable();
+/******************************************************************************
+2. Materialization is OFF, in-to-exists is ON, materialization is cheaper.
+******************************************************************************/
+set @@optimizer_switch='materialization=off,in_to_exists=on';
+/* 2.1 non-indexed table access */
+call remove_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+1 - 03 1
+1 - 04 0
+1 - 05 0
+1 - 06 0
+1 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
a1 a2
+1 - 00 2 - 00
1 - 01 2 - 01
1 - 02 2 - 02
1 - 03 2 - 03
-explain extended
+1 - 04 2 - 04
+1 - 05 2 - 05
+1 - 06 2 - 06
+1 - 07 2 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+3 DEPENDENT UNION t3 ALL NULL NULL NULL NULL 5 Using where
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+2 - 03
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+1 - 03 2 - 03
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
-id select_type table type possible_keys key key_len ref rows filtered Extra
-1 PRIMARY t1 ALL NULL NULL NULL NULL 6 100.00 Using where
-2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; Using temporary; Using filesort
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
Warnings:
-Note 1003 select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),(`test`.`t1`.`a1`,`test`.`t1`.`a2`) in ( <materialize> (select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1`,`test`.`t2`.`b2` having (`test`.`t2`.`b2` < '2 - 04') ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`) and (`test`.`t1`.`a2` = `materialized subselect`.`b2`)))))
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+1 - 03
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 2.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 9 func 2 Using index; Using where; Full scan on NULL key
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+1 - 03 1
+1 - 04 0
+1 - 05 0
+1 - 06 0
+1 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i3 9 func 2 Using index; Using where; Full scan on NULL key
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i3 9 func 2 Using index; Using where; Full scan on NULL key
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index_subquery it2i2 it2i2 9 func 2 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 18 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+a1 a2
+1 - 00 2 - 00
+1 - 01 2 - 01
+1 - 02 2 - 02
+1 - 03 2 - 03
+1 - 04 2 - 04
+1 - 05 2 - 05
+1 - 06 2 - 06
+1 - 07 2 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 9 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 27 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+2 - 03
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 9 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 18 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+1 - 03 2 - 03
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 9 NULL 1 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 1 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 1 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 27 NULL 1 Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 27 NULL 1 Using where; Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 27 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 9 const 2 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+1 - 03
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 2.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i4 8 func 1 Using index; Using where
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+1 - 03 1
+1 - 04 0
+1 - 05 0
+1 - 06 0
+1 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 ref it2i4,it2i1,it2i2,it2i3 it2i4 8 func 2 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8
+2 DEPENDENT SUBQUERY t2 ref it2i4,it2i1,it2i2,it2i3 it2i4 8 func 2 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+1 - 03 2 - 03 1
+1 - 04 2 - 04 0
+1 - 05 2 - 05 0
+1 - 06 2 - 06 0
+1 - 07 2 - 07 0
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index_subquery it2i2 it2i2 8 func 2 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 16 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
a1 a2
+1 - 00 2 - 00
1 - 01 2 - 01
1 - 02 2 - 02
1 - 03 2 - 03
-delete from t1 where a1 >= '1 - 03';
-1.2.2 Materialization is NOT executable, revert to in-to-exists
+1 - 04 2 - 04
+1 - 05 2 - 05
+1 - 06 2 - 06
+1 - 07 2 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 8 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 24 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+2 - 03
-2. Materialization is OFF, in-to-exists is ON
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 8 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
-set @@optimizer_switch='materialization=off,in_to_exists=on';
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i2,it2i3 it2i4 16 func,func 1 Using index; Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+1 - 03 2 - 03
-3. Materialization is ON, in-to-exists is OFF
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 8 NULL 1 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 1 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 1 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+1 - 03 2 - 03 3 - 03 3
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 24 NULL 1 Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 index NULL it2i4 24 NULL 1 Using where; Using index
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 24 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL NULL NULL NULL NULL 3 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+1 - 03x 2 - 03x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 8 const 5 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+1 - 03
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+1 - 03
+1 - 04
+1 - 05
+1 - 06
+1 - 07
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+call set_all_columns_nullable();
+/******************************************************************************
+3. Materialization is ON, in-to-exists is OFF, in-to-exists is cheaper.
+******************************************************************************/
set @@optimizer_switch='materialization=on,in_to_exists=off';
-3.1 Materialization is executable
-3.2 Materialization is NOT executable, revert to in-to-exists
+call delete_materialization_data();
+call make_t1_indexes();
+/* 3.1 non-indexed table access */
+call remove_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
-4. Edge cases
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
-4.0 Both materialization and in_to_exists cannot be off
-set @@optimizer_switch='materialization=off,in_to_exists=off';
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+a1 a2
+1 - 00 2 - 00
+1 - 01 2 - 01
+1 - 02 2 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i2 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+3 DEPENDENT UNION t3 ALL NULL NULL NULL NULL 5 Using where
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref it1i1,it1i3 it1i1 9 const 1 Using where; Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 ALL NULL NULL NULL NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i1,it1i2,it1i3 it1i3 18 NULL 3 Using where; Using index
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 3.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using index
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 9 NULL 5 Using where; Using index
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 5 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using index
+2 SUBQUERY t2 range it2i4,it2i1,it2i3 it2i3 9 NULL 4 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY t2 index NULL it2i2 9 NULL 5 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 5 Using where; Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+a1 a2
+1 - 00 2 - 00
+1 - 01 2 - 01
+1 - 02 2 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i2 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 9 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 27 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref it1i1,it1i3 it1i1 9 const 1 Using where; Using index
+2 SUBQUERY t2 ref it2i2 it2i2 9 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY t2 index NULL it2i4 27 NULL 5 Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 9 NULL 5 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 18 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 range NULL it2i4 27 NULL 6 Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 range NULL it2i4 27 NULL 6 Using where; Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 27 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
-4.1 Outer query with no tables
-4.2 Subquery with no tables
-4.3 optimize_cond detects FALSE where/having clause
-4.4 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const
-4.4 make_join_select detects impossible WHERE
-4.5 constant optimization detects "no matching row in const table"
-5. UPDATE/DELETE with subqueries
-drop table t1, t2, t3, t1i, t2i, t3i;
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 9 const 2 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i1,it1i2,it1i3 it1i3 18 NULL 3 Using where; Using index
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 9 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 18 NULL 3 Using where; Using index
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+/* 3.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+
+/* A. Subqueries in the SELECT clause. */
+explain
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 8 NULL 3 Using index
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 8 NULL 5 Using where; Using index
+select a1, a1 in (select b1 from t2 where b1 > '0') from t1;
+a1 a1 in (select b1 from t2 where b1 > '0')
+1 - 00 0
+1 - 01 1
+1 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 16 NULL 3 Using index
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 5 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+explain
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 16 NULL 3 Using index
+2 SUBQUERY t2 range it2i4,it2i1,it2i3 it2i3 8 NULL 4 Using where; Using index
+select a1, a2, (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9') from t1;
+a1 a2 (a1, a2) in (select b1, b2 from t2 where b1 > '0' and b1 < '9')
+1 - 00 2 - 00 0
+1 - 01 2 - 01 1
+1 - 02 2 - 02 1
+
+/*
+B. "Natural" examples of subqueries without grouping that
+cannot be flattened into semijoin.
+*/
+explain
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 16 NULL 3 Using where; Using index
+2 SUBQUERY t2 index NULL it2i2 8 NULL 5 Using index
+select a1 from t1 where a1 in (select b2 from t2) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 16 NULL 3 Using where; Using index
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 5 Using where; Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0') or a2 < '9';
+a1 a2
+1 - 00 2 - 00
+1 - 01 2 - 01
+1 - 02 2 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i2 8 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY t2 ref it2i2 it2i2 8 func 2 Using index
+3 DEPENDENT UNION t3 index NULL it2i4 24 NULL 5 Using where; Using index
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a2 from t1 where a2 in (select b2 from t2 UNION select b3 from t2 as t3);
+a2
+2 - 01
+2 - 02
+
+explain
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ref it1i1,it1i3 it1i1 8 const 1 Using where; Using index
+2 SUBQUERY t2 ref it2i2 it2i2 8 const 1 Using index condition
+select a1 from t1 where a1 = '1 - 02' and a1 in (select max(b1) from t2 where b2 = '2 - 02');
+a1
+1 - 02
+
+explain
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i3 16 NULL 3 Using where; Using index
+2 SUBQUERY t2 index NULL it2i4 24 NULL 5 Using index
+select a1, a2 from t1 where (a1, a2) in (select b1, b2 from t2 order by b3);
+a1 a2
+1 - 01 2 - 01
+1 - 02 2 - 02
+
+/* C. Subqueries in the WHERE clause with GROUP BY. */
+explain
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i1 8 NULL 5 Using where; Using index
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 index it2i4,it2i1,it2i3 it2i3 16 NULL 5 Using where; Using index
+select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+1 - 02 2 - 02 3 - 02 2
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 range NULL it2i4 24 NULL 6 Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 group by b1, b2, b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+explain
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 range NULL it2i4 24 NULL 6 Using where; Using index for group-by
+select * from t1 where (a1, a2, a3) in (select b1, b2, b3 from t2 where b3 = '3 - 02' group by b1, b2);
+a1 a2 a3 a4
+
+explain
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
+2 SUBQUERY t2 range it2i4,it2i1,it2i3 it2i4 24 NULL 2 Using where; Using index for group-by
+select * from t1 where (a1,a2,a3) in (select b1,b2,b3 from t2 where b1 = '1 - 01' group by b1,b2,b3);
+a1 a2 a3 a4
+1 - 01 2 - 01 3 - 01 1
+
+/*
+D. Subqueries for which materialization is not possible, and the
+optimizer reverts to in-to-exists.
+*/
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i3 it2_1024i3 9 func 1 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select b1 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 index_subquery it2_1024i1,it2_1024i2,it2_1024i3 it2_1024i1 9 func 2 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select b1, b2 from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+explain
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where a1 in (select substring(b1,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+explain
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1_1024 ALL it1_1024i2 NULL NULL NULL 2 Using where
+2 DEPENDENT SUBQUERY t2_1024 range it2_1024i1,it2_1024i3 it2_1024i1 9 NULL 4 Using where
+select left(a1,7), left(a2,7) from t1_1024 where (a1,a2) in (select substring(b1,1,1024), substring(b2,1,1024) from t2_1024 where b1 > '0') or a2 < '9';
+left(a1,7) left(a2,7)
+1 - 00x 2 - 00x
+1 - 01x 2 - 01x
+
+
+/* E. Edge cases. */
+
+/* E.1 Both materialization and in_to_exists cannot be off. */
+set @save_optimizer_switch=@@optimizer_switch;
+set @@optimizer_switch = 'materialization=off,in_to_exists=off';
+select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
+ERROR HY000: At least one of the 'in_to_exists' or 'materialization' optimizer_switch flags must be 'on'.
+set @@optimizer_switch = @save_optimizer_switch;
+/* E.2 Outer query without tables, always uses IN-TO-EXISTS. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY t2 index_subquery it2i4,it2i1,it2i3 it2i1 8 const 5 Using index; Using where
+select '1 - 03' in (select b1 from t2 where b1 > '0');
+'1 - 03' in (select b1 from t2 where b1 > '0')
+1
+/* E.3 Subqueries without tables. */
+explain
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i1,it1i2,it1i3 it1i3 16 NULL 3 Using where; Using index
+Warnings:
+Note 1249 Select 2 was reduced during optimization
+select a1 from t1 where a1 in (select '1 - 03') or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+UNION subqueries are currently limited to only use IN-TO-EXISTS.
+explain
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index NULL it1i1 8 NULL 3 Using where; Using index
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
+3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
+NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
+select a1 from t1 where a1 in (select '1 - 03' UNION select '1 - 02');
+a1
+1 - 02
+/* E.4 optimize_cond detects FALSE where/having clause. */
+explain
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 index it1i2 it1i3 16 NULL 3 Using where; Using index
+2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
+select a1 from t1 where a1 in (select b1 from t2 where b1 = b2 and b2 = '1 - 03' and b1 = '1 - 02' ) or a2 < '9';
+a1
+1 - 00
+1 - 01
+1 - 02
+/* E.5 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const. */
+TODO this test produces wrong result due to missing logic to handle the case
+when JOIN::optimize detects an empty subquery result.
+
+/* E.6 make_join_select detects impossible WHERE. *
+TODO
+/* E.7 constant optimization detects "no matching row in const table". */
+TODO
+/* E.8 Impossible WHERE noticed after reading const tables. */
+explain
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
+2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
+select '1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0');
+'1 - 03' in (select b1 from t2 where b1 > '0' and b1 < '0')
+0
+
+/* F. UPDATE/DELETE with subqueries. */
+
+TODO
+
+call set_all_columns_nullable();
+drop procedure make_t1_indexes;
+drop procedure make_t2_indexes;
+drop procedure remove_t1_indexes;
+drop procedure remove_t2_indexes;
+drop procedure add_materialization_data;
+drop procedure delete_materialization_data;
+drop procedure set_all_columns_not_null;
+drop procedure set_all_columns_nullable;
+drop table t1, t2, t1_1024, t2_1024;
diff --git a/mysql-test/t/subselect_mat.test b/mysql-test/t/subselect_mat.test
index 1c2869c628a..0b3a89336cc 100644
--- a/mysql-test/t/subselect_mat.test
+++ b/mysql-test/t/subselect_mat.test
@@ -48,7 +48,7 @@ insert into t2i select * from t2;
insert into t3i select * from t3;
# force the use of materialization
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
/******************************************************************************
* Simple tests.
@@ -111,22 +111,22 @@ select * from t1 where (a1, a2) in (select b1, min(b2) from t2i limit 1,1);
# test re-optimization/re-execution with different execution methods
# prepare once, exec with different modes
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
prepare st1 from
"select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1)";
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=on';
execute st1;
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
execute st1;
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=on';
prepare st1 from
"select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1)";
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
execute st1;
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=on';
execute st1;
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
# materialize the result of ORDER BY
# non-indexed fields
@@ -327,7 +327,7 @@ select * from t1 order by (select col from columns limit 1);
Test that BLOBs are not materialized (except when arguments of some functions).
*/
# force materialization to be always considered
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
set @prefix_len = 6;
# BLOB == 16 (small blobs that could be stored in HEAP tables)
@@ -680,7 +680,7 @@ insert into t2bit values (b'001', b'101');
insert into t2bit values (b'010', b'110');
insert into t2bit values (b'110', b'111');
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain extended select bin(a1), bin(a2)
from t1bit
@@ -718,7 +718,7 @@ drop table t1, t2, t3, t1i, t2i, t3i, columns;
/******************************************************************************
* Test the cache of the left operand of IN.
******************************************************************************/
-set @@optimizer_switch='semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
# Test that default values of Cached_item are not used for comparison
create table t1 (s1 int);
@@ -812,23 +812,28 @@ drop table t2;
create table t1 (a1 int key);
create table t2 (b1 int);
insert into t1 values (5);
-
+-- echo Only the last query returns correct result. Filed as BUG#40037.
# Query with group by, executed via materialization
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
# Query with group by, executed via IN=>EXISTS
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
explain select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
# Executed with materialization
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
+explain select min(a1) from t1 where 7 in (select b1 from t2);
+select min(a1) from t1 where 7 in (select b1 from t2);
+# Executed via IN=>EXISTS
+set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
explain select min(a1) from t1 where 7 in (select b1 from t2);
select min(a1) from t1 where 7 in (select b1 from t2);
# Executed with semi-join. Notice, this time we get a different result (NULL).
-# This is the only correct result of all four queries. This difference is
+# This is the only correct result of all five queries. This difference is
# filed as BUG#40037.
-set @@optimizer_switch='default,materialization=off';
+set @@optimizer_switch='materialization=off,in_to_exists=off,semijoin=on';
explain select min(a1) from t1 where 7 in (select b1 from t2);
select min(a1) from t1 where 7 in (select b1 from t2);
drop table t1,t2;
@@ -840,7 +845,7 @@ create table t1 (a char(2), b varchar(10));
insert into t1 values ('a', 'aaa');
insert into t1 values ('aa', 'aaaa');
-set @@optimizer_switch='default,semijoin=off';
+set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
explain select a,b from t1 where b in (select a from t1);
select a,b from t1 where b in (select a from t1);
prepare st1 from "select a,b from t1 where b in (select a from t1)";
@@ -861,7 +866,7 @@ CREATE TABLE t2 LIKE t1;
INSERT INTO t2 VALUES (1, 1.789);
INSERT INTO t2 VALUES (13, 1.454);
-SET @@optimizer_switch='default,semijoin=on,materialization=on';
+SET @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
EXPLAIN SELECT COUNT(*) FROM t1 WHERE (f1,f2) IN (SELECT f1,f2 FROM t2);
SELECT COUNT(*) FROM t1 WHERE (f1,f2) IN (SELECT f1,f2 FROM t2);
@@ -883,7 +888,7 @@ INSERT INTO t1 VALUES (1,'o','ffff','ffff','ffoo'),(2,'f','ffff','ffff','ffff');
CREATE TABLE t2 LIKE t1;
INSERT INTO t2 VALUES (1,'i','iiii','iiii','iiii'),(2,'f','ffff','ffff','ffff');
-SET @@optimizer_switch='default,semijoin=on,materialization=on';
+SET @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=on';
EXPLAIN SELECT pk FROM t1 WHERE (a) IN (SELECT a FROM t2 WHERE pk > 0);
SELECT pk FROM t1 WHERE (a) IN (SELECT a FROM t2 WHERE pk > 0);
SELECT pk FROM t1 WHERE (b,c,d) IN (SELECT b,c,d FROM t2 WHERE pk > 0);
@@ -900,7 +905,7 @@ create table t3(i int);
insert into t3 values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
select * from t1 where t1.i in (select t2.i from t2 join t3 where t2.i + t3.i = 5);
set @save_optimizer_switch=@@optimizer_switch;
-set session optimizer_switch='materialization=off';
+set session optimizer_switch='materialization=off,in_to_exists=on';
select * from t1 where t1.i in (select t2.i from t2 join t3 where t2.i + t3.i = 5);
set session optimizer_switch=@save_optimizer_switch;
drop table t1, t2, t3;
diff --git a/mysql-test/t/subselect_mat_cost.test b/mysql-test/t/subselect_mat_cost.test
index 702eac508d6..a268322dc5f 100644
--- a/mysql-test/t/subselect_mat_cost.test
+++ b/mysql-test/t/subselect_mat_cost.test
@@ -4,136 +4,200 @@
#
--disable_warnings
-drop table if exists t1, t2, t3, t1i, t2i, t3i;
+drop table if exists t1, t2, t1_1024, t2_1024;
+drop procedure if exists make_t1_indexes;
+drop procedure if exists make_t2_indexes;
+drop procedure if exists remove_t1_indexes;
+drop procedure if exists remove_t2_indexes;
+drop procedure if exists add_materialization_data;
+drop procedure if exists delete_materialization_data;
+drop procedure if exists set_all_columns_not_null;
+drop procedure if exists set_all_columns_nullable;
--enable_warnings
-create table t1 (a1 char(8), a2 char(8));
-create table t2 (b1 char(8), b2 char(8));
-create table t3 (c1 char(8), c2 char(8));
-
---echo Unindexed tables
-insert into t1 values ('1 - 00', '2 - 00');
-insert into t1 values ('1 - 01', '2 - 01');
-insert into t1 values ('1 - 02', '2 - 02');
-
-insert into t2 values ('1 - 01', '2 - 01');
-insert into t2 values ('1 - 01', '2 - 01');
-insert into t2 values ('1 - 02', '2 - 02');
-insert into t2 values ('1 - 02', '2 - 02');
-insert into t2 values ('1 - 03', '2 - 03');
-
-insert into t3 values ('1 - 01', '2 - 01');
-insert into t3 values ('1 - 02', '2 - 02');
-insert into t3 values ('1 - 03', '2 - 03');
-insert into t3 values ('1 - 04', '2 - 04');
-
---echo Indexed tables
-create table t1i (a1 char(8), a2 char(8));
-create table t2i (b1 char(8), b2 char(8));
-create table t3i (c1 char(8), c2 char(8));
-create index it1i1 on t1i (a1);
-create index it1i2 on t1i (a2);
-create index it1i3 on t1i (a1, a2);
-
-create index it2i1 on t2i (b1);
-create index it2i2 on t2i (b2);
-create index it2i3 on t2i (b1, b2);
-
-create index it3i1 on t3i (c1);
-create index it3i2 on t3i (c2);
-create index it3i3 on t3i (c1, c2);
-
-insert into t1i select * from t1;
-insert into t2i select * from t2;
-insert into t3i select * from t3;
-
---echo
---echo 1. Both materialization and in-to-exists are possible to execute
---echo
+create table t1 (a1 char(8), a2 char(8), a3 char(8), a4 int);
+insert into t1 values ('1 - 00', '2 - 00', '3 - 00', 0);
+insert into t1 values ('1 - 01', '2 - 01', '3 - 01', 1);
+insert into t1 values ('1 - 02', '2 - 02', '3 - 02', 2);
+
+create table t2 (b1 char(8), b2 char(8), b3 char(8), b4 int);
+insert into t2 values ('1 - 01', '2 - 01', '3 - 01', 1);
+insert into t2 values ('1 - 01', '2 - 01', '3 - 02', 2);
+insert into t2 values ('1 - 02', '2 - 02', '3 - 03', 3);
+insert into t2 values ('1 - 02', '2 - 02', '3 - 04', 4);
+insert into t2 values ('1 - 03', '2 - 03', '3 - 05', 5);
+
+create table t1_1024 (a1 blob(1024), a2 blob(1024));
+insert into t1_1024 values (concat('1 - 00', repeat('x', 1018)), concat('2 - 00', repeat('x', 1018)));
+insert into t1_1024 values (concat('1 - 01', repeat('x', 1018)), concat('2 - 01', repeat('x', 1018)));
+
+create table t2_1024 (b1 blob(1024), b2 blob(1024));
+insert into t2_1024 values (concat('1 - 01', repeat('x', 1018)), concat('2 - 01', repeat('x', 1018)));
+insert into t2_1024 values (concat('1 - 02', repeat('x', 1018)), concat('2 - 02', repeat('x', 1018)));
+insert into t2_1024 values (concat('1 - 03', repeat('x', 1018)), concat('2 - 03', repeat('x', 1018)));
+insert into t2_1024 values (concat('1 - 04', repeat('x', 1018)), concat('2 - 04', repeat('x', 1018)));
+
+delimiter |;
+create procedure make_t1_indexes()
+begin
+ create index it1i1 on t1 (a1);
+ create index it1i2 on t1 (a2);
+ create index it1i3 on t1 (a1, a2);
+ create index it1_1024i1 on t1_1024 (a1(6));
+ create index it1_1024i2 on t1_1024 (a2(6));
+ create index it1_1024i3 on t1_1024 (a1(6), a2(6));
+end|
+
+create procedure make_t2_indexes()
+begin
+ create index it2i1 on t2 (b1);
+ create index it2i2 on t2 (b2);
+ create index it2i3 on t2 (b1, b2);
+ create unique index it2i4 on t2 (b1, b2, b3);
+ create index it2_1024i1 on t2_1024 (b1(6));
+ create index it2_1024i2 on t2_1024 (b2(6));
+ create index it2_1024i3 on t2_1024 (b1(6), b2(6));
+end|
+
+create procedure remove_t1_indexes()
+begin
+ drop index it1i1 on t1;
+ drop index it1i2 on t1;
+ drop index it1i3 on t1;
+ drop index it1_1024i1 on t1_1024;
+ drop index it1_1024i2 on t1_1024;
+ drop index it1_1024i3 on t1_1024;
+end|
+
+create procedure remove_t2_indexes()
+begin
+ drop index it2i1 on t2;
+ drop index it2i2 on t2;
+ drop index it2i3 on t2;
+ drop index it2i4 on t2;
+ drop index it2_1024i1 on t2_1024;
+ drop index it2_1024i2 on t2_1024;
+ drop index it2_1024i3 on t2_1024;
+end|
+
+create procedure add_materialization_data()
+begin
+insert into t1 values ('1 - 03', '2 - 03', '3 - 03', 3);
+insert into t1 values ('1 - 04', '2 - 04', '3 - 04', 4);
+insert into t1 values ('1 - 05', '2 - 05', '3 - 05', 5);
+insert into t1 values ('1 - 06', '2 - 06', '3 - 06', 6);
+insert into t1 values ('1 - 07', '2 - 07', '3 - 07', 7);
+insert into t1_1024 values (concat('1 - 03', repeat('x', 1018)), concat('2 - 03', repeat('x', 1018)));
+end|
+
+create procedure delete_materialization_data()
+begin
+delete from t1 where a1 >= '1 - 03';
+delete from t1_1024 where a1 >= '1 - 03';
+end|
+
+create procedure set_all_columns_not_null()
+begin
+alter table t1 modify a1 char(8) not null, modify a2 char(8) not null, modify a3 char(8) not null;
+alter table t2 modify b1 char(8) not null, modify b2 char(8) not null, modify b3 char(8) not null;
+end|
+
+create procedure set_all_columns_nullable()
+begin
+alter table t1 modify a1 char(8) null, modify a2 char(8) null, modify a3 char(8) null;
+alter table t2 modify b1 char(8) null, modify b2 char(8) null, modify b3 char(8) null;
+end|
+
+delimiter ;|
+-- echo
+
+-- echo /******************************************************************************
+-- echo 1. Both materialization and in-to-exists are ON, make a cost-based choice.
+-- echo ******************************************************************************/
set @@optimizer_switch='materialization=on,in_to_exists=on';
+-- echo
+-- echo /* 1.1 In-to-exists is cheaper */
+call make_t1_indexes();
---echo
---echo 1.1 In-to-exists is cheaper
-
-set @@optimizer_switch='semijoin=off';
-explain extended
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
-
-set @@optimizer_switch='semijoin=on';
-explain extended
-select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
-select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
-
---echo
---echo 1.2 Materialization is cheaper
-
---echo 1.2.1 Materialization is executable
-
-insert into t1 values ('1 - 03', '2 - 03');
-insert into t1 values ('1 - 04', '2 - 04');
-insert into t1 values ('1 - 05', '2 - 05');
-
-set @@optimizer_switch='semijoin=off';
-explain extended
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
-select * from t1 where a1 in (select b1 from t2 where b1 > '0');
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0');
-
-set @@optimizer_switch='semijoin=on';
-explain extended
-select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
-select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
-explain extended
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
-select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2 having b2 < '2 - 04');
+-- echo /* 1.1.1 non-indexed table access */
+-- source include/subselect_mat_cost.inc
-delete from t1 where a1 >= '1 - 03';
+-- echo /* 1.1.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+-- source include/subselect_mat_cost.inc
---echo 1.2.2 Materialization is NOT executable, revert to in-to-exists
+-- echo /* 1.1.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+-- source include/subselect_mat_cost.inc
+call set_all_columns_nullable();
---echo
---echo 2. Materialization is OFF, in-to-exists is ON
---echo
-set @@optimizer_switch='materialization=off,in_to_exists=on';
+-- echo
+-- echo /* 1.2 Materialization is cheaper */
+# make materialization cheaper
+call add_materialization_data();
+call remove_t1_indexes();
---echo
---echo 3. Materialization is ON, in-to-exists is OFF
---echo
-set @@optimizer_switch='materialization=on,in_to_exists=off';
+-- echo /* 1.2.1 non-indexed table access */
+call remove_t2_indexes();
+-- source include/subselect_mat_cost.inc
+
+-- echo /* 1.2.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+-- source include/subselect_mat_cost.inc
---echo 3.1 Materialization is executable
---echo 3.2 Materialization is NOT executable, revert to in-to-exists
+-- echo /* 1.2.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+-- source include/subselect_mat_cost.inc
+call set_all_columns_nullable();
---echo
---echo 4. Edge cases
---echo
---echo 4.0 Both materialization and in_to_exists cannot be off
-set @@optimizer_switch='materialization=off,in_to_exists=off';
---error ER_ILLEGAL_SUBQUERY_OPTIMIZER_SWITCHES
-select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
---echo 4.1 Outer query with no tables
---echo 4.2 Subquery with no tables
---echo 4.3 optimize_cond detects FALSE where/having clause
---echo 4.4 opt_sum_query detects no matching min/max row or substitutes MIN/MAX with a const
---echo 4.4 make_join_select detects impossible WHERE
---echo 4.5 constant optimization detects "no matching row in const table"
+-- echo /******************************************************************************
+-- echo 2. Materialization is OFF, in-to-exists is ON, materialization is cheaper.
+-- echo ******************************************************************************/
+set @@optimizer_switch='materialization=off,in_to_exists=on';
+
+-- echo /* 2.1 non-indexed table access */
+call remove_t2_indexes();
+-- source include/subselect_mat_cost.inc
+
+-- echo /* 2.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+-- source include/subselect_mat_cost.inc
---echo 5. UPDATE/DELETE with subqueries
+-- echo /* 2.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+-- source include/subselect_mat_cost.inc
+call set_all_columns_nullable();
-drop table t1, t2, t3, t1i, t2i, t3i;
+
+-- echo /******************************************************************************
+-- echo 3. Materialization is ON, in-to-exists is OFF, in-to-exists is cheaper.
+-- echo ******************************************************************************/
+set @@optimizer_switch='materialization=on,in_to_exists=off';
+# make IN-TO-EXISTS cheaper
+call delete_materialization_data();
+call make_t1_indexes();
+
+-- echo /* 3.1 non-indexed table access */
+call remove_t2_indexes();
+-- source include/subselect_mat_cost.inc
+
+-- echo /* 3.2 indexed table access, nullabale columns. */
+call make_t2_indexes();
+-- source include/subselect_mat_cost.inc
+
+-- echo /* 3.3 indexed table access, non-nullabale columns. */
+call set_all_columns_not_null();
+-- source include/subselect_mat_cost.inc
+call set_all_columns_nullable();
+
+
+drop procedure make_t1_indexes;
+drop procedure make_t2_indexes;
+drop procedure remove_t1_indexes;
+drop procedure remove_t2_indexes;
+drop procedure add_materialization_data;
+drop procedure delete_materialization_data;
+drop procedure set_all_columns_not_null;
+drop procedure set_all_columns_nullable;
+drop table t1, t2, t1_1024, t2_1024;
diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc
index 86d7f606212..25d3aa29904 100644
--- a/sql/opt_subselect.cc
+++ b/sql/opt_subselect.cc
@@ -3403,6 +3403,19 @@ int rewrite_to_index_subquery_engine(JOIN *join)
/*
is this simple IN subquery?
*/
+ /* TODO: In order to use these more efficient subquery engines in more cases,
+ the following problems need to be solved:
+ - the code that removes GROUP BY (group_list), also adds an ORDER BY
+ (order), thus GROUP BY queries (almost?) never pass through this branch.
+ Solution: remove the test below '!join->order', because we remove the
+ ORDER clase for subqueries anyway.
+ - in order to set a more efficient engine, the optimizer needs to both
+ decide to remove GROUP BY, *and* select one of the JT_[EQ_]REF[_OR_NULL]
+ access methods, *and* loose scan should be more expensive or
+ inapliccable. When is that possible?
+ - Consider expanding the applicability of this rewrite for loose scan
+ for group by queries.
+ */
if (!join->group_list && !join->order &&
join->unit->item &&
join->unit->item->substype() == Item_subselect::IN_SUBS &&
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index ee99f98c1ea..4e5dc9074f2 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -19372,12 +19372,6 @@ int JOIN::reoptimize(Item *added_where, table_map join_tables,
DYNAMIC_ARRAY added_keyuse;
SARGABLE_PARAM *sargables= 0; /* Used only as a dummy parameter. */
- if (my_init_dynamic_array(&added_keyuse, sizeof(KEYUSE), 20, 64))
- {
- delete_dynamic(&added_keyuse);
- return 1;
- }
-
/* Re-run the REF optimizer to take into account the new conditions. */
if (update_ref_and_keys(thd, &added_keyuse, join_tab, tables, added_where,
~outer_join, select_lex, &sargables))