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, 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');
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');
/* 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
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');
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');
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
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');
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');
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');
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 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');
set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
EXPLAIN EXTENDED SELECT pk
FROM t1
WHERE c1 IN
(SELECT t1a.c1
FROM (t1b JOIN t2 ON t2.c3 = t1b.c4) LEFT JOIN
t1a ON (t1a.c2 = t1b.pk AND 2)
WHERE t1.pk) ;
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 index c3 c3 9 NULL 2 100.00 Using index
2 DEPENDENT SUBQUERY t1b ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer
2 DEPENDENT SUBQUERY t1a ref c2 c2 5 test.t1b.pk 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.pk' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` where <expr_cache><`test`.`t1`.`c1`,(`test`.`t1`.`pk` and (`test`.`t1b`.`c4` = `test`.`t2`.`c3`) and (<cache>(`test`.`t1`.`c1`) = `test`.`t1a`.`c1`))>(<in_optimizer>(`test`.`t1`.`c1`,<exists>(select `test`.`t1a`.`c1` from `test`.`t1b` join `test`.`t2` left join `test`.`t1a` on((2 and (`test`.`t1a`.`c2` = `test`.`t1b`.`pk`))) where (`test`.`t1`.`pk` and (`test`.`t1b`.`c4` = `test`.`t2`.`c3`) and (<cache>(`test`.`t1`.`c1`) = `test`.`t1a`.`c1`)))))