1
drop table if exists t0, t1, t2, t3;
2
create table t0 (a int);
3
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
8
insert into t1 values (1,1),(1,1),(2,2);
14
insert into t2 select a, a/2 from t0;
32
explain select * from t2 where b in (select a from t1);
33
id select_type table type possible_keys key key_len ref rows Extra
34
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Start temporary
35
1 PRIMARY t2 ref b b 5 test.t1.a 2 End temporary
36
select * from t2 where b in (select a from t1);
46
pk1 char(200), pk2 char(200), pk3 char(200),
47
primary key(pk1, pk2, pk3)
49
insert into t3 select a,a, a,a,a from t0;
50
explain select * from t3 where b in (select a from t1);
51
id select_type table type possible_keys key key_len ref rows Extra
52
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Start temporary
53
1 PRIMARY t3 ref b b 5 test.t1.a 1 End temporary
54
select * from t3 where b in (select a from t1);
58
set @save_max_heap_table_size= @@max_heap_table_size;
59
set max_heap_table_size=16384;
60
set @save_join_buffer_size = @@join_buffer_size;
61
set join_buffer_size= 8000;
63
Warning 1292 Truncated incorrect join_buffer_size value: '8000'
69
pk1 char(200), pk2 char(200),
73
A.a + 10*B.a, A.a + 10*B.a, A.a + 10*B.a, A.a + 10*B.a
74
from t0 A, t0 B where B.a <5;
75
explain select * from t3 where b in (select a from t0);
76
id select_type table type possible_keys key key_len ref rows Extra
77
1 PRIMARY t0 ALL NULL NULL NULL NULL 10 Start temporary
78
1 PRIMARY t3 ref b b 5 test.t0.a 1 End temporary
79
select * from t3 where b in (select A.a+B.a from t0 A, t0 B where B.a<5);
95
set join_buffer_size= @save_join_buffer_size;
96
set max_heap_table_size= @save_max_heap_table_size;
97
explain select * from t1 where a in (select b from t2);
98
id select_type table type possible_keys key key_len ref rows Extra
99
1 PRIMARY t2 index b b 5 NULL 10 Using index; LooseScan
100
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where; Using join buffer
106
select * from t1 where a in (select b from t2);
111
drop table t1, t2, t3;
112
set @save_join_buffer_size = @@join_buffer_size;
113
set join_buffer_size= 8000;
115
Warning 1292 Truncated incorrect join_buffer_size value: '8000'
116
create table t1 (a int, filler1 binary(200), filler2 binary(200));
117
insert into t1 select a, 'filler123456', 'filler123456' from t0;
118
insert into t1 select a+10, 'filler123456', 'filler123456' from t0;
119
create table t2 as select * from t1;
120
insert into t1 select a+20, 'filler123456', 'filler123456' from t0;
121
insert into t1 values (2, 'duplicate ok', 'duplicate ok');
122
insert into t1 values (18, 'duplicate ok', 'duplicate ok');
123
insert into t2 values (3, 'duplicate ok', 'duplicate ok');
124
insert into t2 values (19, 'duplicate ok', 'duplicate ok');
126
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
127
from t1 ot where a in (select a from t2 it);
128
id select_type table type possible_keys key key_len ref rows Extra
129
1 PRIMARY it ALL NULL NULL NULL NULL 22 Start temporary
130
1 PRIMARY ot ALL NULL NULL NULL NULL 32 Using where; End temporary; Using join buffer
132
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
133
from t1 ot where a in (select a from t2 it);
134
a mid(filler1, 1,10) Z
158
a, mid(filler1, 1,10), length(filler1)=length(filler2)
159
from t2 ot where a in (select a from t1 it);
160
id select_type table type possible_keys key key_len ref rows Extra
161
1 PRIMARY ot ALL NULL NULL NULL NULL 22 Start temporary
162
1 PRIMARY it ALL NULL NULL NULL NULL 32 Using where; End temporary; Using join buffer
164
a, mid(filler1, 1,10), length(filler1)=length(filler2)
165
from t2 ot where a in (select a from t1 it);
166
a mid(filler1, 1,10) length(filler1)=length(filler2)
189
insert into t1 select a+20, 'filler123456', 'filler123456' from t0;
190
insert into t1 select a+20, 'filler123456', 'filler123456' from t0;
192
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
193
from t1 ot where a in (select a from t2 it);
194
id select_type table type possible_keys key key_len ref rows Extra
195
1 PRIMARY it ALL NULL NULL NULL NULL 22 Start temporary
196
1 PRIMARY ot ALL NULL NULL NULL NULL 52 Using where; End temporary; Using join buffer
198
a, mid(filler1, 1,10), length(filler1)=length(filler2) as Z
199
from t1 ot where a in (select a from t2 it);
200
a mid(filler1, 1,10) Z
224
a, mid(filler1, 1,10), length(filler1)=length(filler2)
225
from t2 ot where a in (select a from t1 it);
226
id select_type table type possible_keys key key_len ref rows Extra
227
1 PRIMARY ot ALL NULL NULL NULL NULL 22 Start temporary
228
1 PRIMARY it ALL NULL NULL NULL NULL 52 Using where; End temporary; Using join buffer
230
a, mid(filler1, 1,10), length(filler1)=length(filler2)
231
from t2 ot where a in (select a from t1 it);
232
a mid(filler1, 1,10) length(filler1)=length(filler2)
256
create table t1 (a int, b int, key(a));
257
create table t2 (a int, b int, key(a));
258
create table t3 (a int, b int, key(a));
259
insert into t1 select a,a from t0;
260
insert into t2 select a,a from t0;
261
insert into t3 select a,a from t0;
262
t2 and t3 must be use 'ref', not 'ALL':
265
(select t2.a+t3.a from t1 left join (t2 join t3) on t2.a=t1.a and t3.a=t1.a);
266
id select_type table type possible_keys key key_len ref rows Extra
267
1 PRIMARY t0 ALL NULL NULL NULL NULL 10 Start temporary
268
1 PRIMARY t1 index NULL a 5 NULL 10 Using index; Using join buffer
269
1 PRIMARY t2 ref a a 5 test.t1.a 1 Using index
270
1 PRIMARY t3 ref a a 5 test.t1.a 1 Using where; Using index; End temporary
271
drop table t0, t1,t2,t3;
273
ID int(11) NOT NULL auto_increment,
274
Name char(35) NOT NULL default '',
275
Country char(3) NOT NULL default '',
276
Population int(11) NOT NULL default '0',
282
Code char(3) NOT NULL default '',
283
Name char(52) NOT NULL default '',
284
SurfaceArea float(10,2) NOT NULL default '0.00',
285
Population int(11) NOT NULL default '0',
286
Capital int(11) default NULL,
292
Country char(3) NOT NULL default '',
293
Language char(30) NOT NULL default '',
294
Percentage float(3,1) NOT NULL default '0.0',
295
PRIMARY KEY (Country, Language),
300
WHERE t2.Code IN (SELECT Country FROM t1 WHERE Population > 5000000)
302
t2.Code IN (SELECT Country FROM t3
303
WHERE Language='English' AND Percentage > 10 AND
304
t2.Population > 100000);
305
id select_type table type possible_keys key key_len ref rows Extra
306
1 PRIMARY t1 ALL Population,Country NULL NULL NULL 30 Using where; Start temporary
307
1 PRIMARY t3 eq_ref PRIMARY,Percentage PRIMARY 33 test.t1.Country,const 1 Using index condition; Using where
308
1 PRIMARY t2 eq_ref PRIMARY,Population PRIMARY 3 test.t3.Country 1 Using index condition; Using where; End temporary
311
Code char(3) NOT NULL DEFAULT '',
312
Name char(52) NOT NULL DEFAULT '',
313
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL DEFAULT 'Asia',
314
Region char(26) NOT NULL DEFAULT '',
315
SurfaceArea float(10,2) NOT NULL DEFAULT '0.00',
316
IndepYear smallint(6) DEFAULT NULL,
317
Population int(11) NOT NULL DEFAULT '0',
318
LifeExpectancy float(3,1) DEFAULT NULL,
319
GNP float(10,2) DEFAULT NULL,
320
GNPOld float(10,2) DEFAULT NULL,
321
LocalName char(45) NOT NULL DEFAULT '',
322
GovernmentForm char(45) NOT NULL DEFAULT '',
323
HeadOfState char(60) DEFAULT NULL,
324
Capital int(11) DEFAULT NULL,
325
Code2 char(2) NOT NULL DEFAULT '',
329
ID int(11) NOT NULL AUTO_INCREMENT,
330
Name char(35) NOT NULL DEFAULT '',
331
CountryCode char(3) NOT NULL DEFAULT '',
332
District char(20) NOT NULL DEFAULT '',
333
Population int(11) NOT NULL DEFAULT '0',
335
KEY CountryCode (CountryCode)
337
Fill the table with test data
338
This must not use LooseScan:
339
EXPLAIN SELECT Name FROM t1
341
SELECT t2.CountryCode FROM t2 WHERE Population > 5000000);
342
id select_type table type possible_keys key key_len ref rows Extra
343
1 PRIMARY t2 ALL CountryCode NULL NULL NULL 545 Using where; Start temporary
344
1 PRIMARY t1 eq_ref PRIMARY PRIMARY 3 test.t2.CountryCode 1 End temporary
347
SELECT t2.CountryCode FROM t2 WHERE Population > 5000000);
354
CREATE TABLE t1(a INT);
355
CREATE TABLE t2(c INT);
356
CREATE PROCEDURE p1(v1 int)
358
SELECT 1 FROM t1 WHERE a = v1 AND a IN (SELECT c FROM t2);
361
CREATE PROCEDURE p2(v1 int)
363
SELECT 1 FROM t1 WHERE a IN (SELECT c FROM t2);
366
CREATE PROCEDURE p3(v1 int)
370
t1 t01,t1 t02,t1 t03,t1 t04,t1 t05,t1 t06,t1 t07,t1 t08,
371
t1 t09,t1 t10,t1 t11,t1 t12,t1 t13,t1 t14,t1 t15,t1 t16,
372
t1 t17,t1 t18,t1 t19,t1 t20,t1 t21,t1 t22,t1 t23,t1 t24,
373
t1 t25,t1 t26,t1 t27,t1 t28,t1 t29,t1 t30,t1 t31,t1 t32,
374
t1 t33,t1 t34,t1 t35,t1 t36,t1 t37,t1 t38,t1 t39,t1 t40,
375
t1 t41,t1 t42,t1 t43,t1 t44,t1 t45,t1 t46,t1 t47,t1 t48,
376
t1 t49,t1 t50,t1 t51,t1 t52,t1 t53,t1 t54,t1 t55,t1 t56,
377
t1 t57,t1 t58,t1 t59,t1 t60
378
WHERE t01.a IN (SELECT c FROM t2);
381
CREATE PROCEDURE p4(v1 int)
385
t1 t01,t1 t02,t1 t03,t1 t04,t1 t05,t1 t06,t1 t07,t1 t08,
386
t1 t09,t1 t10,t1 t11,t1 t12,t1 t13,t1 t14,t1 t15,t1 t16,
387
t1 t17,t1 t18,t1 t19,t1 t20,t1 t21,t1 t22,t1 t23,t1 t24,
388
t1 t25,t1 t26,t1 t27,t1 t28,t1 t29,t1 t30,t1 t31,t1 t32,
389
t1 t33,t1 t34,t1 t35,t1 t36,t1 t37,t1 t38,t1 t39,t1 t40,
390
t1 t41,t1 t42,t1 t43,t1 t44,t1 t45,t1 t46,t1 t47,t1 t48,
391
t1 t49,t1 t50,t1 t51,t1 t52,t1 t53,t1 t54,t1 t55,t1 t56,
392
t1 t57,t1 t58,t1 t59,t1 t60
393
WHERE t01.a = v1 AND t01.a IN (SELECT c FROM t2);