1
drop table if exists t1,t2;
2
create temporary table t1 (a int, b int not null,unique key (a,b),index(b)) engine=myisam;
3
insert ignore into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(null,7),(9,9),(8,8),(7,7),(null,9),(null,9),(6,6);
4
explain select * from t1 where a is null;
5
id select_type table type possible_keys key key_len ref rows Extra
6
1 SIMPLE t1 ref a a 5 const 3 Using where; Using index
7
explain select * from t1 where a is null and b = 2;
8
id select_type table type possible_keys key key_len ref rows Extra
9
1 SIMPLE t1 ref a,b a 9 const,const 1 Using where; Using index
10
explain select * from t1 where a is null and b = 7;
11
id select_type table type possible_keys key key_len ref rows Extra
12
1 SIMPLE t1 ref a,b a 9 const,const 1 Using where; Using index
13
explain select * from t1 where a=2 and b = 2;
14
id select_type table type possible_keys key key_len ref rows Extra
15
1 SIMPLE t1 const a,b a 9 const,const 1 Using index
16
explain select * from t1 where a<=>b limit 2;
17
id select_type table type possible_keys key key_len ref rows Extra
18
1 SIMPLE t1 index NULL a 9 NULL 12 Using where; Using index
19
explain select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
20
id select_type table type possible_keys key key_len ref rows Extra
21
1 SIMPLE t1 range a,b a 9 NULL 3 Using where; Using index
22
explain select * from t1 where (a is null or a = 7) and b=7;
23
id select_type table type possible_keys key key_len ref rows Extra
24
1 SIMPLE t1 ref_or_null a,b a 9 const,const 2 Using index
25
explain select * from t1 where (a is null or a = 7) and b=7 order by a;
26
id select_type table type possible_keys key key_len ref rows Extra
27
1 SIMPLE t1 ref_or_null a,b a 9 const,const 2 Using index; Using filesort
28
explain select * from t1 where (a is null and b>a) or a is null and b=7 limit 2;
29
id select_type table type possible_keys key key_len ref rows Extra
30
1 SIMPLE t1 ref a,b a 5 const 3 Using where; Using index
31
explain select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
32
id select_type table type possible_keys key key_len ref rows Extra
33
1 SIMPLE t1 range a,b a 9 NULL 3 Using where; Using index
34
explain select * from t1 where a > 1 and a < 3 limit 1;
35
id select_type table type possible_keys key key_len ref rows Extra
36
1 SIMPLE t1 range a a 5 NULL 1 Using where; Using index
37
explain select * from t1 where a > 8 and a < 9;
38
id select_type table type possible_keys key key_len ref rows Extra
39
1 SIMPLE t1 range a a 5 NULL 1 Using where; Using index
40
select * from t1 where a is null;
45
select * from t1 where a is null and b = 7;
48
select * from t1 where a<=>b limit 2;
52
select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
56
select * from t1 where (a is null or a > 0 and a < 3) and b > 7 limit 3;
60
select * from t1 where (a is null or a = 7) and b=7;
64
select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
69
select * from t1 where a > 1 and a < 3 limit 1;
72
select * from t1 where a > 8 and a < 9;
74
create table t2 like t1;
75
ERROR HY000: Can't create table 'test.t2' (errno: 138)
76
create temporary table t2 like t1;
79
t2 CREATE TEMPORARY TABLE `t2` (
82
UNIQUE KEY `a` (`a`,`b`),
85
insert into t2 select * from t1;
86
alter table t1 modify b blob not null, add c int not null, drop key a, add unique key (a,b(20),c), drop key b, add key (b(10));
87
explain select * from t1 where a is null and b = 2;
88
id select_type table type possible_keys key key_len ref rows Extra
89
1 SIMPLE t1 ref a,b a 5 const X Using where
90
explain select * from t1 where a is null and b = 2 and c=0;
91
id select_type table type possible_keys key key_len ref rows Extra
92
1 SIMPLE t1 ref a,b a 5 const X Using where
93
explain select * from t1 where a is null and b = 7 and c=0;
94
id select_type table type possible_keys key key_len ref rows Extra
95
1 SIMPLE t1 ref a,b a 5 const X Using where
96
explain select * from t1 where a=2 and b = 2;
97
id select_type table type possible_keys key key_len ref rows Extra
98
1 SIMPLE t1 ref a,b a 5 const X Using where
99
explain select * from t1 where a<=>b limit 2;
100
id select_type table type possible_keys key key_len ref rows Extra
101
1 SIMPLE t1 ALL NULL NULL NULL NULL X Using where
102
explain select * from t1 where (a is null or a > 0 and a < 3) and b < 5 and c=0 limit 3;
103
id select_type table type possible_keys key key_len ref rows Extra
104
1 SIMPLE t1 range a,b a 5 NULL X Using where
105
explain select * from t1 where (a is null or a = 7) and b=7 and c=0;
106
id select_type table type possible_keys key key_len ref rows Extra
107
1 SIMPLE t1 ref_or_null a,b a 5 const X Using where
108
explain select * from t1 where (a is null and b>a) or a is null and b=7 limit 2;
109
id select_type table type possible_keys key key_len ref rows Extra
110
1 SIMPLE t1 ref a,b a 5 const X Using where
111
explain select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
112
id select_type table type possible_keys key key_len ref rows Extra
113
1 SIMPLE t1 ref a,b a 5 const X Using where
114
explain select * from t1 where a > 1 and a < 3 limit 1;
115
id select_type table type possible_keys key key_len ref rows Extra
116
1 SIMPLE t1 range a a 5 NULL X Using where
117
explain select * from t1 where a is null and b=7 or a > 1 and a < 3 limit 1;
118
id select_type table type possible_keys key key_len ref rows Extra
119
1 SIMPLE t1 range a,b a 5 NULL X Using where
120
explain select * from t1 where a > 8 and a < 9;
121
id select_type table type possible_keys key key_len ref rows Extra
122
1 SIMPLE t1 range a a 5 NULL X Using where
123
explain select * from t1 where b like "6%";
124
id select_type table type possible_keys key key_len ref rows Extra
125
1 SIMPLE t1 range b b 12 NULL X Using where
126
select * from t1 where a is null;
131
select * from t1 where a is null and b = 7 and c=0;
134
select * from t1 where a<=>b limit 2;
138
select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
142
select * from t1 where (a is null or a > 0 and a < 3) and b > 7 limit 3;
146
select * from t1 where (a is null or a = 7) and b=7 and c=0;
150
select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
155
select * from t1 where b like "6%";
160
create temporary table t1 (a int, b int not null,unique key (a,b),index(b)) engine=myisam;
161
alter table t1 modify b int null;
162
insert into t1 values (7,null), (8,null), (8,7);
163
explain select * from t1 where a = 7 and (b=7 or b is null);
164
id select_type table type possible_keys key key_len ref rows Extra
165
1 SIMPLE t1 ref_or_null a,b a 10 const,const 2 Using index
166
select * from t1 where a = 7 and (b=7 or b is null);
169
explain select * from t1 where (a = 7 or a is null) and (b=7 or b is null);
170
id select_type table type possible_keys key key_len ref rows Extra
171
1 SIMPLE t1 ref_or_null a,b a 5 const 2 Using where; Using index
172
select * from t1 where (a = 7 or a is null) and (b=7 or b is null);
175
explain select * from t1 where (a = 7 or a is null) and (a = 7 or a is null);
176
id select_type table type possible_keys key key_len ref rows Extra
177
1 SIMPLE t1 ref_or_null a a 5 const 2 Using index
178
select * from t1 where (a = 7 or a is null) and (a = 7 or a is null);
181
create table t2 (a int);
182
insert into t2 values (7),(8);
183
explain select * from t2 straight_join t1 where t1.a=t2.a and b is null;
184
id select_type table type possible_keys key key_len ref rows Extra
185
1 SIMPLE t2 ALL NULL NULL NULL NULL X
186
1 SIMPLE t1 ref a,b b 5 const X Using where
188
explain select * from t2,t1 where t1.a=t2.a and b is null;
189
id select_type table type possible_keys key key_len ref rows Extra
190
1 SIMPLE t2 ALL NULL NULL NULL NULL X
191
1 SIMPLE t1 ref a a 10 test.t2.a,const X Using where; Using index
192
select * from t2,t1 where t1.a=t2.a and b is null;
196
explain select * from t2,t1 where t1.a=t2.a and (b= 7 or b is null);
197
id select_type table type possible_keys key key_len ref rows Extra
198
1 SIMPLE t2 ALL NULL NULL NULL NULL X
199
1 SIMPLE t1 ref_or_null a a 10 test.t2.a,const X Using index
200
select * from t2,t1 where t1.a=t2.a and (b= 7 or b is null);
205
explain select * from t2,t1 where (t1.a=t2.a or t1.a is null) and b= 7;
206
id select_type table type possible_keys key key_len ref rows Extra
207
1 SIMPLE t2 ALL NULL NULL NULL NULL X
208
1 SIMPLE t1 ref_or_null a a 10 test.t2.a,const X Using index
209
select * from t2,t1 where (t1.a=t2.a or t1.a is null) and b= 7;
212
explain select * from t2,t1 where (t1.a=t2.a or t1.a is null) and (b= 7 or b is null);
213
id select_type table type possible_keys key key_len ref rows Extra
214
1 SIMPLE t2 ALL NULL NULL NULL NULL X
215
1 SIMPLE t1 ref_or_null a a 5 test.t2.a X Using where; Using index
216
select * from t2,t1 where (t1.a=t2.a or t1.a is null) and (b= 7 or b is null);
221
insert into t2 values (null),(6);
222
delete from t1 where a=8;
223
explain select * from t2,t1 where t1.a=t2.a or t1.a is null;
224
id select_type table type possible_keys key key_len ref rows Extra
225
1 SIMPLE t1 system a NULL NULL NULL X
226
1 SIMPLE t2 ALL NULL NULL NULL NULL X Using where
227
explain select * from t2,t1 where t1.a<=>t2.a or (t1.a is null and t1.b <> 9);
228
id select_type table type possible_keys key key_len ref rows Extra
229
1 SIMPLE t1 system a NULL NULL NULL X
230
1 SIMPLE t2 ALL NULL NULL NULL NULL X Using where
231
select * from t2,t1 where t1.a<=>t2.a or (t1.a is null and t1.b <> 9);
235
CREATE TEMPORARY TABLE t1 (
236
id int NOT NULL auto_increment,
237
uniq_id int default NULL,
239
UNIQUE KEY idx1 (uniq_id)
241
CREATE TEMPORARY TABLE t2 (
242
id int NOT NULL auto_increment,
243
uniq_id int default NULL,
246
INSERT INTO t1 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL);
247
INSERT INTO t2 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL);
248
explain select id from t1 where uniq_id is null;
249
id select_type table type possible_keys key key_len ref rows Extra
250
1 SIMPLE t1 ref idx1 idx1 5 const 5 Using where
251
explain select id from t1 where uniq_id =1;
252
id select_type table type possible_keys key key_len ref rows Extra
253
1 SIMPLE t1 const idx1 idx1 5 const 1
254
UPDATE t1 SET id=id+100 where uniq_id is null;
255
UPDATE t2 SET id=id+100 where uniq_id is null;
256
select id from t1 where uniq_id is null;
264
select id from t2 where uniq_id is null;
272
DELETE FROM t1 WHERE uniq_id IS NULL;
273
DELETE FROM t2 WHERE uniq_id IS NULL;
274
SELECT * FROM t1 ORDER BY uniq_id, id;
280
SELECT * FROM t2 ORDER BY uniq_id, id;
287
CREATE TEMPORARY TABLE `t1` (
288
`order_id` char(32) NOT NULL default '',
289
`product_id` char(32) NOT NULL default '',
290
`product_type` int NOT NULL default '0',
291
PRIMARY KEY (`order_id`,`product_id`,`product_type`)
293
CREATE TEMPORARY TABLE `t2` (
294
`order_id` char(32) NOT NULL default '',
295
`product_id` char(32) NOT NULL default '',
296
`product_type` int NOT NULL default '0',
297
PRIMARY KEY (`order_id`,`product_id`,`product_type`)
299
INSERT INTO t1 (order_id, product_id, product_type) VALUES
300
('3d7ce39b5d4b3e3d22aaafe9b633de51',1206029, 3),
301
('3d7ce39b5d4b3e3d22aaafe9b633de51',5880836, 3),
302
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
303
INSERT INTO t2 (order_id, product_id, product_type) VALUES
304
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
306
left join t2 using(order_id, product_id, product_type)
307
where t2.order_id=NULL;
308
order_id product_id product_type
310
left join t2 using(order_id, product_id, product_type)
311
where t2.order_id is NULL;
312
order_id product_id product_type
313
3d7ce39b5d4b3e3d22aaafe9b633de51 1206029 3
314
3d7ce39b5d4b3e3d22aaafe9b633de51 5880836 3
316
create table t1 (id int);
317
insert into t1 values (null), (0);
318
create table t2 (id int);
319
insert into t2 values (null);
320
select * from t1, t2 where t1.id = t2.id;
322
alter table t1 add key id (id);
323
select * from t1, t2 where t1.id = t2.id;
328
id2 integer not null,
332
insert into t1 values(null,null),(1,1);
333
ERROR 23000: Column 'id2' cannot be null
336
select * from t1 where id <=> null;
338
select * from t1 where id <=> null or id > 0;
340
select * from t1 where id is null or id > 0;
342
select * from t1 where id2 <=> null or id2 > 0;
344
select * from t1 where id2 is null or id2 > 0;
346
delete from t1 where id <=> NULL;
350
CREATE TABLE t1 (a int);
351
CREATE TABLE t2 (a int, b int, INDEX idx(a));
352
CREATE TABLE t3 (b int, INDEX idx(b));
353
CREATE TABLE t4 (b int, INDEX idx(b));
354
INSERT INTO t1 VALUES (1), (2), (3), (4);
355
INSERT INTO t2 VALUES (1, 1), (3, 1);
356
INSERT INTO t3 VALUES
357
(NULL), (NULL), (NULL), (NULL), (NULL),
358
(NULL), (NULL), (NULL), (NULL), (NULL);
359
INSERT INTO t4 SELECT * FROM t3;
360
INSERT INTO t3 SELECT * FROM t4;
361
INSERT INTO t4 SELECT * FROM t3;
362
INSERT INTO t3 SELECT * FROM t4;
363
INSERT INTO t4 SELECT * FROM t3;
364
INSERT INTO t3 SELECT * FROM t4;
365
INSERT INTO t4 SELECT * FROM t3;
366
INSERT INTO t3 SELECT * FROM t4;
367
INSERT INTO t4 SELECT * FROM t3;
368
INSERT INTO t3 SELECT * FROM t4;
369
INSERT INTO t4 SELECT * FROM t3;
370
INSERT INTO t3 SELECT * FROM t4;
371
INSERT INTO t4 SELECT * FROM t3;
372
INSERT INTO t3 SELECT * FROM t4;
373
INSERT INTO t4 SELECT * FROM t3;
374
INSERT INTO t3 SELECT * FROM t4;
375
INSERT INTO t3 VALUES (2), (3);
376
ANALYZE table t1, t2, t3;
377
Table Op Msg_type Msg_text
378
test.t1 analyze status OK
379
test.t2 analyze status OK
380
test.t3 analyze status OK
381
SELECT COUNT(*) FROM t3;
384
EXPLAIN SELECT SQL_CALC_FOUND_ROWS * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
385
LEFT JOIN t3 ON t2.b=t3.b;
386
id select_type table type possible_keys key key_len ref rows Extra
387
1 SIMPLE t1 ALL NULL NULL NULL NULL X
388
1 SIMPLE t2 ref idx idx 5 test.t1.a X
389
1 SIMPLE t3 ref idx idx 5 test.t2.b X Using index
391
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
392
LEFT JOIN t3 ON t2.b=t3.b;
401
SHOW STATUS LIKE "handler_read%";
408
Handler_read_rnd_next 5
409
DROP TABLE t1,t2,t3,t4;
415
INSERT INTO t1 VALUES (0,10),(0,11),(0,12);
421
INSERT INTO t2 VALUES (3,NULL),(3,11),(3,12);
422
SELECT * FROM t2 inner join t1 WHERE ( t1.a = 0 OR t1.a IS NULL) AND t2.a = 3 AND t2.b = t1.b;