~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to mysql-test/r/subquery_mat_all.result

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
set optimizer_switch='semijoin=on,materialization=on,firstmatch=on,loosescan=on,index_condition_pushdown=on,mrr=on';
 
2
set @old_opt_switch=@@optimizer_switch;
 
3
set optimizer_switch='subquery_materialization_cost_based=off';
 
4
drop table if exists t1, t2, t3, t1i, t2i, t3i;
 
5
drop view if exists v1, v2, v1m, v2m;
 
6
create table t1 (a1 char(8), a2 char(8));
 
7
create table t2 (b1 char(8), b2 char(8));
 
8
create table t3 (c1 char(8), c2 char(8));
 
9
insert into t1 values ('1 - 00', '2 - 00');
 
10
insert into t1 values ('1 - 01', '2 - 01');
 
11
insert into t1 values ('1 - 02', '2 - 02');
 
12
insert into t2 values ('1 - 01', '2 - 01');
 
13
insert into t2 values ('1 - 01', '2 - 01');
 
14
insert into t2 values ('1 - 02', '2 - 02');
 
15
insert into t2 values ('1 - 02', '2 - 02');
 
16
insert into t2 values ('1 - 03', '2 - 03');
 
17
insert into t3 values ('1 - 01', '2 - 01');
 
18
insert into t3 values ('1 - 02', '2 - 02');
 
19
insert into t3 values ('1 - 03', '2 - 03');
 
20
insert into t3 values ('1 - 04', '2 - 04');
 
21
create table t1i (a1 char(8), a2 char(8));
 
22
create table t2i (b1 char(8), b2 char(8));
 
23
create table t3i (c1 char(8), c2 char(8));
 
24
create index it1i1 on t1i (a1);
 
25
create index it1i2 on t1i (a2);
 
26
create index it1i3 on t1i (a1, a2);
 
27
create index it2i1 on t2i (b1);
 
28
create index it2i2 on t2i (b2);
 
29
create index it2i3 on t2i (b1, b2);
 
30
create index it3i1 on t3i (c1);
 
31
create index it3i2 on t3i (c2);
 
32
create index it3i3 on t3i (c1, c2);
 
33
insert into t1i select * from t1;
 
34
insert into t2i select * from t2;
 
35
insert into t3i select * from t3;
 
36
/******************************************************************************
 
37
* Simple tests.
 
38
******************************************************************************/
 
39
# non-indexed nullable fields
 
40
explain extended
 
41
select * from t1 where a1 in (select b1 from t2 where b1 > '0');
 
42
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
43
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
44
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
45
Warnings:
 
46
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t1`.`a1` > '0'))
 
47
select * from t1 where a1 in (select b1 from t2 where b1 > '0');
 
48
a1      a2
 
49
1 - 01  2 - 01
 
50
1 - 02  2 - 02
 
51
explain extended
 
52
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
 
53
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
54
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
55
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
56
Warnings:
 
57
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t1`.`a1` > '0'))
 
58
select * from t1 where a1 in (select b1 from t2 where b1 > '0' group by b1);
 
59
a1      a2
 
60
1 - 01  2 - 01
 
61
1 - 02  2 - 02
 
62
explain extended
 
63
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
 
64
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
65
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
66
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
67
Warnings:
 
68
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t1`.`a1` > '0'))
 
69
select * from t1 where (a1, a2) in (select b1, b2 from t2 where b1 > '0' group by b1, b2);
 
70
a1      a2
 
71
1 - 01  2 - 01
 
72
1 - 02  2 - 02
 
73
explain extended
 
74
select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1);
 
75
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
76
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
77
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; Using temporary; Using filesort
 
78
Warnings:
 
79
Note    1003    /* select#1 */ 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#2 */ select `test`.`t2`.`b1`,min(`test`.`t2`.`b2`) from `test`.`t2` where (`test`.`t2`.`b1` > '0') group by `test`.`t2`.`b1` having 1 ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on <auto_key> where ((`test`.`t1`.`a1` = `materialized-subquery`.`b1`) and (`test`.`t1`.`a2` = `materialized-subquery`.`min(b2)`)))))
 
80
select * from t1 where (a1, a2) in (select b1, min(b2) from t2 where b1 > '0' group by b1);
 
81
a1      a2
 
82
1 - 01  2 - 01
 
83
1 - 02  2 - 02
 
84
explain extended
 
85
select * from t1i where a1 in (select b1 from t2i where b1 > '0');
 
86
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
87
1       SIMPLE  t2i     index   it2i1,it2i3     it2i1   9       NULL    5       50.00   Using where; Using index; LooseScan
 
88
1       SIMPLE  t1i     ref     it1i1,it1i3     it1i1   9       test.t2i.b1     1       100.00  NULL
 
89
Warnings:
 
90
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) where ((`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0'))
 
91
select * from t1i where a1 in (select b1 from t2i where b1 > '0');
 
92
a1      a2
 
93
1 - 01  2 - 01
 
94
1 - 02  2 - 02
 
95
explain extended
 
96
select * from t1i where a1 in (select b1 from t2i where b1 > '0' group by b1);
 
97
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
98
1       SIMPLE  t2i     index   it2i1,it2i3     it2i1   9       NULL    5       50.00   Using where; Using index; LooseScan
 
99
1       SIMPLE  t1i     ref     it1i1,it1i3     it1i1   9       test.t2i.b1     1       100.00  NULL
 
100
Warnings:
 
101
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) where ((`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0'))
 
102
select * from t1i where a1 in (select b1 from t2i where b1 > '0' group by b1);
 
103
a1      a2
 
104
1 - 01  2 - 01
 
105
1 - 02  2 - 02
 
106
explain extended
 
107
select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0');
 
108
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
109
1       SIMPLE  t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       50.00   Using where; Using index; LooseScan
 
110
1       SIMPLE  t1i     ref     it1i1,it1i2,it1i3       it1i1   9       test.t2i.b1     1       100.00  Using where
 
111
Warnings:
 
112
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0'))
 
113
select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0');
 
114
a1      a2
 
115
1 - 01  2 - 01
 
116
1 - 02  2 - 02
 
117
explain extended
 
118
select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0' group by b1, b2);
 
119
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
120
1       SIMPLE  t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       50.00   Using where; Using index; LooseScan
 
121
1       SIMPLE  t1i     ref     it1i1,it1i2,it1i3       it1i1   9       test.t2i.b1     1       100.00  Using where
 
122
Warnings:
 
123
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0'))
 
124
select * from t1i where (a1, a2) in (select b1, b2 from t2i where b1 > '0' group by b1, b2);
 
125
a1      a2
 
126
1 - 01  2 - 01
 
127
1 - 02  2 - 02
 
128
explain extended
 
129
select * from t1i where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
 
130
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
131
1       PRIMARY t1i     index   NULL    it1i3   18      NULL    3       100.00  Using where; Using index
 
132
2       SUBQUERY        t2i     range   it2i1,it2i3     it2i3   18      NULL    3       100.00  Using where; Using index for group-by
 
133
Warnings:
 
134
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` where <in_optimizer>((`test`.`t1i`.`a1`,`test`.`t1i`.`a2`),(`test`.`t1i`.`a1`,`test`.`t1i`.`a2`) in ( <materialize> (/* select#2 */ select `test`.`t2i`.`b1`,min(`test`.`t2i`.`b2`) from `test`.`t2i` where (`test`.`t2i`.`b1` > '0') group by `test`.`t2i`.`b1` having 1 ), <primary_index_lookup>(`test`.`t1i`.`a1` in <temporary table> on <auto_key> where ((`test`.`t1i`.`a1` = `materialized-subquery`.`b1`) and (`test`.`t1i`.`a2` = `materialized-subquery`.`min(b2)`)))))
 
135
select * from t1i where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
 
136
a1      a2
 
137
1 - 01  2 - 01
 
138
1 - 02  2 - 02
 
139
explain extended
 
140
select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1);
 
141
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
142
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
143
2       SUBQUERY        t2i     range   it2i1,it2i3     it2i3   9       NULL    3       100.00  Using index for group-by
 
144
Warnings:
 
145
Note    1003    /* select#1 */ 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#2 */ select `test`.`t2i`.`b1`,max(`test`.`t2i`.`b2`) from `test`.`t2i` group by `test`.`t2i`.`b1` having 1 ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on <auto_key> where ((`test`.`t1`.`a1` = `materialized-subquery`.`b1`) and (`test`.`t1`.`a2` = `materialized-subquery`.`max(b2)`)))))
 
146
select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1);
 
147
a1      a2
 
148
1 - 01  2 - 01
 
149
1 - 02  2 - 02
 
150
prepare st1 from "explain select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1)";
 
151
execute st1;
 
152
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
153
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
154
2       SUBQUERY        t2i     range   it2i1,it2i3     it2i3   9       NULL    3       Using index for group-by
 
155
execute st1;
 
156
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
157
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
158
2       SUBQUERY        t2i     range   it2i1,it2i3     it2i3   9       NULL    3       Using index for group-by
 
159
prepare st2 from "select * from t1 where (a1, a2) in (select b1, max(b2) from t2i group by b1)";
 
160
execute st2;
 
161
a1      a2
 
162
1 - 01  2 - 01
 
163
1 - 02  2 - 02
 
164
execute st2;
 
165
a1      a2
 
166
1 - 01  2 - 01
 
167
1 - 02  2 - 02
 
168
explain extended
 
169
select * from t1 where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
 
170
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
171
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
172
2       SUBQUERY        t2i     range   it2i1,it2i3     it2i3   18      NULL    3       100.00  Using where; Using index for group-by
 
173
Warnings:
 
174
Note    1003    /* select#1 */ 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#2 */ select `test`.`t2i`.`b1`,min(`test`.`t2i`.`b2`) from `test`.`t2i` where (`test`.`t2i`.`b1` > '0') group by `test`.`t2i`.`b1` having 1 ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on <auto_key> where ((`test`.`t1`.`a1` = `materialized-subquery`.`b1`) and (`test`.`t1`.`a2` = `materialized-subquery`.`min(b2)`)))))
 
175
select * from t1 where (a1, a2) in (select b1, min(b2) from t2i where b1 > '0' group by b1);
 
176
a1      a2
 
177
1 - 01  2 - 01
 
178
1 - 02  2 - 02
 
179
select * from t1 where (a1, a2) in (select b1, min(b2) from t2i limit 1,1);
 
180
ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
 
181
explain extended
 
182
select * from t1 where (a1, a2) in (select b1, b2 from t2 order by b1, b2);
 
183
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
184
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
185
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
186
Warnings:
 
187
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`))
 
188
select * from t1 where (a1, a2) in (select b1, b2 from t2 order by b1, b2);
 
189
a1      a2
 
190
1 - 01  2 - 01
 
191
1 - 02  2 - 02
 
192
explain extended
 
193
select * from t1i where (a1, a2) in (select b1, b2 from t2i order by b1, b2);
 
194
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
195
1       SIMPLE  t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       50.00   Using where; Using index; LooseScan
 
196
1       SIMPLE  t1i     ref     it1i1,it1i2,it1i3       it1i1   9       test.t2i.b1     1       100.00  Using where
 
197
Warnings:
 
198
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`))
 
199
select * from t1i where (a1, a2) in (select b1, b2 from t2i order by b1, b2);
 
200
a1      a2
 
201
1 - 01  2 - 01
 
202
1 - 02  2 - 02
 
203
/******************************************************************************
 
204
* Views, UNIONs, several levels of nesting.
 
205
******************************************************************************/
 
206
# materialize the result of subquery over temp-table view
 
207
create algorithm=merge view v1 as
 
208
select b1, c2 from t2, t3 where b2 > c2;
 
209
create algorithm=merge view v2 as
 
210
select b1, c2 from t2, t3 group by b2, c2;
 
211
Warnings:
 
212
Warning 1354    View merge algorithm can't be used here for now (assumed undefined algorithm)
 
213
create algorithm=temptable view v1m as
 
214
select b1, c2 from t2, t3 where b2 > c2;
 
215
create algorithm=temptable view v2m as
 
216
select b1, c2 from t2, t3 group by b2, c2;
 
217
select * from v1 where (c2, b1) in (select c2, b1 from v2 where b1 is not null);
 
218
b1      c2
 
219
1 - 02  2 - 01
 
220
1 - 02  2 - 01
 
221
1 - 03  2 - 01
 
222
1 - 03  2 - 02
 
223
select * from v1 where (c2, b1) in (select distinct c2, b1 from v2 where b1 is not null);
 
