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 where; 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 where; 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`),
84
) ENGINE=MyISAM COLLATE = utf8_general_ci
85
insert into t2 select * from t1;
86
alter table t1 modify b blob not null, add c int DEFAULT 42 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;
133
select * from t1 where a<=>b limit 2;
137
select * from t1 where (a is null or a > 0 and a < 3) and b < 5 limit 3;
141
select * from t1 where (a is null or a > 0 and a < 3) and b > 7 limit 3;
145
select * from t1 where (a is null or a = 7) and b=7 and c=0;
147
select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
152
select * from t1 where b like "6%";
157
create temporary table t1 (a int, b int not null,unique key (a,b),index(b)) engine=myisam;
158
alter table t1 modify b int null;
159
insert into t1 values (7,null), (8,null), (8,7);
160
explain select * from t1 where a = 7 and (b=7 or b is null);
161
id select_type table type possible_keys key key_len ref rows Extra
162
1 SIMPLE t1 ref_or_null a,b a 10 const,const 2 Using where; Using index
163
select * from t1 where a = 7 and (b=7 or b is null);
166
explain select * from t1 where (a = 7 or a is null) and (b=7 or b is null);
167
id select_type table type possible_keys key key_len ref rows Extra
168
1 SIMPLE t1 ref_or_null a,b a 5 const 2 Using where; Using index
169
select * from t1 where (a = 7 or a is null) and (b=7 or b is null);
172
explain select * from t1 where (a = 7 or a is null) and (a = 7 or a is null);
173
id select_type table type possible_keys key key_len ref rows Extra
174
1 SIMPLE t1 ref_or_null a a 5 const 2 Using where; Using index
175
select * from t1 where (a = 7 or a is null) and (a = 7 or a is null);
178
create table t2 (a int);
179
insert into t2 values (7),(8);
180
explain select * from t2 straight_join t1 where t1.a=t2.a and b is null;
181
id select_type table type possible_keys key key_len ref rows Extra
182
1 SIMPLE t2 ALL NULL NULL NULL NULL X
183
1 SIMPLE t1 ref a,b b 5 const X Using where
185
explain select * from t2,t1 where t1.a=t2.a and b is null;
186
id select_type table type possible_keys key key_len ref rows Extra
187
1 SIMPLE t2 ALL NULL NULL NULL NULL X
188
1 SIMPLE t1 ref a a 10 test.t2.a,const X Using where; Using index
189
select * from t2,t1 where t1.a=t2.a and b is null;
193
explain select * from t2,t1 where t1.a=t2.a and (b= 7 or b is null);
194
id select_type table type possible_keys key key_len ref rows Extra
195
1 SIMPLE t2 ALL NULL NULL NULL NULL X
196
1 SIMPLE t1 ref_or_null a a 10 test.t2.a,const X Using where; Using index
197
select * from t2,t1 where t1.a=t2.a and (b= 7 or b is null);
202
explain select * from t2,t1 where (t1.a=t2.a or t1.a is null) and b= 7;
203
id select_type table type possible_keys key key_len ref rows Extra
204
1 SIMPLE t2 ALL NULL NULL NULL NULL X
205
1 SIMPLE t1 ref_or_null a a 10 test.t2.a,const X Using where; Using index
206
select * from t2,t1 where (t1.a=t2.a or t1.a is null) and b= 7;
209
explain select * from t2,t1 where (t1.a=t2.a or t1.a is null) and (b= 7 or b is null);
210
id select_type table type possible_keys key key_len ref rows Extra
211
1 SIMPLE t2 ALL NULL NULL NULL NULL X
212
1 SIMPLE t1 ref_or_null a a 5 test.t2.a X Using where; Using index
213
select * from t2,t1 where (t1.a=t2.a or t1.a is null) and (b= 7 or b is null);
218
insert into t2 values (null),(6);
219
delete from t1 where a=8;
220
explain select * from t2,t1 where t1.a=t2.a or t1.a is null;
221
id select_type table type possible_keys key key_len ref rows Extra
222
1 SIMPLE t1 system a NULL NULL NULL X
223
1 SIMPLE t2 ALL NULL NULL NULL NULL X Using where
224
explain select * from t2,t1 where t1.a<=>t2.a or (t1.a is null and t1.b <> 9);
225
id select_type table type possible_keys key key_len ref rows Extra
226
1 SIMPLE t1 system a NULL NULL NULL X
227
1 SIMPLE t2 ALL NULL NULL NULL NULL X Using where
228
select * from t2,t1 where t1.a<=>t2.a or (t1.a is null and t1.b <> 9);
232
CREATE TEMPORARY TABLE t1 (
233
id int NOT NULL auto_increment,
234
uniq_id int default NULL,
236
UNIQUE KEY idx1 (uniq_id)
238
CREATE TEMPORARY TABLE t2 (
239
id int NOT NULL auto_increment,
240
uniq_id int default NULL,
243
INSERT INTO t1 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL);
244
INSERT INTO t2 VALUES (1,NULL),(2,NULL),(3,1),(4,2),(5,NULL),(6,NULL),(7,3),(8,4),(9,NULL),(10,NULL);
245
explain select id from t1 where uniq_id is null;
246
id select_type table type possible_keys key key_len ref rows Extra
247
1 SIMPLE t1 ref idx1 idx1 5 const 5 Using where
248
explain select id from t1 where uniq_id =1;
249
id select_type table type possible_keys key key_len ref rows Extra
250
1 SIMPLE t1 const idx1 idx1 5 const 1
251
UPDATE t1 SET id=id+100 where uniq_id is null;
252
UPDATE t2 SET id=id+100 where uniq_id is null;
253
select id from t1 where uniq_id is null;
261
select id from t2 where uniq_id is null;
269
DELETE FROM t1 WHERE uniq_id IS NULL;
270
DELETE FROM t2 WHERE uniq_id IS NULL;
271
SELECT * FROM t1 ORDER BY uniq_id, id;
277
SELECT * FROM t2 ORDER BY uniq_id, id;
284
CREATE TEMPORARY TABLE `t1` (
285
`order_id` char(32) NOT NULL default '',
286
`product_id` char(32) NOT NULL default '',
287
`product_type` int NOT NULL default '0',
288
PRIMARY KEY (`order_id`,`product_id`,`product_type`)
290
CREATE TEMPORARY TABLE `t2` (
291
`order_id` char(32) NOT NULL default '',
292
`product_id` char(32) NOT NULL default '',
293
`product_type` int NOT NULL default '0',
294
PRIMARY KEY (`order_id`,`product_id`,`product_type`)
296
INSERT INTO t1 (order_id, product_id, product_type) VALUES
297
('3d7ce39b5d4b3e3d22aaafe9b633de51',1206029, 3),
298
('3d7ce39b5d4b3e3d22aaafe9b633de51',5880836, 3),
299
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
300
INSERT INTO t2 (order_id, product_id, product_type) VALUES
301
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
303
left join t2 using(order_id, product_id, product_type)
304
where t2.order_id=NULL;
305
order_id product_id product_type
307
left join t2 using(order_id, product_id, product_type)
308
where t2.order_id is NULL;
309
order_id product_id product_type
310
3d7ce39b5d4b3e3d22aaafe9b633de51 1206029 3
311
3d7ce39b5d4b3e3d22aaafe9b633de51 5880836 3
313
create table t1 (id int);
314
insert into t1 values (null), (0);
315
create table t2 (id int);
316
insert into t2 values (null);
317
select * from t1, t2 where t1.id = t2.id;
319
alter table t1 add key id (id);
320
select * from t1, t2 where t1.id = t2.id;
325
id2 integer not null,
329
insert into t1 values(null,null),(1,1);
330
ERROR 23000: Column 'id2' cannot be null
333
select * from t1 where id <=> null;
335
select * from t1 where id <=> null or id > 0;
337
select * from t1 where id is null or id > 0;
339
select * from t1 where id2 <=> null or id2 > 0;
341
select * from t1 where id2 is null or id2 > 0;
343
delete from t1 where id <=> NULL;
347
CREATE TABLE t1 (a int);
348
CREATE TABLE t2 (a int, b int, INDEX idx(a));
349
CREATE TABLE t3 (b int, INDEX idx(b));
350
CREATE TABLE t4 (b int, INDEX idx(b));
351
INSERT INTO t1 VALUES (1), (2), (3), (4);
352
INSERT INTO t2 VALUES (1, 1), (3, 1);
353
INSERT INTO t3 VALUES
354
(NULL), (NULL), (NULL), (NULL), (NULL),
355
(NULL), (NULL), (NULL), (NULL), (NULL);
356
INSERT INTO t4 SELECT * FROM t3;
357
INSERT INTO t3 SELECT * FROM t4;
358
INSERT INTO t4 SELECT * FROM t3;
359
INSERT INTO t3 SELECT * FROM t4;
360
INSERT INTO t4 SELECT * FROM t3;
361
INSERT INTO t3 SELECT * FROM t4;
362
INSERT INTO t4 SELECT * FROM t3;
363
INSERT INTO t3 SELECT * FROM t4;
364
INSERT INTO t4 SELECT * FROM t3;
365
INSERT INTO t3 SELECT * FROM t4;
366
INSERT INTO t4 SELECT * FROM t3;
367
INSERT INTO t3 SELECT * FROM t4;
368
INSERT INTO t4 SELECT * FROM t3;
369
INSERT INTO t3 SELECT * FROM t4;
370
INSERT INTO t4 SELECT * FROM t3;
371
INSERT INTO t3 SELECT * FROM t4;
372
INSERT INTO t3 VALUES (2), (3);
373
ANALYZE table t1, t2, t3;
374
Table Op Msg_type Msg_text
375
test.t1 analyze status OK
376
test.t2 analyze status OK
377
test.t3 analyze status OK
378
SELECT COUNT(*) FROM t3;
381
EXPLAIN SELECT SQL_CALC_FOUND_ROWS * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
382
LEFT JOIN t3 ON t2.b=t3.b;
383
id select_type table type possible_keys key key_len ref rows Extra
384
1 SIMPLE t1 ALL NULL NULL NULL NULL X
385
1 SIMPLE t2 ref idx idx 5 test.t1.a X
386
1 SIMPLE t3 ref idx idx 5 test.t2.b X Using index
388
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LEFT JOIN t2 ON t1.a=t2.a
389
LEFT JOIN t3 ON t2.b=t3.b;
398
SHOW STATUS LIKE "handler_read%";
405
Handler_read_rnd_next 5
406
DROP TABLE t1,t2,t3,t4;
412
INSERT INTO t1 VALUES (0,10),(0,11),(0,12);
418
INSERT INTO t2 VALUES (3,NULL),(3,11),(3,12);
419
SELECT * FROM t2 inner join t1 WHERE ( t1.a = 0 OR t1.a IS NULL) AND t2.a = 3 AND t2.b = t1.b;