1
drop table if exists t0, t1, t2, t10, t11, t12;
2
create table t0 (a int);
3
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4
create table t1(a int, b int);
5
insert into t1 values (0,0),(1,1),(2,2);
6
create table t2 as select * from t1;
7
create table t11(a int, b int);
8
create table t10 (pk int, a int, primary key(pk));
9
insert into t10 select a,a from t0;
10
create table t12 like t10;
11
insert into t12 select * from t10;
12
Flattened because of dependency, t10=func(t1)
13
explain select * from t1 where a in (select pk from t10);
14
id select_type table type possible_keys key key_len ref rows Extra
15
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
16
1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
17
select * from t1 where a in (select pk from t10);
22
A confluent case of dependency
23
explain select * from t1 where a in (select a from t10 where pk=12);
24
id select_type table type possible_keys key key_len ref rows Extra
25
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
26
1 PRIMARY t10 const PRIMARY PRIMARY 4 const 1 Using where
27
select * from t1 where a in (select a from t10 where pk=12);
30
explain select * from t1 where a in (select a from t11);
31
id select_type table type possible_keys key key_len ref rows Extra
32
1 PRIMARY t11 ALL NULL NULL NULL NULL 1 Start temporary
33
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where; End temporary; Using join buffer
34
select * from t1 where a in (select a from t11);
36
explain select * from t1 where a in (select pk from t10) and b in (select pk from t10);
37
id select_type table type possible_keys key key_len ref rows Extra
38
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
39
1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
40
1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 Using index
41
select * from t1 where a in (select pk from t10) and b in (select pk from t10);
46
flattening a nested subquery
47
explain select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
48
id select_type table type possible_keys key key_len ref rows Extra
49
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
50
1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1
51
1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 Using index
52
select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
57
flattening subquery w/ several tables
58
explain extended select * from t1 where a in (select t10.pk from t10, t12 where t12.pk=t10.a);
59
id select_type table type possible_keys key key_len ref rows filtered Extra
60
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
61
1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00
62
1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 100.00 Using index
64
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from (`test`.`t10` join `test`.`t12`) join `test`.`t1` where ((`test`.`t10`.`pk` = `test`.`t1`.`a`) and (`test`.`t12`.`pk` = `test`.`t10`.`a`))
65
subqueries within outer joins go into ON expr.
66
insert into t1 select (A.A + 10 * B.A),1 from t0 A, t0 B;
67
explain extended select * from t1 where a in (select pk from t10 where pk<3);
68
id select_type table type possible_keys key key_len ref rows filtered Extra
69
1 PRIMARY t1 ALL NULL NULL NULL NULL 103 100.00 Using where
70
1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00 Using index
72
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from (`test`.`t10`) join `test`.`t1` where ((`test`.`t10`.`pk` = `test`.`t1`.`a`) and (`test`.`t1`.`a` < 3))
74
drop table t10, t11, t12;