224
b1      c2
 
225
1 - 02  2 - 01
 
226
1 - 02  2 - 01
 
227
1 - 03  2 - 01
 
228
1 - 03  2 - 02
 
229
select * from v1m where (c2, b1) in (select c2, b1 from v2m where b1 is not null);
 
230
b1      c2
 
231
1 - 02  2 - 01
 
232
1 - 02  2 - 01
 
233
1 - 03  2 - 01
 
234
1 - 03  2 - 02
 
235
select * from v1m where (c2, b1) in (select distinct c2, b1 from v2m where b1 is not null);
 
236
b1      c2
 
237
1 - 02  2 - 01
 
238
1 - 02  2 - 01
 
239
1 - 03  2 - 01
 
240
1 - 03  2 - 02
 
241
drop view v1, v2, v1m, v2m;
 
242
explain extended
 
243
select * from t1
 
244
where (a1, a2) in (select b1, b2 from t2 where b1 >  '0') and
 
245
(a1, a2) in (select c1, c2 from t3
 
246
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
247
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
248
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
249
1       SIMPLE  <subquery3>     eq_ref  <auto_key>      <auto_key>      18      test.t1.a1,test.t1.a2   1       100.00  NULL
 
250
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(<subquery3>); Using join buffer (Block Nested Loop)
 
251
3       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
252
3       MATERIALIZED    t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       80.00   Using where; Using index; Using join buffer (Block Nested Loop)
 
253
Warnings:
 
254
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) semi join (`test`.`t2`) where ((`<subquery3>`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t3`.`c2`) and (`<subquery3>`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t3`.`c1`) and (`test`.`t3`.`c2` > '0') and (`test`.`t1`.`a1` > '0'))
 
255
select * from t1
 
256
where (a1, a2) in (select b1, b2 from t2 where b1 >  '0') and
 
257
(a1, a2) in (select c1, c2 from t3
 
258
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
259
a1      a2
 
260
1 - 01  2 - 01
 
261
1 - 02  2 - 02
 
262
explain extended
 
263
select * from t1i
 
264
where (a1, a2) in (select b1, b2 from t2i where b1 >  '0') and
 
265
(a1, a2) in (select c1, c2 from t3i
 
266
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
267
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
268
1       SIMPLE  t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       50.00   Using where; Using index; LooseScan
 
269
1       SIMPLE  t1i     ref     it1i1,it1i2,it1i3       it1i1   9       test.t2i.b1     1       100.00  Using where
 
270
1       SIMPLE  t3i     ref     it3i1,it3i2,it3i3       it3i1   9       test.t2i.b1     1       100.00  Using where
 
271
1       SIMPLE  t2i     ref     it2i1,it2i2,it2i3       it2i3   18      test.t2i.b1,test.t2i.b2 2       100.00  Using index; FirstMatch(t1i)
 
272
Warnings:
 
273
Note    1003    /* select#1 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i` join `test`.`t3i`) semi join (`test`.`t2i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t3i`.`c2` = `test`.`t2i`.`b2`) and (`test`.`t2i`.`b2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t3i`.`c1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b2` > '0') and (`test`.`t2i`.`b1` > '0'))
 
274
select * from t1i
 
275
where (a1, a2) in (select b1, b2 from t2i where b1 >  '0') and
 
276
(a1, a2) in (select c1, c2 from t3i
 
277
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
278
a1      a2
 
279
1 - 01  2 - 01
 
280
1 - 02  2 - 02
 
281
explain extended
 
282
select * from t1
 
283
where (a1, a2) in (select b1, b2 from t2
 
284
where b2 in (select c2 from t3 where c2 LIKE '%02') or
 
285
b2 in (select c2 from t3 where c2 LIKE '%03')) and
 
286
(a1, a2) in (select c1, c2 from t3
 
287
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
288
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
289
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
290
1       PRIMARY <subquery5>     eq_ref  <auto_key>      <auto_key>      18      test.t1.a1,test.t1.a2   1       100.00  NULL
 
291
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(<subquery5>); Using join buffer (Block Nested Loop)
 
292
5       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
293
5       MATERIALIZED    t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       80.00   Using where; Using index; Using join buffer (Block Nested Loop)
 
294
4       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
295
3       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
296
Warnings:
 
297
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) semi join (`test`.`t2`) where ((`<subquery5>`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t3`.`c2`) and (`<subquery5>`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t3`.`c1`) and (`test`.`t3`.`c2` > '0') and (<in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (/* select#3 */ select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%02') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on <auto_key> where ((`test`.`t2`.`b2` = `materialized-subquery`.`c2`))))) or <in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (/* select#4 */ select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%03') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on <auto_key> where ((`test`.`t2`.`b2` = `materialized-subquery`.`c2`)))))))
 
298
select * from t1
 
299
where (a1, a2) in (select b1, b2 from t2
 
300
where b2 in (select c2 from t3 where c2 LIKE '%02') or
 
301
b2 in (select c2 from t3 where c2 LIKE '%03')) and
 
302
(a1, a2) in (select c1, c2 from t3
 
303
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
304
a1      a2
 
305
1 - 02  2 - 02
 
306
explain extended
 
307
select * from t1
 
308
where (a1, a2) in (select b1, b2 from t2
 
309
where b2 in (select c2 from t3 t3a where c1 = a1) or
 
310
b2 in (select c2 from t3 t3b where c2 LIKE '%03')) and
 
311
(a1, a2) in (select c1, c2 from t3 t3c
 
312
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
313
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
314
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
315
1       PRIMARY <subquery5>     eq_ref  <auto_key>      <auto_key>      18      test.t1.a1,test.t1.a2   1       100.00  NULL
 
316
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(<subquery5>); Using join buffer (Block Nested Loop)
 
317
5       MATERIALIZED    t3c     ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
318
5       MATERIALIZED    t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       80.00   Using where; Using index; Using join buffer (Block Nested Loop)
 
319
4       SUBQUERY        t3b     ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
320
3       DEPENDENT SUBQUERY      t3a     ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
321
Warnings:
 
322
Note    1276    Field or reference 'test.t1.a1' of SELECT #3 was resolved in SELECT #1
 
323
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2`) semi join (`test`.`t2i` join `test`.`t3` `t3c`) where ((`<subquery5>`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t3c`.`c2`) and (`<subquery5>`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t3c`.`c1`) and (<in_optimizer>(`test`.`t2`.`b2`,<exists>(/* select#3 */ select 1 from `test`.`t3` `t3a` where ((`test`.`t3a`.`c1` = `test`.`t1`.`a1`) and (<cache>(`test`.`t2`.`b2`) = `test`.`t3a`.`c2`)))) or <in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (/* select#4 */ select `test`.`t3b`.`c2` from `test`.`t3` `t3b` where (`test`.`t3b`.`c2` like '%03') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on <auto_key> where ((`test`.`t2`.`b2` = `materialized-subquery`.`c2`)))))) and (`test`.`t3c`.`c2` > '0'))
 
324
select * from t1
 
325
where (a1, a2) in (select b1, b2 from t2
 
326
where b2 in (select c2 from t3 t3a where c1 = a1) or
 
327
b2 in (select c2 from t3 t3b where c2 LIKE '%03')) and
 
328
(a1, a2) in (select c1, c2 from t3 t3c
 
329
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
330
a1      a2
 
331
1 - 01  2 - 01
 
332
1 - 02  2 - 02
 
333
explain extended
 
334
(select * from t1
 
335
where (a1, a2) in (select b1, b2 from t2
 
336
where b2 in (select c2 from t3 where c2 LIKE '%02') or
 
337
b2 in (select c2 from t3 where c2 LIKE '%03')
 
338
group by b1, b2) and
 
339
(a1, a2) in (select c1, c2 from t3
 
340
where (c1, c2) in (select b1, b2 from t2i where b2 > '0')))
 
341
UNION
 
342
(select * from t1i
 
343
where (a1, a2) in (select b1, b2 from t2i where b1 >  '0') and
 
344
(a1, a2) in (select c1, c2 from t3i
 
345
where (c1, c2) in (select b1, b2 from t2i where b2 > '0')));
 
346
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
347
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
348
1       PRIMARY <subquery5>     eq_ref  <auto_key>      <auto_key>      18      test.t1.a1,test.t1.a2   1       100.00  NULL
 
349
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(<subquery5>); Using join buffer (Block Nested Loop)
 
350
5       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
351
5       MATERIALIZED    t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       80.00   Using where; Using index; Using join buffer (Block Nested Loop)
 
352
4       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
353
3       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
354
7       UNION   t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       50.00   Using where; Using index; LooseScan
 
355
7       UNION   t1i     ref     it1i1,it1i2,it1i3       it1i1   9       test.t2i.b1     1       100.00  Using where
 
356
7       UNION   t3i     ref     it3i1,it3i2,it3i3       it3i1   9       test.t2i.b1     1       100.00  Using where
 
357
7       UNION   t2i     ref     it2i1,it2i2,it2i3       it2i3   18      test.t2i.b1,test.t2i.b2 2       100.00  Using index; FirstMatch(t1i)
 
358
NULL    UNION RESULT    <union1,7>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    Using temporary
 
359
Warnings:
 
360
Note    1003    (/* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) semi join (`test`.`t2`) where ((`<subquery5>`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t3`.`c2`) and (`<subquery5>`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t3`.`c1`) and (`test`.`t3`.`c2` > '0') and (<in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (/* select#3 */ select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%02') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on <auto_key> where ((`test`.`t2`.`b2` = `materialized-subquery`.`c2`))))) or <in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (/* select#4 */ select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%03') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on <auto_key> where ((`test`.`t2`.`b2` = `materialized-subquery`.`c2`)))))))) union (/* select#7 */ select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i` join `test`.`t3i`) semi join (`test`.`t2i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t3i`.`c2` = `test`.`t2i`.`b2`) and (`test`.`t2i`.`b2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t3i`.`c1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b2` > '0') and (`test`.`t2i`.`b1` > '0')))
 
361
(select * from t1
 
362
where (a1, a2) in (select b1, b2 from t2
 
363
where b2 in (select c2 from t3 where c2 LIKE '%02') or
 
364
b2 in (select c2 from t3 where c2 LIKE '%03')
 
365
group by b1, b2) and
 
366
(a1, a2) in (select c1, c2 from t3
 
367
where (c1, c2) in (select b1, b2 from t2i where b2 > '0')))
 
368
UNION
 
369
(select * from t1i
 
370
where (a1, a2) in (select b1, b2 from t2i where b1 >  '0') and
 
371
(a1, a2) in (select c1, c2 from t3i
 
372
where (c1, c2) in (select b1, b2 from t2i where b2 > '0')));
 
373
a1      a2
 
374
1 - 02  2 - 02
 
375
1 - 01  2 - 01
 
376
explain extended
 
377
select * from t1
 
378
where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
 
379
(a1, a2) in (select c1, c2 from t3
 
380
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
381
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
382
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
383
1       PRIMARY <subquery4>     eq_ref  <auto_key>      <auto_key>      18      test.t1.a1,test.t1.a2   1       100.00  NULL
 
384
4       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
385
4       MATERIALIZED    t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       80.00   Using where; Using index; Using join buffer (Block Nested Loop)
 
386
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
387
3       DEPENDENT UNION t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where
 
388
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    Using temporary
 
389
Warnings:
 
390
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) where ((`<subquery4>`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t3`.`c2`) and (`<subquery4>`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t3`.`c1`) and <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),<exists>(/* select#2 */ select 1,1 from `test`.`t1` where ((`test`.`t1`.`a1` > '0') and (<cache>(`test`.`t1`.`a1`) = `test`.`t1`.`a1`) and (<cache>(`test`.`t1`.`a2`) = `test`.`t1`.`a2`)) union /* select#3 */ select 1,1 from `test`.`t2` where ((`test`.`t2`.`b1` < '9') and (<cache>(`test`.`t1`.`a1`) = `test`.`t2`.`b1`) and (<cache>(`test`.`t1`.`a2`) = `test`.`t2`.`b2`)))) and (`test`.`t3`.`c2` > '0'))
 
391
select * from t1
 
392
where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
 
