1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
drop table if exists t0, t1, t2, t3;
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (
a int,
b int
);
insert into t1 values (1,1),(1,1),(2,2);
create table t2 (
a int,
b int,
key(b)
);
insert into t2 select a, a/2 from t0;
select * from t1;
a b
1 1
1 1
2 2
select * from t2;
a b
0 0
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
explain select * from t2 where b in (select a from t1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 10 Using where
2 SUBQUERY t1 ALL NULL NULL NULL NULL 3
select * from t2 where b in (select a from t1);
a b
1 1
2 1
3 2
4 2
create table t3 (
a int,
b int,
key(b),
pk1 char(100), pk2 char(100), pk3 char(100),
primary key(pk1, pk2, pk3)
) engine=innodb;
insert into t3 select a,a, a,a,a from t0;
explain select * from t3 where b in (select a from t1);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
2 SUBQUERY t1 ALL NULL NULL NULL NULL 3
select * from t3 where b in (select a from t1);
a b pk1 pk2 pk3
1 1 1 1 1
2 2 2 2 2
set @save_max_heap_table_size= @@max_heap_table_size;
set max_heap_table_size=16384;
set @save_join_buffer_size = @@join_buffer_size;
drop table t3;
create table t3 (
a int,
b int,
key(b),
pk1 char(100), pk2 char(100),
primary key(pk1, pk2)
) engine=innodb;
insert into t3 select
A.a + 10*B.a, A.a + 10*B.a, A.a + 10*B.a, A.a + 10*B.a
from t0 A, t0 B where B.a <5;
explain select * from t3 where b in (select a from t0);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 50 Using where
2 SUBQUERY t0 ALL NULL NULL NULL NULL 10
select * from t3 where b in (select A.a+B.a from t0 A, t0 B where B.a<5);
a b pk1 pk2
0 0 0 0
1 1 1 1
10 10 10 10
11 11 11 11
12 12 12 12
13 13 13 13
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
set join_buffer_size= @save_join_buffer_size;
set max_heap_table_size= @save_max_heap_table_size;
explain select * from t1 where a in (select b from t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
2 SUBQUERY t2 index NULL b 5 NULL 10 Using index
select * from t1;
a b
1 1
1 1
2 2
select * from t1 where a in (select b from t2);
a b
1 1
1 1
2 2
drop table t1, t2, t3;
set @save_join_buffer_size = @@join_buffer_size;
set join_buffer_size=8000;
Warnings:
Error 1292 Truncated incorrect join_buffer_size value: '8000'
create table t1 (a int, filler1 varbinary(20), filler2 varbinary(20));
insert into t1 select a, 'filler123456', 'filler123456' from t0;
insert into t1 select a+10, 'filler123456', 'filler123456' from t0;
create table t2 as select * from t1;
insert into t1 select a+20, 'filler123456', 'filler123456' from t0;
insert into t1 values (2, 'duplicate ok', 'duplicate ok');
insert into t1 values (18, 'duplicate ok', 'duplicate ok');
insert into t2 values (3, 'duplicate ok', 'duplicate ok');
insert into t2 values (19, 'duplicate ok', 'duplicate ok');
explain select
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
from t1 t0 where a in (select a from t2 it);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t0 ALL NULL NULL NULL NULL 32 Using where
2 SUBQUERY it ALL NULL NULL NULL NULL 22
select
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
from t1 t0 where a in (select a from t2 it);
a mid(filler1, 1,10) Z
0 filler1234 1
1 filler1234 1
2 filler1234 1
3 filler1234 1
4 filler1234 1
5 filler1234 1
6 filler1234 1
7 filler1234 1
8 filler1234 1
9 filler1234 1
10 filler1234 1
11 filler1234 1
12 filler1234 1
13 filler1234 1
14 filler1234 1
15 filler1234 1
16 filler1234 1
17 filler1234 1
18 filler1234 1
19 filler1234 1
2 duplicate 1
18 duplicate 1
explain select
a, mid(filler1, 1,10), length(filler1)=length(filler2)
from t2 t0 where a in (select a from t1 it);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t0 ALL NULL NULL NULL NULL 22 Using where
2 SUBQUERY it ALL NULL NULL NULL NULL 32
select
a, mid(filler1, 1,10), length(filler1)=length(filler2)
from t2 t0 where a in (select a from t1 it);
a mid(filler1, 1,10) length(filler1)=length(filler2)
0 filler1234 1
1 filler1234 1
2 filler1234 1
3 filler1234 1
4 filler1234 1
5 filler1234 1
6 filler1234 1
7 filler1234 1
8 filler1234 1
9 filler1234 1
10 filler1234 1
11 filler1234 1
12 filler1234 1
13 filler1234 1
14 filler1234 1
15 filler1234 1
16 filler1234 1
17 filler1234 1
18 filler1234 1
19 filler1234 1
3 duplicate 1
19 duplicate 1
insert into t1 select a+20, 'filler123456', 'filler123456' from t0;
insert into t1 select a+20, 'filler123456', 'filler123456' from t0;
explain select
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
from t1 t0 where a in (select a from t2 it);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t0 ALL NULL NULL NULL NULL 52 Using where
2 SUBQUERY it ALL NULL NULL NULL NULL 22
select
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
from t1 t0 where a in (select a from t2 it);
a mid(filler1, 1,10) Z
0 filler1234 1
1 filler1234 1
2 filler1234 1
3 filler1234 1
4 filler1234 1
5 filler1234 1
6 filler1234 1
7 filler1234 1
8 filler1234 1
9 filler1234 1
10 filler1234 1
11 filler1234 1
12 filler1234 1
13 filler1234 1
14 filler1234 1
15 filler1234 1
16 filler1234 1
17 filler1234 1
18 filler1234 1
19 filler1234 1
2 duplicate 1
18 duplicate 1
explain select
a, mid(filler1, 1,10), length(filler1)=length(filler2)
from t2 t0 where a in (select a from t1 it);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t0 ALL NULL NULL NULL NULL 22 Using where
2 SUBQUERY it ALL NULL NULL NULL NULL 52
select
a, mid(filler1, 1,10), length(filler1)=length(filler2)
from t2 t0 where a in (select a from t1 it);
a mid(filler1, 1,10) length(filler1)=length(filler2)
0 filler1234 1
1 filler1234 1
2 filler1234 1
3 filler1234 1
4 filler1234 1
5 filler1234 1
6 filler1234 1
7 filler1234 1
8 filler1234 1
9 filler1234 1
10 filler1234 1
11 filler1234 1
12 filler1234 1
13 filler1234 1
14 filler1234 1
15 filler1234 1
16 filler1234 1
17 filler1234 1
18 filler1234 1
19 filler1234 1
3 duplicate 1
19 duplicate 1
drop table t1, t2;
create table t1 (a int, b int, key(a));
create table t2 (a int, b int, key(a));
create table t3 (a int, b int, key(a));
insert into t1 select a,a from t0;
insert into t2 select a,a from t0;
insert into t3 select a,a from t0;
t2 and t3 must be use 'ref', not 'ALL':
explain select *
from t0 where a in
(select t2.a+t3.a from t1 left join (t2 join t3) on t2.a=t1.a and t3.a=t1.a);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t0 ALL NULL NULL NULL NULL 10 Using where
2 SUBQUERY t1 index NULL a 5 NULL 10 Using index
2 SUBQUERY t2 ref a a 5 test.t1.a 1 Using index
2 SUBQUERY t3 ref a a 5 test.t1.a 1 Using index
drop table t0, t1,t2,t3;
CREATE TABLE t1 (
ID int NOT NULL auto_increment,
Name char(35) NOT NULL default '',
Country char(3) NOT NULL default '',
Population int NOT NULL default '0',
PRIMARY KEY (ID),
INDEX (Population),
INDEX (Country)
);
CREATE TABLE t2 (
Code char(3) NOT NULL default '',
Name char(52) NOT NULL default '',
SurfaceArea float(10,2) NOT NULL default '0.00',
Population int NOT NULL default '0',
Capital int default NULL,
PRIMARY KEY (Code),
UNIQUE INDEX (Name),
INDEX (Population)
);
CREATE TABLE t3 (
Country char(3) NOT NULL default '',
Language char(30) NOT NULL default '',
Percentage float(3,1) NOT NULL default '0.0',
PRIMARY KEY (Country, Language),
INDEX (Percentage)
);
EXPLAIN
SELECT Name FROM t2
WHERE t2.Code IN (SELECT Country FROM t1 WHERE Population > 5000000)
AND
t2.Code IN (SELECT Country FROM t3
WHERE Language='English' AND Percentage > 10 AND
t2.Population > 100000);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 16 Using where
3 DEPENDENT SUBQUERY t3 unique_subquery PRIMARY,Percentage PRIMARY 136 func,const 1 Using where
2 SUBQUERY t1 range Population Population 4 NULL 1 Using where
DROP TABLE t1,t2,t3;
CREATE TABLE t1 (
Code char(3) NOT NULL DEFAULT '',
Name char(52) NOT NULL DEFAULT '',
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL DEFAULT 'Asia',
Region char(26) NOT NULL DEFAULT '',
SurfaceArea float(10,2) NOT NULL DEFAULT '0.00',
IndepYear int DEFAULT NULL,
Population int NOT NULL DEFAULT '0',
LifeExpectancy float(3,1) DEFAULT NULL,
GNP float(10,2) DEFAULT NULL,
GNPOld float(10,2) DEFAULT NULL,
LocalName char(45) NOT NULL DEFAULT '',
GovernmentForm char(45) NOT NULL DEFAULT '',
HeadOfState char(60) DEFAULT NULL,
Capital int DEFAULT NULL,
Code2 char(2) NOT NULL DEFAULT '',
PRIMARY KEY (Code)
);
CREATE TABLE t2 (
ID int NOT NULL,
Name char(35) NOT NULL DEFAULT '',
CountryCode char(3) NOT NULL DEFAULT '',
Population int NOT NULL DEFAULT '0',
PRIMARY KEY (ID),
KEY CountryCode (CountryCode)
);
Fill the table with test data
This must not use LooseScan:
EXPLAIN SELECT Name FROM t1
WHERE t1.Code IN (
SELECT t2.CountryCode FROM t2 WHERE Population > 5000000);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 31 Using where
2 SUBQUERY t2 ALL NULL NULL NULL NULL 125 Using where
SELECT Name FROM t1
WHERE t1.Code IN (
SELECT t2.CountryCode FROM t2 WHERE Population > 5000000);
Name
drop table t1, t2;
#
# MySQL BUG #42742: crash in setup_sj_materialization, Copy_field::set
#
create table t3 ( c1 date) engine=innodb;
insert into t3 values ('2009-10-22'),('2142-10-22');
create table t2 (c1 tinytext,c2 text,c6 timestamp) engine=innodb;
select * from t3;
c1
2009-10-22
2142-10-22
explain select 1 from t2 where c2 in (select 1 from t3 CROSS JOIN t2) and c1 in (select convert(c6,char(1)) from t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where
3 SUBQUERY t2 ALL NULL NULL NULL NULL 1
2 SUBQUERY t2 ALL NULL NULL NULL NULL 1
2 SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using join buffer
drop table t2, t3;
|