393
(a1, a2) in (select c1, c2 from t3
 
394
where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 
395
a1      a2
 
396
1 - 01  2 - 01
 
397
1 - 02  2 - 02
 
398
explain extended
 
399
select * from t1, t3
 
400
where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
 
401
(c1, c2) in (select c1, c2 from t3
 
402
where (c1, c2) in (select b1, b2 from t2i where b2 > '0')) and
 
403
a1 = c1;
 
404
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
405
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
406
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where; Using join buffer (Block Nested Loop)
 
407
1       PRIMARY <subquery4>     eq_ref  <auto_key>      <auto_key>      18      test.t1.a1,test.t3.c2   1       100.00  NULL
 
408
4       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
409
4       MATERIALIZED    t2i     index   it2i1,it2i2,it2i3       it2i3   18      NULL    5       80.00   Using where; Using index; Using join buffer (Block Nested Loop)
 
410
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
411
3       DEPENDENT UNION t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where
 
412
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    Using temporary
 
413
Warnings:
 
414
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2`,`test`.`t3`.`c1` AS `c1`,`test`.`t3`.`c2` AS `c2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) join `test`.`t3` where ((`<subquery4>`.`c2` = `test`.`t3`.`c2`) and (`test`.`t2i`.`b2` = `test`.`t3`.`c2`) and (`test`.`t3`.`c1` = `test`.`t1`.`a1`) and (`<subquery4>`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t3`.`c1`) and <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),<exists>(/* select#2 */ select 1,1 from `test`.`t1` where ((`test`.`t1`.`a1` > '0') and (<cache>(`test`.`t1`.`a1`) = `test`.`t1`.`a1`) and (<cache>(`test`.`t1`.`a2`) = `test`.`t1`.`a2`)) union /* select#3 */ select 1,1 from `test`.`t2` where ((`test`.`t2`.`b1` < '9') and (<cache>(`test`.`t1`.`a1`) = `test`.`t2`.`b1`) and (<cache>(`test`.`t1`.`a2`) = `test`.`t2`.`b2`)))) and (`test`.`t3`.`c2` > '0'))
 
415
select * from t1, t3
 
416
where (a1, a2) in (select * from t1 where a1 > '0' UNION select * from t2 where b1 < '9') and
 
417
(c1, c2) in (select c1, c2 from t3
 
418
where (c1, c2) in (select b1, b2 from t2i where b2 > '0')) and
 
419
a1 = c1;
 
420
a1      a2      c1      c2
 
421
1 - 01  2 - 01  1 - 01  2 - 01
 
422
1 - 02  2 - 02  1 - 02  2 - 02
 
423
/******************************************************************************
 
424
* Negative tests, where materialization should not be applied.
 
425
******************************************************************************/
 
426
# UNION in a subquery
 
427
explain extended
 
428
select * from t3
 
429
where c1 in (select a1 from t1 where a1 > '0' UNION select b1 from t2 where b1 < '9');
 
430
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
431
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
432
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
433
3       DEPENDENT UNION t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where
 
434
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    Using temporary
 
435
Warnings:
 
436
Note    1003    /* select#1 */ select `test`.`t3`.`c1` AS `c1`,`test`.`t3`.`c2` AS `c2` from `test`.`t3` where <in_optimizer>(`test`.`t3`.`c1`,<exists>(/* select#2 */ select 1 from `test`.`t1` where ((`test`.`t1`.`a1` > '0') and (<cache>(`test`.`t3`.`c1`) = `test`.`t1`.`a1`)) union /* select#3 */ select 1 from `test`.`t2` where ((`test`.`t2`.`b1` < '9') and (<cache>(`test`.`t3`.`c1`) = `test`.`t2`.`b1`))))
 
437
select * from t3
 
438
where c1 in (select a1 from t1 where a1 > '0' UNION select b1 from t2 where b1 < '9');
 
439
c1      c2
 
440
1 - 01  2 - 01
 
441
1 - 02  2 - 02
 
442
1 - 03  2 - 03
 
443
explain extended
 
444
select * from t1
 
445
where (a1, a2) in (select b1, b2 from t2
 
446
where b2 in (select c2 from t3 t3a where c1 = a1) or
 
447
b2 in (select c2 from t3 t3b where c2 LIKE '%03')) and
 
448
(a1, a2) in (select c1, c2 from t3 t3c
 
449
where (c1, c2) in (select b1, b2 from t2i where b2 > '0' or b2 = a2));
 
450
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
451
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where; Start temporary
 
452
1       PRIMARY t2i     ref     it2i1,it2i2,it2i3       it2i3   18      test.t1.a1,test.t1.a2   2       100.00  Using index
 
453
1       PRIMARY t3c     ALL     NULL    NULL    NULL    NULL    4       100.00  Using where; End temporary; Using join buffer (Block Nested Loop)
 
454
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    5       100.00  Using where; FirstMatch(t3c); Using join buffer (Block Nested Loop)
 
455
4       SUBQUERY        t3b     ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
456
3       DEPENDENT SUBQUERY      t3a     ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
457
Warnings:
 
458
Note    1276    Field or reference 'test.t1.a1' of SELECT #3 was resolved in SELECT #1
 
459
Note    1276    Field or reference 'test.t1.a2' of SELECT #6 was resolved in SELECT #1
 
460
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3` `t3c`) semi join (`test`.`t2`) where ((`test`.`t2i`.`b2` = `test`.`t1`.`a2`) and (`test`.`t3c`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2`.`b2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b1` = `test`.`t1`.`a1`) and (`test`.`t3c`.`c1` = `test`.`t1`.`a1`) and (`test`.`t2`.`b1` = `test`.`t1`.`a1`) and (<in_optimizer>(`test`.`t2`.`b2`,<exists>(/* select#3 */ select 1 from `test`.`t3` `t3a` where ((`test`.`t3a`.`c1` = `test`.`t1`.`a1`) and (<cache>(`test`.`t2`.`b2`) = `test`.`t3a`.`c2`)))) or <in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (/* select#4 */ select `test`.`t3b`.`c2` from `test`.`t3` `t3b` where (`test`.`t3b`.`c2` like '%03') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on <auto_key> where ((`test`.`t2`.`b2` = `materialized-subquery`.`c2`)))))))
 
461
explain extended
 
462
select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01');
 
463
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
464
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
465
2       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
466
Warnings:
 
467
Note    1003    /* select#1 */ 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#2 */ select '1 - 01','2 - 01' having ((<cache>(`test`.`t1`.`a1`) = '1 - 01') and (<cache>(`test`.`t1`.`a2`) = '2 - 01'))))
 
468
select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01');
 
469
a1      a2
 
470
1 - 01  2 - 01
 
471
explain extended
 
472
select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01' from dual);
 
473
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
474
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
475
2       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
476
Warnings:
 
477
Note    1003    /* select#1 */ 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#2 */ select '1 - 01','2 - 01' having ((<cache>(`test`.`t1`.`a1`) = '1 - 01') and (<cache>(`test`.`t1`.`a2`) = '2 - 01'))))
 
478
select * from t1 where (a1, a2) in (select '1 - 01', '2 - 01' from dual);
 
479
a1      a2
 
480
1 - 01  2 - 01
 
481
/******************************************************************************
 
482
* Subqueries in other uncovered clauses.
 
483
******************************************************************************/
 
484
/* SELECT clause */
 
485
select ((a1,a2) IN (select * from t2 where b2 > '0')) IS NULL from t1;
 
486
((a1,a2) IN (select * from t2 where b2 > '0')) IS NULL
 
487
0
 
488
0
 
489
0
 
490
/* GROUP BY clause */
 
491
create table columns (col int key);
 
492
insert into columns values (1), (2);
 
493
explain extended
 
494
select * from t1 group by (select col from columns limit 1);
 
495
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
496
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
497
2       SUBQUERY        columns index   NULL    PRIMARY 4       NULL    2       100.00  Using index
 
498
Warnings:
 
499
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` group by (/* select#2 */ select `test`.`columns`.`col` from `test`.`columns` limit 1)
 
500
select * from t1 group by (select col from columns limit 1);
 
501
a1      a2
 
502
1 - 00  2 - 00
 
503
explain extended
 
504
select * from t1 group by (a1 in (select col from columns));
 
505
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
506
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  Using temporary; Using filesort
 
507
2       DEPENDENT SUBQUERY      columns unique_subquery PRIMARY PRIMARY 4       func    1       100.00  Using index; Using where; Full scan on NULL key
 
508
Warnings:
 
509
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` group by <in_optimizer>(`test`.`t1`.`a1`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`a1`) in columns on PRIMARY where <if>(outer_field_is_not_null, (<cache>(`test`.`t1`.`a1`) = `test`.`columns`.`col`), true))))
 
510
select * from t1 group by (a1 in (select col from columns));
 
511
a1      a2
 
512
1 - 00  2 - 00
 
513
/* ORDER BY clause */
 
514
explain extended
 
515
select * from t1 order by (select col from columns limit 1);
 
516
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
517
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
518
2       SUBQUERY        columns index   NULL    PRIMARY 4       NULL    2       100.00  Using index
 
519
Warnings:
 
520
Note    1003    /* select#1 */ select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` order by (/* select#2 */ select `test`.`columns`.`col` from `test`.`columns` limit 1)
 
521
select * from t1 order by (select col from columns limit 1);
 
522
a1      a2
 
523
1 - 00  2 - 00
 
524
1 - 01  2 - 01
 
525
1 - 02  2 - 02
 
526
/******************************************************************************
 
527
* Column types/sizes that affect materialization.
 
528
******************************************************************************/
 
529
# test for BIT fields
 
530
create table t1bit (a1 bit(3), a2 bit(3));
 
531
create table t2bit (b1 bit(3), b2 bit(3));
 
532
insert into t1bit values (b'000', b'100');
 
533
insert into t1bit values (b'001', b'101');
 
534
insert into t1bit values (b'010', b'110');
 
535
insert into t2bit values (b'001', b'101');
 
536
insert into t2bit values (b'010', b'110');
 
537
insert into t2bit values (b'110', b'111');
 
538
explain extended select bin(a1), bin(a2)
 
539
from t1bit
 
540
where (a1, a2) in (select b1, b2 from t2bit);
 
541
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
542
1       SIMPLE  t1bit   ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
543
1       SIMPLE  t2bit   ALL     NULL    NULL    NULL    NULL    3       100.00  Using where; FirstMatch(t1bit); Using join buffer (Block Nested Loop)
 
544
Warnings:
 
545
Note    1003    /* select#1 */ select conv(`test`.`t1bit`.`a1`,10,2) AS `bin(a1)`,conv(`test`.`t1bit`.`a2`,10,2) AS `bin(a2)` from `test`.`t1bit` semi join (`test`.`t2bit`) where ((`test`.`t2bit`.`b2` = `test`.`t1bit`.`a2`) and (`test`.`t2bit`.`b1` = `test`.`t1bit`.`a1`))
 
546
select bin(a1), bin(a2)
 
547
from t1bit
 
548
where (a1, a2) in (select b1, b2 from t2bit);
 
549
bin(a1) bin(a2)
 
550
1       101
 
551
10      110
 
552
drop table t1bit, t2bit;
 
553
create table t1bb (a1 bit(3), a2 blob(3));
 
554
create table t2bb (b1 bit(3), b2 blob(3));
 
555
insert into t1bb values (b'000', '100');
 
556
insert into t1bb values (b'001', '101');
 
557
insert into t1bb values (b'010', '110');
 
558
insert into t2bb values (b'001', '101');
 
559
insert into t2bb values (b'010', '110');
 
560
insert into t2bb values (b'110', '111');
 
561
explain extended select bin(a1), a2
 
562
from t1bb
 
563
where (a1, a2) in (select b1, b2 from t2bb);
 
564
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
565
1       SIMPLE  t1bb    ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
566
1       SIMPLE  t2bb    ALL     NULL    NULL    NULL    NULL    3       100.00  Using where; FirstMatch(t1bb); Using join buffer (Block Nested Loop)
 
567
Warnings:
 
568
Note    1003    /* select#1 */ select conv(`test`.`t1bb`.`a1`,10,2) AS `bin(a1)`,`test`.`t1bb`.`a2` AS `a2` from `test`.`t1bb` semi join (`test`.`t2bb`) where ((`test`.`t2bb`.`b2` = `test`.`t1bb`.`a2`) and (`test`.`t2bb`.`b1` = `test`.`t1bb`.`a1`))
 
569
select bin(a1), a2
 
570
from t1bb
 
571
where (a1, a2) in (select b1, b2 from t2bb);
 
572
bin(a1) a2
 
573
1       101
 
574
10      110
 
575
drop table t1bb, t2bb;
 
576
drop table t1, t2, t3, t1i, t2i, t3i, columns;
 
577
/******************************************************************************
 
578
* Test the cache of the left operand of IN.
 
579
******************************************************************************/
 
580
# Test that default values of Cached_item are not used for comparison
 
581
create table t1 (s1 int);
 
582
create table t2 (s2 int);
 
583
insert into t1 values (5),(1),(0);
 
584
insert into t2 values (0), (1);
 
585
select s2 from t2 where s2 in (select s1 from t1);
 
586
s2
 
587
0
 
588
1
 
589
drop table t1, t2;
 
590
create table t1 (a int not null, b int not null);
 
591
create table t2 (c int not null, d int not null);
 
592
create table t3 (e int not null);
 
593
insert into t1 values (1,10);
 
594
insert into t1 values (1,20);
 
595
insert into t1 values (2,10);
 
596
insert into t1 values (2,20);
 
597
insert into t1 values (2,30);
 
598
insert into t1 values (3,20);
 
599
insert into t1 values (4,40);
 
600
insert into t2 values (2,10);
 
601
insert into t2 values (2,20);
 
602
insert into t2 values (2,40);
 
603
insert into t2 values (3,20);
 
604
insert into t2 values (4,10);
 
605
insert into t2 values (5,10);
 
606
insert into t3 values (10);
 
607
insert into t3 values (10);
 
608
insert into t3 values (20);
 
609
insert into t3 values (30);
 
610
explain extended
 
611
select a from t1 where a in (select c from t2 where d >= 20);
 
612
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
613
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    0.00    NULL
 
614
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    7       100.00  Using where; Using join buffer (Block Nested Loop)
 
615
2       MATERIALIZED    t2      ALL     NULL    NULL    NULL    NULL    6       100.00  Using where
 
616
Warnings:
 
617
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `<subquery2>`.`c`) and (`test`.`t2`.`d` >= 20))
 
618
select a from t1 where a in (select c from t2 where d >= 20);
 
619
a
 
620
2
 
621
2
 
622
2
 
623
3
 
624
create index it1a on t1(a);
 
625
explain extended
 
626
select a from t1 where a in (select c from t2 where d >= 20);
 
627
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
628
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    0.00    NULL
 
629
1       SIMPLE  t1      ref     it1a    it1a    4       <subquery2>.c   2       100.00  Using index
 
630
2       MATERIALIZED    t2      ALL     NULL    NULL    NULL    NULL    6       100.00  Using where
 
631
Warnings:
 
632
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `<subquery2>`.`c`) and (`test`.`t2`.`d` >= 20))
 
633
select a from t1 where a in (select c from t2 where d >= 20);
 
634
a
 
635
2
 
636
2
 
637
2
 
638
3
 
639
insert into t2 values (1,10);
 
640
explain extended
 
641
select a from t1 where a in (select c from t2 where d >= 20);
 
642
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
643
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    0.00    NULL
 
644
1       SIMPLE  t1      ref     it1a    it1a    4       <subquery2>.c   2       100.00  Using index
 
645
2       MATERIALIZED    t2      ALL     NULL    NULL    NULL    NULL    7       100.00  Using where
 
646
Warnings:
 
647
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `<subquery2>`.`c`) and (`test`.`t2`.`d` >= 20))
 
648
select a from t1 where a in (select c from t2 where d >= 20);
 
649
a
 
650
2
 
651
2
 
652
2
 
653
3
 
654
explain extended
 
655
select a from t1 group by a having a in (select c from t2 where d >= 20);
 
656
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
657
1       PRIMARY t1      index   it1a    it1a    4       NULL    7       100.00  Using index
 
658
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    7       100.00  Using where
 
659
Warnings:
 
660
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`c` from `test`.`t2` where (`test`.`t2`.`d` >= 20) ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`c`)))))
 
661
select a from t1 group by a having a in (select c from t2 where d >= 20);
 
662
a
 
663
2
 
664
3
 
665
create index iab on t1(a, b);
 
666
explain extended
 
667
select a from t1 group by a having a in (select c from t2 where d >= 20);
 
668
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
669
1       PRIMARY t1      index   it1a,iab        it1a    4       NULL    7       100.00  Using index
 
670
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    7       100.00  Using where
 
671
Warnings:
 
672
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`c` from `test`.`t2` where (`test`.`t2`.`d` >= 20) ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`c`)))))
 
673
select a from t1 group by a having a in (select c from t2 where d >= 20);
 
674
a
 
675
2
 
676
3
 
677
explain extended
 
678
select a from t1 group by a
 
679
having a in (select c from t2 where d >= some(select e from t3 where max(b)=e));
 
680
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
681
1       PRIMARY t1      index   it1a,iab        iab     8       NULL    7       100.00  Using index
 
682
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    7       100.00  Using where
 
683
3       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
684
Warnings:
 
685
Note    1276    Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
 
686
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select 1 from `test`.`t2` where (<nop>(<in_optimizer>(`test`.`t2`.`d`,<exists>(/* select#3 */ select 1 from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having (<cache>(`test`.`t2`.`d`) >= <ref_null_helper>(`test`.`t3`.`e`))))) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`c`))))
 
687
select a from t1 group by a
 
688
having a in (select c from t2 where d >= some(select e from t3 where max(b)=e));
 
689
a
 
690
2
 
691
3
 
692
explain extended
 
693
select a from t1
 
694
where a in (select c from t2 where d >= some(select e from t3 where b=e));
 
695
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
696
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    7       100.00  Start temporary
 
697
1       PRIMARY t1      ref     it1a,iab        iab     4       test.t2.c       1       100.00  Using where; Using index; End temporary
 
698
3       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
699
Warnings:
 
700
Note    1276    Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
 
701
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `test`.`t2`.`c`) and <nop>(<in_optimizer>(`test`.`t2`.`d`,<exists>(/* select#3 */ select 1 from `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`e`) and (<cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`))))))
 
702
select a from t1
 
703
where a in (select c from t2 where d >= some(select e from t3 where b=e));
 
704
a
 
705
1
 
706
2
 
707
2
 
708
2
 
709
3
 
710
drop table t1, t2, t3;
 
711
create table t2 (a int, b int, key(a), key(b));
 
712
insert into t2 values (3,3),(3,3),(3,3);
 
713
select 1 from t2 where  
 
714
t2.a > 1 
 
715
or 
 
716
t2.a = 3 and not t2.a not in (select t2.b from t2);
 
717
1
 
718
1
 
719
1
 
720
1
 
721
drop table t2;
 
722
create table t1 (a1 int key);
 
723
create table t2 (b1 int);
 
724
insert into t1 values (5);
 
725
explain select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
 
726
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
727
1       SIMPLE  t1      system  NULL    NULL    NULL    NULL    1       NULL
 
728
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    0       Using where; FirstMatch(t1)
 
729
select min(a1) from t1 where 7 in (select b1 from t2 group by b1);
 
730
min(a1)
 
731
NULL
 
732
explain select min(a1) from t1 where 7 in (select b1 from t2);
 
733
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
734
1       SIMPLE  t1      system  NULL    NULL    NULL    NULL    1       NULL
 
735
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    0       Using where; FirstMatch(t1)
 
736
select min(a1) from t1 where 7 in (select b1 from t2);
 
737
min(a1)
 
738
NULL
 
739
drop table t1,t2;
 
740
create table t1 (a char(2), b varchar(10));
 
741
insert into t1 values ('a',  'aaa');
 
742
insert into t1 values ('aa', 'aaaa');
 
743
explain select a,b from t1 where b in (select a from t1);
 
744
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
745
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       NULL
 
746
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
747
select a,b from t1 where b in (select a from t1);
 
748
a       b
 
749
prepare st1 from "select a,b from t1 where b in (select a from t1)";
 
750
execute st1;
 
751
a       b
 
752
execute st1;
 
753
a       b
 
754
drop table t1;
 
755
CREATE TABLE t1 (a varchar(5), b varchar(10));
 
756
INSERT INTO t1 VALUES
 
757
('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
 
758
('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
 
759
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
760
a       b
 
761
BBB     4
 
762
CCC     7
 
763
AAA     8
 
764
EXPLAIN
 
765
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
766
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
767
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    9       Using where
 
768
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    9       Using temporary; Using filesort
 
769
ALTER TABLE t1 ADD INDEX(a);
 
770
FLUSH STATUS;
 
771
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
772
a       b
 
773
BBB     4
 
774
CCC     7
 
775
AAA     8
 
776
SHOW SESSION STATUS LIKE 'Sort_scan%';
 
777
Variable_name   Value
 
778
Sort_scan       1
 
779
EXPLAIN SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
780
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
781
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    9       Using where
 
782
2       SUBQUERY        t1      ALL     a       NULL    NULL    NULL    9       Using temporary; Using filesort
 
783
DROP TABLE t1;
 
784
CREATE TABLE t1 (a INT);
 
785
INSERT INTO t1 VALUES (1),(2);
 
786
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 GROUP BY a);
 
787
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
788
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       100.00  FirstMatch
 
789
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using join buffer (Block Nested Loop)
 
790
Warnings:
 
791
Note    1003    /* select#1 */ select 1 AS `1` from `test`.`t1` semi join (`test`.`t1`) where 1
 
792
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 WHERE a > 3 GROUP BY a);
 
793
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
794
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; FirstMatch
 
795
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using join buffer (Block Nested Loop)
 
796
Warnings:
 
797
Note    1003    /* select#1 */ select 1 AS `1` from `test`.`t1` semi join (`test`.`t1`) where (`test`.`t1`.`a` > 3)
 
798
DROP TABLE t1;
 
799
#
 
800
# BUG#49630: Segfault in select_describe() with double 
 
801
#            nested subquery and materialization
 
802
#
 
803
CREATE TABLE t1 (t1i int);
 
804
CREATE TABLE t2 (t2i int);
 
805
CREATE TABLE t3 (t3i int);
 
806
CREATE TABLE t4 (t4i int);
 
807
INSERT INTO t1 VALUES (1);
 
808
INSERT INTO t2 VALUES (1),(2);
 
809
INSERT INTO t3 VALUES (1),(2);
 
810
INSERT INTO t4 VALUES (1),(2);
 
811
 
 
812
EXPLAIN 
 
813
SELECT t1i
 
814
FROM t1 JOIN t4 ON t1i=t4i  
 
815
WHERE (t1i)  IN (  
 
816
SELECT t2i
 
817
FROM t2  
 
818
WHERE (t2i)  IN (  
 
819
SELECT t3i
 
820
FROM t3  
 
821
GROUP BY t3i
 
822
)  
 
823
);
 
824
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
825
1       SIMPLE  t1      system  NULL    NULL    NULL    NULL    1       NULL
 
826
1       SIMPLE  t4      ALL     NULL    NULL    NULL    NULL    2       Using where
 
827
1       SIMPLE  <subquery2>     const   <auto_key>      <auto_key>      5       const   1       NULL
 
828
2       MATERIALIZED    t2      ALL     NULL    NULL    NULL    NULL    2       Using where
 
829
2       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    2       Using where; Using join buffer (Block Nested Loop)
 
830
DROP TABLE t1,t2,t3,t4;
 
831
#
 
832
# BUG#46680 - Assertion failed in file item_subselect.cc, 
 
833
#             line 305 crashing on HAVING subquery
 
834
#
 
835
# Create tables
 
836
#
 
837
CREATE TABLE t1 (
 
838
pk INT,
 
839
v VARCHAR(1) DEFAULT NULL,
 
840
PRIMARY KEY(pk)
 
841
);
 
842
CREATE TABLE t2 LIKE t1;
 
843
CREATE TABLE t3 LIKE t1;
 
844
CREATE TABLE empty1 (a int);
 
845
INSERT INTO t1 VALUES (1,'c'),(2,NULL);
 
846
INSERT INTO t2 VALUES (3,'m'),(4,NULL);
 
847
INSERT INTO t3 VALUES (1,'n');
 
848
 
 
849
#
 
850
# 1) Test that subquery materialization is setup for query with
 
851
#    premature optimize() exit due to "Impossible WHERE"
 
852
#
 
853
SELECT MIN(t2.pk)
 
854
FROM t2 JOIN t1 ON t1.pk=t2.pk
 
855
WHERE 'j'
 
856
HAVING ('m') IN ( 
 
857
SELECT v
 
858
FROM t2);
 
859
MIN(t2.pk)
 
860
NULL
 
861
Warnings:
 
862
Warning 1292    Truncated incorrect INTEGER value: 'j'
 
863
 
 
864
EXPLAIN
 
865
SELECT MIN(t2.pk)
 
866
FROM t2 JOIN t1 ON t1.pk=t2.pk
 
867
WHERE 'j'
 
868
HAVING ('m') IN ( 
 
869
SELECT v
 
870
FROM t2);
 
871
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
872
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
873
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where
 
874
Warnings:
 
875
Warning 1292    Truncated incorrect INTEGER value: 'j'
 
876
 
 
877
#
 
878
# 2) Test that subquery materialization is setup for query with
 
879
#    premature optimize() exit due to "No matching min/max row"
 
880
#
 
881
SELECT MIN(t2.pk)
 
882
FROM t2 
 
883
WHERE t2.pk>10
 
884
HAVING ('m') IN ( 
 
885
SELECT v
 
886
FROM t2);
 
887
MIN(t2.pk)
 
888
NULL
 
889
 
 
890
EXPLAIN
 
891
SELECT MIN(t2.pk)
 
892
FROM t2 
 
893
WHERE t2.pk>10
 
894
HAVING ('m') IN ( 
 
895
SELECT v
 
896
FROM t2);
 
897
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
898
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    No matching min/max row
 
899
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where
 
900
 
 
901
#
 
902
# 3) Test that subquery materialization is setup for query with
 
903
#    premature optimize() exit due to "Select tables optimized away"
 
904
#
 
905
SELECT MIN(pk)
 
906
FROM t1
 
907
WHERE pk=NULL
 
908
HAVING ('m') IN ( 
 
909
SELECT v
 
910
FROM t2);
 
911
MIN(pk)
 
912
NULL
 
913
 
 
914
EXPLAIN
 
915
SELECT MIN(pk)
 
916
FROM t1
 
917
WHERE pk=NULL
 
918
HAVING ('m') IN ( 
 
919
SELECT v
 
920
FROM t2);
 
921
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
922
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
923
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where
 
924
 
 
925
#
 
926
# 4) Test that subquery materialization is setup for query with
 
927
#    premature optimize() exit due to "No matching row in const table"
 
928
#
 
929
 
 
930
SELECT MIN(a)
 
931
FROM (SELECT a FROM empty1) tt
 
932
HAVING ('m') IN ( 
 
933
SELECT v
 
934
FROM t2);
 
935
MIN(a)
 
936
NULL
 
937
 
 
938
EXPLAIN 
 
939
SELECT MIN(a)
 
940
FROM (SELECT a FROM empty1) tt
 
941
HAVING ('m') IN ( 
 
942
SELECT v
 
943
FROM t2);
 
944
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
945
1       PRIMARY <derived2>      system  NULL    NULL    NULL    NULL    0       const row not found
 
946
3       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where
 
947
2       DERIVED NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
948
 
 
949
#
 
950
# 5) Test that subquery materialization is setup for query with
 
951
#    premature optimize() exit due to "Impossible WHERE noticed 
 
952
#    after reading const tables"
 
953
#
 
954
SELECT min(t1.pk)
 
955
FROM t1
 
956
WHERE t1.pk IN (SELECT 1 from t3 where pk>10)
 
957
HAVING ('m') IN ( 
 
958
SELECT v
 
959
FROM t2);
 
960
min(t1.pk)
 
961
NULL
 
962
 
 
963
EXPLAIN
 
964
SELECT min(t1.pk)
 
965
FROM t1
 
966
WHERE t1.pk IN (SELECT 1 from t3 where pk>10)
 
967
HAVING ('m') IN ( 
 
968
SELECT v
 
969
FROM t2);
 
970
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
971
1       PRIMARY t1      const   PRIMARY PRIMARY 4       const   1       Using index
 
972
1       PRIMARY t3      index   PRIMARY PRIMARY 4       NULL    1       Using where; Using index; FirstMatch(t1)
 
973
3       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where
 
974
#
 
975
# Cleanup for BUG#46680
 
976
#
 
977
DROP TABLE IF EXISTS t1,t2,t3,empty1;
 
978
#
 
979
# BUG#52344 - Subquery materialization: 
 
980
#            Assertion if subquery in on-clause of outer join
 
981
#
 
982
CREATE TABLE t1 (i INTEGER);
 
983
INSERT INTO t1 VALUES (10);
 
984
CREATE TABLE t2 (j INTEGER);
 
985
INSERT INTO t2 VALUES (5);
 
986
CREATE TABLE t3 (k INTEGER);
 
987
EXPLAIN
 
988
SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
 
989
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
990
1       SIMPLE  t1      system  NULL    NULL    NULL    NULL    1       NULL
 
991
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    0       Using where; Start temporary
 
992
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    1       Using where; End temporary
 
993
SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
 
994
i       j
 
995
10      NULL
 
996
EXPLAIN
 
997
SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
 
998
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
999
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    1       NULL
 
1000
1       PRIMARY t2      system  NULL    NULL    NULL    NULL    1       NULL
 
1001
2       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1002
SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
 
1003
i       j
 
1004
10      NULL
 
1005
DROP TABLE t1, t2, t3;
 
1006
# End BUG#52344
 
1007
CREATE TABLE t1 (
 
1008
pk INTEGER AUTO_INCREMENT,
 
1009
col_int_nokey INTEGER,
 
1010
col_int_key INTEGER,
 
1011
col_varchar_key VARCHAR(1),
 
1012
PRIMARY KEY (pk),
 
1013
KEY (col_int_key),
 
1014
KEY (col_varchar_key, col_int_key)
 
1015
)
 
1016
;
 
1017
INSERT INTO t1 (
 
1018
col_int_key, col_int_nokey, col_varchar_key
 
1019
 
1020
VALUES
 
1021
(2, NULL, 'w'),
 
1022
(9, 7, 'm'),
 
1023
(3, 9, 'm'),
 
1024
(9, 7, 'k'),
 
1025
(NULL, 4, 'r'),
 
1026
(9, 2, 't'),
 
1027
(3, 6, 'j'),
 
1028
(8, 8, 'u'),
 
1029
(8, NULL, 'h'),
 
1030
(53, 5, 'o'),
 
1031
(0, NULL, NULL),
 
1032
(5, 6, 'k'),
 
1033
(166, 188, 'e'),
 
1034
(3, 2, 'n'),
 
1035
(0, 1, 't'),
 
1036
(1, 1, 'c'),
 
1037
(9, 0, 'm'),
 
1038
(5, 9, 'y'),
 
1039
(6, NULL, 'f'),
 
1040
(2, 4, 'd')
 
1041
;
 
1042
SELECT table2.col_varchar_key AS field1,
 
1043
table2.col_int_nokey AS field2
 
1044
FROM ( t1 AS table1 LEFT OUTER JOIN t1 AS table2
 
1045
ON (table2.col_varchar_key = table1.col_varchar_key  ) ) 
 
1046
WHERE table1.pk = 6
 
1047
HAVING  ( field2 ) IN 
 
1048
( SELECT SUBQUERY2_t2.col_int_nokey AS SUBQUERY2_field2 
 
1049
FROM ( t1 AS SUBQUERY2_t1 JOIN t1 AS SUBQUERY2_t2
 
1050
ON (SUBQUERY2_t2.col_varchar_key = SUBQUERY2_t1.col_varchar_key ) ) )
 
1051
ORDER BY field2 
 
1052
;
 
1053
field1  field2
 
1054
t       1
 
1055
t       2
 
1056
drop table t1;
 
1057
#
 
1058
# BUG#53103: MTR test ps crashes in optimize_cond() 
 
1059
#            when running with --debug
 
1060
#
 
1061
CREATE TABLE t1(track varchar(15));
 
1062
INSERT INTO t1 VALUES ('CAD'), ('CAD');
 
1063
PREPARE STMT FROM
 
1064
"SELECT 1 FROM t1
 
1065
  WHERE
 
1066
        track IN (SELECT track FROM t1
 
1067
                                    GROUP BY track 
 
1068
                                      HAVING track>='CAD')";
 
1069
EXECUTE STMT ;
 
1070
1
 
1071
1
 
1072
1
 
1073
EXECUTE STMT ;
 
1074
1
 
1075
1
 
1076
1
 
1077
DEALLOCATE PREPARE STMT;
 
1078
DROP TABLE t1;
 
1079
# End of BUG#53103
 
1080
#
 
1081
# BUG#54511 - Assertion failed: cache != 0L in file 
 
1082
#             sql_select.cc::sub_select_cache on HAVING
 
1083
#
 
1084
CREATE TABLE t1 (i int(11));
 
1085
CREATE TABLE t2 (c char(1));
 
1086
CREATE TABLE t3 (c char(1));
 
1087
INSERT INTO t1 VALUES (1), (2);
 
1088
INSERT INTO t2 VALUES ('a'), ('b');
 
1089
INSERT INTO t3 VALUES ('x'), ('y');
 
1090
SELECT COUNT( i ),i
 
1091
FROM t1
 
1092
HAVING ('c')  
 
1093
IN (SELECT t2.c FROM (t2 JOIN t3));
 
1094
COUNT( i )      i
 
1095
DROP TABLE t1,t2,t3;
 
1096
# End BUG#54511
 
1097
#
 
1098
# BUG#56367 - Assertion exec_method != EXEC_MATERIALIZATION...
 
1099
#             on subquery in FROM
 
1100
#
 
1101
CREATE TABLE t1 (a INTEGER);
 
1102
CREATE TABLE t2 (b INTEGER);
 
1103
INSERT INTO t2 VALUES (1);
 
1104
explain SELECT a FROM (
 
1105
SELECT t1.* FROM t1 LEFT JOIN t2 ON t1.a > 3 OR t2.b IN (SELECT a FROM t1)
 
1106
) table1;
 
1107
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1108
1       PRIMARY <derived2>      system  NULL    NULL    NULL    NULL    0       const row not found
 
1109
2       DERIVED NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1110
3       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1111
SELECT a FROM (
 
1112
SELECT t1.* FROM t1 LEFT JOIN t2 ON t1.a > 3 OR t2.b IN (SELECT a FROM t1)
 
1113
) table1;
 
1114
a
 
1115
DROP TABLE t1, t2;
 
1116
# End BUG#56367
 
1117
#
 
1118
# Bug#59833 - materialization=on/off leads to different result set
 
1119
#             when using IN
 
1120
#
 
1121
CREATE TABLE t1 (
 
1122
pk int NOT NULL,
 
1123
f1 int DEFAULT NULL,
 
1124
PRIMARY KEY (pk)
 
1125
) ENGINE=MyISAM;
 
1126
CREATE TABLE t2 (
 
1127
pk int NOT NULL,
 
1128
f1 int DEFAULT NULL,
 
1129
PRIMARY KEY (pk)
 
1130
) ENGINE=MyISAM;
 
1131
INSERT INTO t1 VALUES (10,0);
 
1132
INSERT INTO t2 VALUES (10,0),(11,0);
 
1133
explain SELECT * FROM t1 JOIN t2 USING (f1)
 
1134
WHERE t1.f1 IN (SELECT t1.pk FROM t1 ORDER BY t1.f1);
 
1135
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1136
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
1137
SELECT * FROM t1 JOIN t2 USING (f1)
 
1138
WHERE t1.f1 IN (SELECT t1.pk FROM t1 ORDER BY t1.f1);
 
1139
f1      pk      pk
 
1140
DROP TABLE t1, t2;
 
1141
# End Bug#59833
 
1142
#
 
1143
# Bug#11852644 - CRASH IN ITEM_REF::SAVE_IN_FIELD ON SELECT DISTINCT
 
1144
#
 
1145
CREATE TABLE t1 (
 
1146
col_varchar_key varchar(1) DEFAULT NULL,
 
1147
col_varchar_nokey varchar(1) DEFAULT NULL,
 
1148
KEY col_varchar_key (col_varchar_key)) 
 
1149
;
 
1150
INSERT INTO t1 VALUES
 
1151
('v','v'),('r','r');
 
1152
CREATE TABLE t2 (
 
1153
col_varchar_key varchar(1) DEFAULT NULL,
 
1154
col_varchar_nokey varchar(1) DEFAULT NULL,
 
1155
KEY col_varchar_key(col_varchar_key)) 
 
1156
;
 
1157
INSERT INTO t2 VALUES
 
1158
('r','r'),('c','c');
 
1159
CREATE VIEW v3 AS SELECT * FROM t2;
 
1160
SELECT DISTINCT alias2.col_varchar_key 
 
1161
FROM t1 AS alias1 JOIN v3 AS alias2 
 
1162
ON alias2.col_varchar_key = alias1.col_varchar_key
 
1163
HAVING col_varchar_key IN (SELECT col_varchar_nokey FROM t2)
 
1164
;
 
1165
col_varchar_key
 
1166
r
 
1167
DROP TABLE t1, t2;
 
1168
DROP VIEW v3;
 
1169
# End Bug#11852644
 
1170
 
 
1171
# Bug#12668294 - GROUP BY ON EMPTY RESULT GIVES EMPTY ROW
 
1172
# INSTEAD OF NULL WHEN MATERIALIZATION ON
 
1173
 
 
1174
CREATE TABLE t1 (col_int_nokey INT) ENGINE=MEMORY;
 
1175
CREATE TABLE t2 (col_int_nokey INT) ENGINE=MEMORY;
 
1176
INSERT INTO t2 VALUES (8),(7);
 
1177
CREATE TABLE t3 (col_int_nokey INT) ENGINE=MEMORY;
 
1178
INSERT INTO t3 VALUES (7);
 
1179
SELECT MIN(t3.col_int_nokey),t1.col_int_nokey AS field3
 
1180
FROM t3
 
1181
LEFT JOIN t1
 
1182
ON t1.col_int_nokey
 
1183
WHERE (194, 200) IN (
 
1184
SELECT SQ4_alias1.col_int_nokey,
 
1185
SQ4_alias2.col_int_nokey
 
1186
FROM t2 AS SQ4_alias1
 
1187
JOIN
 
1188
t2 AS SQ4_alias2
 
1189
ON SQ4_alias2.col_int_nokey = 5
 
1190
)
 
1191
GROUP BY field3 ;
 
1192
MIN(t3.col_int_nokey)   field3
 
1193
DROP TABLE t1;
 
1194
DROP TABLE t2;
 
1195
DROP TABLE t3;
 
1196
#
 
1197
# Bug#13419028 - SUBQUERY MATERIALIZATION NOT USED IN CREATE
 
1198
# SELECT
 
1199
#
 
1200
CREATE TABLE t1(a int);
 
1201
INSERT INTO t1 values(1),(2);
 
1202
CREATE TABLE t2(a int);
 
1203
INSERT INTO t2 values(1),(2);
 
1204
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
 
1205
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1206
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       NULL
 
1207
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    2       Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
1208
FLUSH STATUS;
 
1209
SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
 
1210
a
 
1211
1
 
1212
2
 
1213
CREATE TABLE t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2);
 
1214
SELECT * FROM t3;
 
1215
a
 
1216
1
 
1217
2
 
1218
SHOW STATUS LIKE "CREATED_TMP_TABLES";
 
1219
Variable_name   Value
 
1220
Created_tmp_tables      0
 
1221
DROP TABLE t1,t2,t3;
 
1222
#
 
1223
# Bug#13552968: Extra row with materialization on join + subquery in
 
1224
#
 
1225
CREATE TABLE t1 (
 
1226
col_varchar_nokey varchar(1) NOT NULL
 
1227
) ENGINE=MyISAM;
 
1228
INSERT INTO t1 VALUES ('b');
 
1229
CREATE TABLE t2 (
 
1230
col_varchar_nokey varchar(1) NOT NULL
 
1231
) ENGINE=MyISAM;
 
1232
INSERT INTO t2 VALUES ('k');
 
1233
CREATE TABLE t3 (
 
1234
col_varchar_nokey varchar(1) NOT NULL
 
1235
) ENGINE=MyISAM;
 
1236
explain SELECT STRAIGHT_JOIN *
 
1237
FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
 
1238
FROM t3);
 
1239
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1240
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    1       NULL
 
1241
1       PRIMARY t2      system  NULL    NULL    NULL    NULL    1       NULL
 
1242
2       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1243
SELECT STRAIGHT_JOIN *
 
1244
FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
 
1245
FROM t3);
 
1246
col_varchar_nokey       col_varchar_nokey
 
1247
b       NULL
 
1248
DROP TABLE t1, t2, t3;
 
1249
# End of test for bug#13552968
 
1250
#
 
1251
# Bug#13591383: Assertion !(*tab->on_expr_ref && .. && is_expensive())
 
1252
# in join_read_const_table()
 
1253
#
 
1254
CREATE TABLE t1 (v INTEGER) ENGINE=MyISAM;
 
1255
INSERT INTO t1 VALUES(1);
 
1256
CREATE TABLE t2 (v INTEGER) ENGINE=MyISAM;
 
1257
SELECT *
 
1258
FROM t1 LEFT JOIN t2
 
1259
ON t2.v IN(SELECT v FROM t1);
 
1260
v       v
 
1261
1       NULL
 
1262
DROP TABLE t1, t2;
 
1263
# End of test for bug#13591383.
 
1264
#
 
1265
# Bug#13607423: Assertion !(*tab->on_expr_ref->is_expensive())
 
1266
# in join_read_const_table()
 
1267
#
 
1268
CREATE TABLE t1 (
 
1269
pk int NOT NULL,
 
1270
col_int_nokey int DEFAULT NULL,
 
1271
col_int_key int DEFAULT NULL,
 
1272
PRIMARY KEY (pk),
 
1273
KEY col_int_key (col_int_key)
 
1274
) ENGINE=MyISAM;
 
1275
INSERT INTO t1 VALUES (1,2,4), (2,150,62);
 
1276
CREATE TABLE t2 (
 
1277
pk int NOT NULL,
 
1278
col_int_key int DEFAULT NULL,
 
1279
PRIMARY KEY (pk)
 
1280
) ENGINE=MyISAM;
 
1281
INSERT INTO t2 VALUES (1,7);
 
1282
explain SELECT table1.pk, table2.pk
 
1283
FROM t2 AS table1 LEFT JOIN t2 AS table2
 
1284
ON table2.pk = table1.pk AND
 
1285
table2.col_int_key IN
 
1286
(SELECT col_int_key
 
1287
FROM t1 AS innr
 
1288
WHERE innr.col_int_nokey > innr.col_int_nokey
 
1289
GROUP BY col_int_key
 
1290
HAVING COUNT(*) > 0
 
1291
);
 
1292
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1293
1       PRIMARY table1  system  NULL    NULL    NULL    NULL    1       NULL
 
1294
1       PRIMARY table2  system  PRIMARY NULL    NULL    NULL    1       NULL
 
1295
2       DEPENDENT SUBQUERY      innr    ALL     col_int_key     NULL    NULL    NULL    2       Using where; Using temporary; Using filesort
 
1296
FLUSH STATUS;
 
1297
SELECT table1.pk, table2.pk
 
1298
FROM t2 AS table1 LEFT JOIN t2 AS table2
 
1299
ON table2.pk = table1.pk AND
 
1300
table2.col_int_key IN
 
1301
(SELECT col_int_key
 
1302
FROM t1 AS innr
 
1303
WHERE innr.col_int_nokey > innr.col_int_nokey
 
1304
GROUP BY col_int_key
 
1305
HAVING COUNT(*) > 0
 
1306
);
 
1307
pk      pk
 
1308
1       NULL
 
1309
SHOW SESSION STATUS LIKE 'Sort_scan%';
 
1310
Variable_name   Value
 
1311
Sort_scan       1
 
1312
DROP TABLE t1, t2;
 
1313
# End of test for bug#13607423.
 
1314
 
 
1315
Test of WL#6094 "Allow subquery materialization in NOT IN if all
 
1316
columns are not nullable"
 
1317
 
 
1318
create table t1(a int not null);
 
1319
create table t2(a int not null);
 
1320
insert into t1 values(1),(2);
 
1321
insert into t2 values(1),(2);
 
1322
Test in SELECT list
 
1323
 
 
1324
cols not nullable => subq materialization
 
1325
explain extended select a, (a,a) in (select a,a from t2) from t1;
 
1326
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1327
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1328
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1329
Warnings:
 
1330
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,<in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a`),(`test`.`t1`.`a`,`test`.`t1`.`a`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`a`,`test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`a`) and (`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `(a,a) in (select a,a from t2)` from `test`.`t1`
 
1331
select a, (a,a) in (select a,a from t2) from t1;
 
1332
a       (a,a) in (select a,a from t2)
 
1333
1       1
 
1334
2       1
 
1335
 
 
1336
cols not nullable => subq materialization
 
1337
explain extended select t1.a, t2.a, (t1.a,t1.a) in (select a,a from t2 as t3)
 
1338
from t1 join t2 on t1.a+t2.a=1000;
 
1339
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1340
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1341
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1342
2       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1343
Warnings:
 
1344
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,<in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a`),(`test`.`t1`.`a`,`test`.`t1`.`a`) in ( <materialize> (/* select#2 */ select `test`.`t3`.`a`,`test`.`t3`.`a` from `test`.`t2` `t3` where 1 having 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`a`) and (`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `(t1.a,t1.a) in (select a,a from t2 as t3)` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` + `test`.`t2`.`a`) = 1000)
 
1345
select t1.a, t2.a, (t1.a,t1.a) in (select a,a from t2 as t3)
 
1346
from t1 join t2 on t1.a+t2.a=1000;
 
1347
a       a       (t1.a,t1.a) in (select a,a from t2 as t3)
 
1348
 
 
1349
t2.a is not nullable, but in the query it may appear as NULL
 
1350
as it's in an outer join. So, no materialization.
 
1351
explain extended select t1.a, t2.a, (t2.a,t2.a) in (select a,a from t2 as t3)
 
1352
from t1 left join t2 on t1.a+t2.a=1000;
 
1353
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1354
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1355
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1356
2       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
1357
Warnings:
 
1358
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`a`),<exists>(/* select#2 */ select 1,1 from `test`.`t2` `t3` where (<if>(outer_field_is_not_null, ((<cache>(`test`.`t2`.`a`) = `test`.`t3`.`a`) or <cache>(isnull(`test`.`t3`.`a`))), true) and <if>(outer_field_is_not_null, ((<cache>(`test`.`t2`.`a`) = `test`.`t3`.`a`) or <cache>(isnull(`test`.`t3`.`a`))), true)) having (<if>(outer_field_is_not_null, <is_not_null_test>(`test`.`t3`.`a`), true) and <if>(outer_field_is_not_null, <is_not_null_test>(`test`.`t3`.`a`), true)))) AS `(t2.a,t2.a) in (select a,a from t2 as t3)` from `test`.`t1` left join `test`.`t2` on(((`test`.`t1`.`a` + `test`.`t2`.`a`) = 1000)) where 1
 
1359
select t1.a, t2.a, (t2.a,t2.a) in (select a,a from t2 as t3)
 
1360
from t1 left join t2 on t1.a+t2.a=1000;
 
1361
a       a       (t2.a,t2.a) in (select a,a from t2 as t3)
 
1362
1       NULL    NULL
 
1363
2       NULL    NULL
 
1364
 
 
1365
alter table t2 modify a int;
 
1366
two nullable inner cols => no subq materialization
 
1367
explain extended select t1.a, t2.a, (t1.a,t1.a) in (select a,a from t2 as t3)
 
1368
from t1 join t2 on t1.a+t2.a=1000;
 
1369
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1370
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1371
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1372
2       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
1373
Warnings:
 
1374
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,<in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a`),<exists>(/* select#2 */ select 1,1 from `test`.`t2` `t3` where (((<cache>(`test`.`t1`.`a`) = `test`.`t3`.`a`) or isnull(`test`.`t3`.`a`)) and ((<cache>(`test`.`t1`.`a`) = `test`.`t3`.`a`) or isnull(`test`.`t3`.`a`))) having (<is_not_null_test>(`test`.`t3`.`a`) and <is_not_null_test>(`test`.`t3`.`a`)))) AS `(t1.a,t1.a) in (select a,a from t2 as t3)` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` + `test`.`t2`.`a`) = 1000)
 
1375
select t1.a, t2.a, (t1.a,t1.a) in (select a,a from t2 as t3)
 
1376
from t1 join t2 on t1.a+t2.a=1000;
 
1377
a       a       (t1.a,t1.a) in (select a,a from t2 as t3)
 
1378
alter table t2 modify a int not null;
 
1379
 
 
1380
Test in WHERE
 
1381
 
 
1382
top-level => subq materialization. With one exception: if
 
1383
semijoin is enabled in @@optimizer_switch, semijoin is chosen,
 
1384
then rejected (due to outer join), and in that case, the
 
1385
fallback is IN->EXISTS, subq-materialization is not tried...
 
1386
explain extended select t1.a, t2.a
 
1387
from t1 join t2 on t1.a+t2.a=3
 
1388
where (t2.a,t2.a) in (select a,a from t2 as t3);
 
1389
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1390
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1391
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1392
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; FirstMatch(t2); Using join buffer (Block Nested Loop)
 
1393
Warnings:
 
1394
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2` `t3`) join `test`.`t2` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and ((`test`.`t1`.`a` + `test`.`t2`.`a`) = 3))
 
1395
select t1.a, t2.a
 
1396
from t1 join t2 on t1.a+t2.a=3
 
1397
where (t2.a,t2.a) in (select a,a from t2 as t3);
 
1398
a       a
 
1399
2       1
 
1400
1       2
 
1401
 
 
1402
cols not nullable => subq materialization
 
1403
explain extended select t1.a, t2.a
 
1404
from t1 join t2 on t1.a+t2.a=3
 
1405
where (t2.a,t2.a) not in (select a,a from t2 as t3);
 
1406
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1407
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1408
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1409
2       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1410
Warnings:
 
1411
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where ((not(<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`a`),(`test`.`t2`.`a`,`test`.`t2`.`a`) in ( <materialize> (/* select#2 */ select `test`.`t3`.`a`,`test`.`t3`.`a` from `test`.`t2` `t3` where 1 having 1 ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on <auto_key> where ((`test`.`t2`.`a` = `materialized-subquery`.`a`) and (`test`.`t2`.`a` = `materialized-subquery`.`a`))))))) and ((`test`.`t1`.`a` + `test`.`t2`.`a`) = 3))
 
1412
select t1.a, t2.a
 
1413
from t1 join t2 on t1.a+t2.a=3
 
1414
where (t2.a,t2.a) not in (select a,a from t2 as t3);
 
1415
a       a
 
1416
drop table t1,t2;
 
1417
 
 
1418
Test of WL6095 "Allow subquery materialization in NOT IN if
 
1419
single-column subquery"
 
1420
 
 
1421
create table t1(a int null);
 
1422
create table t2(a int null);
 
1423
insert into t1 values(1),(2);
 
1424
insert into t2 values(1),(2);
 
1425
 
 
1426
one col => subq materialization
 
1427
explain extended select a, a in (select a from t2) from t1;
 
1428
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1429
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1430
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1431
Warnings:
 
1432
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `a in (select a from t2)` from `test`.`t1`
 
1433
select a, a in (select a from t2) from t1;
 
1434
a       a in (select a from t2)
 
1435
1       1
 
1436
2       1
 
1437
 
 
1438
t2.a is not nullable, but in the query it may appear as NULL
 
1439
as it's in an outer join. But there is only one inner column so
 
1440
materialization is possible
 
1441
explain extended select t1.a, t2.a, t2.a in (select * from t2 as t3)
 
1442
from t1 left join t2 on t1.a+t2.a=1000;
 
1443
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1444
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1445
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1446
2       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1447
Warnings:
 
1448
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t3`.`a` from `test`.`t2` `t3` where 1 having 1 ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on <auto_key> where ((`test`.`t2`.`a` = `materialized-subquery`.`a`))))) AS `t2.a in (select * from t2 as t3)` from `test`.`t1` left join `test`.`t2` on(((`test`.`t1`.`a` + `test`.`t2`.`a`) = 1000)) where 1
 
1449
select t1.a, t2.a, t2.a in (select * from t2 as t3)
 
1450
from t1 left join t2 on t1.a+t2.a=1000;
 
1451
a       a       t2.a in (select * from t2 as t3)
 
1452
1       NULL    NULL
 
1453
2       NULL    NULL
 
1454
 
 
1455
_two_ outer columns, nullable => no materialization
 
1456
explain extended select t1.a, t2.a, (t2.a,t2.a) in (select a,a from t2 as t3)
 
1457
from t1 left join t2 on t1.a+t2.a=1000;
 
1458
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1459
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1460
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using join buffer (Block Nested Loop)
 
1461
2       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
1462
Warnings:
 
1463
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `a`,<in_optimizer>((`test`.`t2`.`a`,`test`.`t2`.`a`),<exists>(/* select#2 */ select 1,1 from `test`.`t2` `t3` where (<if>(outer_field_is_not_null, ((<cache>(`test`.`t2`.`a`) = `test`.`t3`.`a`) or isnull(`test`.`t3`.`a`)), true) and <if>(outer_field_is_not_null, ((<cache>(`test`.`t2`.`a`) = `test`.`t3`.`a`) or isnull(`test`.`t3`.`a`)), true)) having (<if>(outer_field_is_not_null, <is_not_null_test>(`test`.`t3`.`a`), true) and <if>(outer_field_is_not_null, <is_not_null_test>(`test`.`t3`.`a`), true)))) AS `(t2.a,t2.a) in (select a,a from t2 as t3)` from `test`.`t1` left join `test`.`t2` on(((`test`.`t1`.`a` + `test`.`t2`.`a`) = 1000)) where 1
 
1464
select t1.a, t2.a, (t2.a,t2.a) in (select a,a from t2 as t3)
 
1465
from t1 left join t2 on t1.a+t2.a=1000;
 
1466
a       a       (t2.a,t2.a) in (select a,a from t2 as t3)
 
1467
1       NULL    NULL
 
1468
2       NULL    NULL
 
1469
drop table t1,t2;
 
1470
 
 
1471
Test in HAVING
 
1472
create table t1(a int, b int);
 
1473
create table t2(a int);
 
1474
insert into t1 values(1,1),(1,2),(1,3),(2,1),(2,2),(2,3);
 
1475
insert into t2 values(10),(20);
 
1476
no NULLs.
 
1477
explain extended select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1478
having (z in (select * from t2)) is null;
 
1479
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1480
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    6       100.00  Using temporary; Using filesort
 
1481
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1482
Warnings:
 
1483
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `z`,sum(`test`.`t1`.`b`) AS `sum(t1.b)` from `test`.`t1` group by `test`.`t1`.`a` having isnull(<in_optimizer>(`z`,`z` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`z` in <temporary table> on <auto_key> where ((`z` = `materialized-subquery`.`a`))))))
 
1484
select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1485
having (z in (select * from t2)) is null;
 
1486
z       sum(t1.b)
 
1487
one outer NULL
 
1488
insert into t1 values(null,null);
 
1489
explain extended select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1490
having (z in (select * from t2)) is null;
 
1491
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1492
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    7       100.00  Using temporary; Using filesort
 
1493
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       100.00  NULL
 
1494
Warnings:
 
1495
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `z`,sum(`test`.`t1`.`b`) AS `sum(t1.b)` from `test`.`t1` group by `test`.`t1`.`a` having isnull(<in_optimizer>(`z`,`z` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`z` in <temporary table> on <auto_key> where ((`z` = `materialized-subquery`.`a`))))))
 
1496
select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1497
having (z in (select * from t2)) is null;
 
1498
z       sum(t1.b)
 
1499
NULL    NULL
 
1500
one outer NULL and one inner NULL
 
1501
insert into t2 values(null);
 
1502
explain extended select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1503
having (z in (select * from t2)) is null;
 
1504
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1505
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    7       100.00  Using temporary; Using filesort
 
1506
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
1507
Warnings:
 
1508
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `z`,sum(`test`.`t1`.`b`) AS `sum(t1.b)` from `test`.`t1` group by `test`.`t1`.`a` having isnull(<in_optimizer>(`z`,`z` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`z` in <temporary table> on <auto_key> where ((`z` = `materialized-subquery`.`a`))))))
 
1509
select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1510
having (z in (select * from t2)) is null;
 
1511
z       sum(t1.b)
 
1512
NULL    NULL
 
1513
1       6
 
1514
2       6
 
1515
one inner NULL
 
1516
delete from t1 where a is null;
 
1517
explain extended select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1518
having (z in (select * from t2)) is null;
 
1519
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1520
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    6       100.00  Using temporary; Using filesort
 
1521
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
1522
Warnings:
 
1523
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `z`,sum(`test`.`t1`.`b`) AS `sum(t1.b)` from `test`.`t1` group by `test`.`t1`.`a` having isnull(<in_optimizer>(`z`,`z` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`z` in <temporary table> on <auto_key> where ((`z` = `materialized-subquery`.`a`))))))
 
1524
select t1.a as z, sum(t1.b) from t1 group by t1.a
 
1525
having (z in (select * from t2)) is null;
 
1526
z       sum(t1.b)
 
1527
1       6
 
1528
2       6
 
1529
drop table t1,t2;
 
1530
 
 
1531
Verify that an inner NULL is looked up only once (result is
 
1532
cached).
 
1533
create table t1(a int);
 
1534
create table t2(a int);
 
1535
insert into t1 values(1),(2),(3),(4),(5),(6);
 
1536
insert into t1 select * from t1;
 
1537
insert into t2 values(10),(20),(NULL);
 
1538
explain extended select a, (a in (select * from t2)) from t1;
 
1539
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1540
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    12      100.00  NULL
 
1541
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    3       100.00  NULL
 
1542
Warnings:
 
1543
Note    1003    /* select#1 */ select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `(a in (select * from t2))` from `test`.`t1`
 
1544
flush status;
 
1545
select a, (a in (select * from t2)) from t1;
 
1546
a       (a in (select * from t2))
 
1547
1       NULL
 
1548
2       NULL
 
1549
3       NULL
 
1550
4       NULL
 
1551
5       NULL
 
1552
6       NULL
 
1553
1       NULL
 
1554
2       NULL
 
1555
3       NULL
 
1556
4       NULL
 
1557
5       NULL
 
1558
6       NULL
 
1559
There will be one look-up in the temporary table for each row
 
1560
of t1 (12), plus one additional look-up to check whether table
 
1561
contains a NULL value.
 
1562
show status like "handler_read_key";
 
1563
Variable_name   Value
 
1564
Handler_read_key        13
 
1565
drop table t1,t2;
 
1566
#
 
1567
# Bug#13495157 - SUBQUERY MATERIALIZATION NOT USED FOR CERTAIN
 
1568
# STATEMENTS
 
1569
#
 
1570
CREATE TABLE t1(a INT);
 
1571
INSERT INTO t1 VALUES(1),(2),(3);
 
1572
CREATE TABLE t2(a INT);
 
1573
INSERT INTO t2 VALUES(1),(2),(4);
 
1574
# subquery materialization used for SELECT:
 
1575
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1576
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1577
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1578
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
1579
SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1580
a
 
1581
1
 
1582
# Also used for INSERT SELECT:
 
1583
CREATE TABLE t3 SELECT * FROM t1;
 
1584
EXPLAIN INSERT INTO t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1585
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1586
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1587
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
1588
INSERT INTO t3 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1589
SELECT * FROM t3;
 
1590
a
 
1591
1
 
1592
1
 
1593
2
 
1594
3
 
1595
EXPLAIN INSERT INTO t2 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1596
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1597
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using where; Using temporary
 
1598
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
1599
INSERT INTO t2 SELECT * FROM t1 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1600
SELECT * FROM t2;
 
1601
a
 
1602
1
 
1603
1
 
1604
2
 
1605
4
 
1606
EXPLAIN INSERT INTO t2 SELECT * FROM t2 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1607
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1608
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    4       Using where; Using temporary
 
1609
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    4       Using where; FirstMatch(t2); Using join buffer (Block Nested Loop)
 
1610
INSERT INTO t2 SELECT * FROM t2 WHERE a IN (SELECT * FROM t2 WHERE a <> 2);
 
1611
SELECT * FROM t2;
 
1612
a
 
1613
1
 
1614
1
 
1615
1
 
1616
1
 
1617
2
 
1618
4
 
1619
4
 
1620
# Not used for single-table UPDATE, DELETE:
 
1621
EXPLAIN SELECT * FROM t2 WHERE a IN (SELECT * FROM t1);
 
1622
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1623
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    NULL
 
1624
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    7       Using where; Using join buffer (Block Nested Loop)
 
1625
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    3       NULL
 
1626
EXPLAIN UPDATE t2 SET a=a-1 WHERE a IN (SELECT * FROM t1);
 
1627
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1628
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    7       Using where
 
1629
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1630
UPDATE t2 SET a=a-1 WHERE a IN (SELECT * FROM t1);
 
1631
SELECT * FROM t2;
 
1632
a
 
1633
0
 
1634
0
 
1635
0
 
1636
0
 
1637
1
 
1638
4
 
1639
4
 
1640
EXPLAIN DELETE FROM t2 WHERE a IN (SELECT * FROM t1);
 
1641
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1642
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    7       Using where
 
1643
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1644
DELETE FROM t2 WHERE a IN (SELECT * FROM t1);
 
1645
SELECT * FROM t2;
 
1646
a
 
1647
0
 
1648
0
 
1649
0
 
1650
0
 
1651
4
 
1652
4
 
1653
EXPLAIN UPDATE t2 SET a=a-1 WHERE a IN (SELECT * FROM t2);
 
1654
ERROR HY000: You can't specify target table 't2' for update in FROM clause
 
1655
EXPLAIN DELETE FROM t2 WHERE a IN (SELECT * FROM t2);
 
1656
ERROR HY000: You can't specify target table 't2' for update in FROM clause
 
1657
UPDATE t2 SET a=3 WHERE a=0;
 
1658
# Used for multi-table UPDATE, DELETE:
 
1659
EXPLAIN SELECT * FROM t2,t3 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
 
1660
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1661
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    NULL
 
1662
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    6       Using where; Using join buffer (Block Nested Loop)
 
1663
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    4       Using join buffer (Block Nested Loop)
 
1664
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1665
EXPLAIN UPDATE t2,t3 SET t2.a=t2.a-2 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
 
1666
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1667
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    NULL
 
1668
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    6       Using where; Using join buffer (Block Nested Loop)
 
1669
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    4       Using join buffer (Block Nested Loop)
 
1670
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1671
UPDATE t2,t3 SET t2.a=t2.a-2 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
 
1672
SELECT * FROM t2;
 
1673
a
 
1674
1
 
1675
1
 
1676
1
 
1677
1
 
1678
4
 
1679
4
 
1680
EXPLAIN DELETE t2.* FROM t2,t3 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
 
1681
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1682
1       SIMPLE  <subquery2>     ALL     NULL    NULL    NULL    NULL    NULL    NULL
 
1683
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    6       Using where; Using join buffer (Block Nested Loop)
 
1684
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    4       Using join buffer (Block Nested Loop)
 
1685
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
1686
DELETE t2.* FROM t2,t3 WHERE t2.a IN (SELECT * FROM t1 WHERE a <> 2);
 
1687
SELECT * FROM t2;
 
1688
a
 
1689
4
 
1690
4
 
1691
EXPLAIN UPDATE t2,t3 SET t2.a=10 WHERE t2.a IN (SELECT * FROM t2 WHERE a <> 2);
 
1692
ERROR HY000: You can't specify target table 't2' for update in FROM clause
 
1693
EXPLAIN DELETE t2.* FROM t2,t3 WHERE t2.a IN (SELECT * FROM t2 WHERE a <> 2);
 
1694
ERROR HY000: You can't specify target table 't2' for update in FROM clause
 
1695
DROP TABLE t1,t2,t3;
 
1696
#
 
1697
# Test that subquery materialization only does one lookup: does
 
1698
# not try to read the next row if the first row failed the
 
1699
# subquery's WHERE. We use a case where index lookup is not
 
1700
# enough to satisfy IN(), because index has length two when the
 
1701
# outer value has length three, and thus the post-filtering
 
1702
# WHERE added by subselect_hash_sj_engine::setup() makes the
 
1703
# decision.
 
1704
#
 
1705
create table t1 (a varchar(3));
 
1706
create table t2 (a varchar(2));
 
1707
insert into t1 values('aaa'), ('aaa');
 
1708
insert into t2 values('aa'), ('aa');
 
1709
explain select * from t1 where a in (select a from t2);
 
1710
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1711
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    2       NULL
 
1712
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    2       Using where; FirstMatch(t1); Using join buffer (Block Nested Loop)
 
1713
flush status;
 
1714
select * from t1 where a in (select a from t2);
 
1715
a
 
1716
show status like "handler_read%";
 
1717
Variable_name   Value
 
1718
Handler_read_first      0
 
1719
Handler_read_key        0
 
1720
Handler_read_last       0
 
1721
Handler_read_next       0
 
1722
Handler_read_prev       0
 
1723
Handler_read_rnd        0
 
1724
Handler_read_rnd_next   6
 
1725
drop table t1,t2;
 
1726
#
 
1727
# Bug#13655791 DIFFERENT RESULT WITH WL6094 ON QUERY WITH XOR
 
1728
# IN WHERE CLAUSE + MYISAM
 
1729
#
 
1730
CREATE TABLE t1 (
 
1731
pk int NOT NULL,
 
1732
col_varchar_nokey varchar(1) DEFAULT NULL,
 
1733
PRIMARY KEY (pk)
 
1734
);
 
1735
INSERT INTO t1 VALUES (10,'x');
 
1736
CREATE TABLE t2 (
 
1737
pk int NOT NULL,
 
1738
col_varchar_nokey varchar(1) DEFAULT NULL,
 
1739
PRIMARY KEY (pk)
 
1740
);
 
1741
INSERT INTO t2 VALUES (1,'v'), (5,'x'), (11,'z'), (12,'c'), (15,'y');
 
1742
CREATE TABLE t3 (
 
1743
pk int NOT NULL,
 
1744
col_int_key int DEFAULT NULL,
 
1745
PRIMARY KEY (pk),
 
1746
KEY col_int_key (col_int_key)
 
1747
);
 
1748
INSERT INTO t3 VALUES (10,8);
 
1749
CREATE TABLE t4 (
 
1750
pk int NOT NULL,
 
1751
col_varchar_nokey varchar(1) DEFAULT NULL,
 
1752
PRIMARY KEY (pk)
 
1753
);
 
1754
INSERT INTO t4 VALUES (1,'x');
 
1755
EXPLAIN SELECT OUTR.pk, OUTR.col_varchar_nokey, OUTR2.col_varchar_nokey
 
1756
FROM t2 AS OUTR2
 
1757
JOIN t4 AS OUTR
 
1758
ON (OUTR2.col_varchar_nokey > OUTR.col_varchar_nokey)
 
1759
WHERE
 
1760
OUTR.col_varchar_nokey IN (
 
1761
SELECT INNR.col_varchar_nokey
 
1762
FROM t3 AS INNR2
 
1763
LEFT JOIN t1 AS INNR
 
1764
ON (INNR2.col_int_key >= INNR.pk)
 
1765
)
 
1766
XOR OUTR.pk < 6
 
1767
;
 
1768
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1769
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
1770
2       DEPENDENT SUBQUERY      INNR2   system  NULL    NULL    NULL    NULL    1       NULL
 
1771
2       DEPENDENT SUBQUERY      INNR    system  PRIMARY NULL    NULL    NULL    1       NULL
 
1772
FLUSH STATUS;
 
1773
SELECT OUTR.pk, OUTR.col_varchar_nokey, OUTR2.col_varchar_nokey
 
1774
FROM t2 AS OUTR2
 
1775
JOIN t4 AS OUTR
 
1776
ON (OUTR2.col_varchar_nokey > OUTR.col_varchar_nokey)
 
1777
WHERE
 
1778
OUTR.col_varchar_nokey IN (
 
1779
SELECT INNR.col_varchar_nokey
 
1780
FROM t3 AS INNR2
 
1781
LEFT JOIN t1 AS INNR
 
1782
ON (INNR2.col_int_key >= INNR.pk)
 
1783
)
 
1784
XOR OUTR.pk < 6
 
1785
;
 
1786
pk      col_varchar_nokey       col_varchar_nokey
 
1787
SHOW STATUS LIKE "HANDLER_READ%";
 
1788
Variable_name   Value
 
1789
Handler_read_first      3
 
1790
Handler_read_key        0
 
1791
Handler_read_last       0
 
1792
Handler_read_next       0
 
1793
Handler_read_prev       0
 
1794
Handler_read_rnd        0
 
1795
Handler_read_rnd_next   3
 
1796
DROP TABLE t1,t2,t3,t4;
 
1797
#
 
1798
# Bug#13727407: Assert !item->const_item() || !item->not_null_tables()
 
1799
#
 
1800
CREATE TABLE t1 (
 
1801
col_int_key INT,
 
1802
KEY col_int_key (col_int_key)
 
1803
);
 
1804
INSERT INTO t1 VALUES (1);
 
1805
CREATE TABLE t2 (
 
1806
col_int_key INT,
 
1807
col_time_key TIME,
 
1808
col_datetime_nokey DATETIME,
 
1809
KEY col_int_key (col_int_key),
 
1810
KEY col_time_key (col_time_key)
 
1811
);
 
1812
INSERT INTO t2 VALUES
 
1813
(7,'14:03:03','2001-11-28 00:50:27'), (1,'01:46:09','2007-10-09 19:53:04');
 
1814
EXPLAIN SELECT col_datetime_nokey AS x
 
1815
FROM t2 AS outr
 
1816
WHERE col_int_key IN (
 
1817
SELECT STRAIGHT_JOIN col_int_key
 
1818
FROM t1
 
1819
) AND outr.col_int_key = 0
 
1820
HAVING x = '2000-09-09'
 
1821
ORDER BY col_time_key;
 
1822
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1823
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
1824
2       DEPENDENT SUBQUERY      t1      system  col_int_key     NULL    NULL    NULL    1       NULL
 
1825
SELECT col_datetime_nokey AS x
 
1826
FROM t2 AS outr
 
1827
WHERE col_int_key IN (
 
1828
SELECT STRAIGHT_JOIN col_int_key
 
1829
FROM t1
 
1830
) AND outr.col_int_key = 0
 
1831
HAVING x = '2000-09-09'
 
1832
ORDER BY col_time_key;
 
1833
x
 
1834
DROP TABLE t1, t2;
 
1835
#
 
1836
# Bug#13838501 ASSERTION `TABLE->FILE->INITED' FAILED IN
 
1837
# SUBSELECT_HASH_SJ_ENGINE::EXEC
 
1838
#
 
1839
CREATE TABLE t1
 
1840
(c1 bigint,c2 char,pk INT,c3 char,c4 int,c5 INT,key (c5))
 
1841
ENGINE=InnoDB;
 
1842
INSERT INTO t1 VALUES (763078661862588416,0,1,'',1,'');
 
1843
Warnings:
 
1844
Warning 1366    Incorrect integer value: '' for column 'c5' at row 1
 
1845
CREATE TABLE t2 (c4k int,c4 int,cminnuk INT,key (cminnuk)) ENGINE=InnoDB;
 
1846
INSERT INTO t2 VALUES(0,'','');
 
1847
Warnings:
 
1848
Warning 1366    Incorrect integer value: '' for column 'c4' at row 1
 
1849
Warning 1366    Incorrect integer value: '' for column 'cminnuk' at row 1
 
1850
CREATE TABLE t3
 
1851
(c4 int,pk INT,c1 bigint,cyk year,cy year,key (cyk))
 
1852
ENGINE=InnoDB;
 
1853
INSERT INTO t3 VALUES(0,8,'',0,'');
 
1854
Warnings:
 
1855
Warning 1366    Incorrect integer value: '' for column 'c1' at row 1
 
1856
Warning 1366    Incorrect integer value: '' for column 'cy' at row 1
 
1857
EXPLAIN SELECT o.c2 AS x FROM t1 AS o
 
1858
WHERE o.c1 IN
 
1859
(SELECT innr.c4 AS y
 
1860
FROM t2 AS innr2 JOIN t3 AS innr
 
1861
ON (innr2.c4k=innr.c4)
 
1862
WHERE innr.c1=6 OR NOT innr.c1=innr.pk
 
1863
ORDER BY innr.c4)
 
1864
AND o.c4=7 XOR o.pk=3 ORDER BY o.pk;
 
1865
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1866
1       PRIMARY o       ALL     NULL    NULL    NULL    NULL    1       Using where; Using filesort
 
1867
2       SUBQUERY        innr2   ALL     NULL    NULL    NULL    NULL    1       NULL
 
1868
2       SUBQUERY        innr    ALL     NULL    NULL    NULL    NULL    1       Using where; Using join buffer (Block Nested Loop)
 
1869
SELECT o.c2 AS x FROM t1 AS o
 
1870
WHERE o.c1 IN
 
1871
(SELECT innr.c4 AS y
 
1872
FROM t2 AS innr2 JOIN t3 AS innr
 
1873
ON (innr2.c4k=innr.c4)
 
1874
WHERE innr.c1=6 OR NOT innr.c1=innr.pk
 
1875
ORDER BY innr.c4)
 
1876
AND o.c4=7 XOR o.pk=3 ORDER BY o.pk;
 
1877
x
 
1878
DROP TABLE t1,t2,t3;
 
1879
# End of 5.6 tests
 
1880
set @@optimizer_switch=@old_opt_switch;
 
1881
set optimizer_switch=default;