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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
|
drop table if exists t0, t1, t2, t3, t4;
create table t1 (oref int, grp int, ie int) ;
insert into t1 (oref, grp, ie) values
(1, 1, 1),
(1, 1, 1),
(1, 2, NULL),
(2, 1, 3),
(3, 1, 4),
(3, 2, NULL);
create table t2 (oref int, a int);
insert into t2 values
(1, 1),
(2, 2),
(3, 3),
(4, NULL),
(2, NULL);
select a, oref, a in (select max(ie)
from t1 where oref=t2.oref group by grp) Z from t2;
a oref Z
1 1 1
2 2 0
3 3 NULL
NULL 4 0
NULL 2 NULL
explain extended
select a, oref, a in (select max(ie)
from t1 where oref=t2.oref group by grp) Z from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 5 100.00
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 6 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."a" AS "a","test"."t2"."oref" AS "oref",<in_optimizer>("test"."t2"."a",<exists>(select max("test"."t1"."ie") AS "max(ie)" from "test"."t1" where ("test"."t1"."oref" = "test"."t2"."oref") group by "test"."t1"."grp" having trigcond((<cache>("test"."t2"."a") = <ref_null_helper>(max("test"."t1"."ie")))))) AS "Z" from "test"."t2"
explain extended
select a, oref from t2
where a in (select max(ie) from t1 where oref=t2.oref group by grp);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 6 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."a" AS "a","test"."t2"."oref" AS "oref" from "test"."t2" where <in_optimizer>("test"."t2"."a",<exists>(select max("test"."t1"."ie") AS "max(ie)" from "test"."t1" where ("test"."t1"."oref" = "test"."t2"."oref") group by "test"."t1"."grp" having (<cache>("test"."t2"."a") = <ref_null_helper>(max("test"."t1"."ie")))))
select a, oref, a in (
select max(ie) from t1 where oref=t2.oref group by grp union
select max(ie) from t1 where oref=t2.oref group by grp
) Z from t2;
a oref Z
1 1 1
2 2 0
3 3 NULL
NULL 4 0
NULL 2 NULL
create table t3 (a int);
insert into t3 values (NULL), (NULL);
flush status;
select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
a in (select max(ie) from t1 where oref=4 group by grp)
0
0
show status like 'Handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 11
select ' ^ This must show 11' Z;
Z
^ This must show 11
explain extended select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 6 100.00 Using where; Using temporary; Using filesort
Warnings:
Note 1003 select <in_optimizer>("test"."t3"."a",<exists>(select max("test"."t1"."ie") AS "max(ie)" from "test"."t1" where ("test"."t1"."oref" = 4) group by "test"."t1"."grp" having trigcond((<cache>("test"."t3"."a") = <ref_null_helper>(max("test"."t1"."ie")))))) AS "a in (select max(ie) from t1 where oref=4 group by grp)" from "test"."t3"
drop table t1, t2, t3;
create table t1 (a int, oref int, key(a));
insert into t1 values
(1, 1),
(1, NULL),
(2, 3),
(2, NULL),
(3, NULL);
create table t2 (a int, oref int);
insert into t2 values (1, 1), (2,2), (NULL, 3), (NULL, 4);
select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
oref a Z
1 1 1
2 2 0
3 NULL NULL
4 NULL 0
explain extended
select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 4 100.00
2 DEPENDENT SUBQUERY t1 ALL a NULL NULL NULL 5 80.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."oref" AS "oref","test"."t2"."a" AS "a",<in_optimizer>("test"."t2"."a",<exists>(select 1 AS "Not_used" from "test"."t1" where (("test"."t1"."oref" = "test"."t2"."oref") and trigcond(((<cache>("test"."t2"."a") = "test"."t1"."a") or isnull("test"."t1"."a")))) having trigcond(<is_not_null_test>("test"."t1"."a")))) AS "Z" from "test"."t2"
flush status;
select oref, a from t2 where a in (select a from t1 where oref=t2.oref);
oref a
1 1
show status like '%Handler_read_rnd_next';
Variable_name Value
Handler_read_rnd_next 11
delete from t2;
insert into t2 values (NULL, 0),(NULL, 0), (NULL, 0), (NULL, 0);
flush status;
select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
oref a Z
0 NULL 0
0 NULL 0
0 NULL 0
0 NULL 0
show status like '%Handler_read%';
Variable_name Value
Handler_read_first 5
Handler_read_key 10
Handler_read_next 0
Handler_read_prev 0
Handler_read_rnd 0
Handler_read_rnd_next 29
select 'No key lookups, seq reads: 29= 5 reads from t2 + 4 * 6 reads from t1.' Z;
Z
No key lookups, seq reads: 29= 5 reads from t2 + 4 * 6 reads from t1.
drop table t1, t2;
create table t1 (a int, b int, primary key (a));
insert into t1 values (1,1), (3,1),(100,1);
create table t2 (a int, b int);
insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
a b Z
1 1 1
2 1 0
NULL 1 NULL
NULL 0 0
drop table t1, t2;
create table t1 (a int, b int, key(a));
insert into t1 values
(0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
create table t2 like t1;
insert into t2 select * from t1;
update t2 set b=1;
create table t3 (a int, oref int);
insert into t3 values (1, 1), (NULL,1), (NULL,0);
select a, oref,
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
from t3;
a oref Z
1 1 1
NULL 1 NULL
NULL 0 0
explain extended
select a, oref,
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
from t3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t1 ref_or_null a a 5 func 2 100.00 Using where; Full scan on NULL key
2 DEPENDENT SUBQUERY t2 ref a a 5 test.t1.b 1 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t3.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t3"."a" AS "a","test"."t3"."oref" AS "oref",<in_optimizer>("test"."t3"."a",<exists>(select 1 AS "Not_used" from "test"."t1" join "test"."t2" where (("test"."t2"."a" = "test"."t1"."b") and ("test"."t2"."b" = "test"."t3"."oref") and trigcond(((<cache>("test"."t3"."a") = "test"."t1"."a") or isnull("test"."t1"."a")))) having trigcond(<is_not_null_test>("test"."t1"."a")))) AS "Z" from "test"."t3"
drop table t1, t2, t3;
create table t1 (a int NOT NULL, b int NOT NULL, key(a));
insert into t1 values
(0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
create table t2 like t1;
insert into t2 select * from t1;
update t2 set b=1;
create table t3 (a int, oref int);
insert into t3 values (1, 1), (NULL,1), (NULL,0);
select a, oref,
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
from t3;
a oref Z
1 1 1
NULL 1 NULL
NULL 0 0
This must show a trig_cond:
explain extended
select a, oref,
t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
from t3;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
2 DEPENDENT SUBQUERY t1 ref a a 4 func 1 100.00 Using where; Full scan on NULL key
2 DEPENDENT SUBQUERY t2 ref a a 4 test.t1.b 1 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t3.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t3"."a" AS "a","test"."t3"."oref" AS "oref",<in_optimizer>("test"."t3"."a",<exists>(select 1 AS "Not_used" from "test"."t1" join "test"."t2" where (("test"."t2"."a" = "test"."t1"."b") and ("test"."t2"."b" = "test"."t3"."oref") and trigcond((<cache>("test"."t3"."a") = "test"."t1"."a"))))) AS "Z" from "test"."t3"
drop table t1,t2,t3;
create table t1 (oref int, grp int);
insert into t1 (oref, grp) values
(1, 1),
(1, 1);
create table t2 (oref int, a int);
insert into t2 values
(1, NULL),
(2, NULL);
select a, oref,
a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;
a oref Z
NULL 1 NULL
NULL 2 0
This must show a trig_cond:
explain extended
select a, oref,
a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
Warnings:
Note 1276 Field or reference 't2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."a" AS "a","test"."t2"."oref" AS "oref",<in_optimizer>("test"."t2"."a",<exists>(select count(0) AS "count(*)" from "test"."t1" group by "test"."t1"."grp" having (("test"."t1"."grp" = "test"."t2"."oref") and trigcond((<cache>("test"."t2"."a") = <ref_null_helper>(count(0))))))) AS "Z" from "test"."t2"
drop table t1, t2;
create table t1 (a int, b int, primary key (a));
insert into t1 values (1,1), (3,1),(100,1);
create table t2 (a int, b int);
insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
select a,b, a in (select a from t1 where t1.b = t2.b union select a from
t1 where t1.b = t2.b) Z from t2 ;
a b Z
1 1 1
2 1 0
NULL 1 NULL
NULL 0 0
select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
a b Z
1 1 1
2 1 0
NULL 1 NULL
NULL 0 0
drop table t1, t2;
create table t3 (a int);
insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t2 (a int, b int, oref int);
insert into t2 values (NULL,1, 100), (NULL,2, 100);
create table t1 (a int, b int, c int, key(a,b));
insert into t1 select 2*A, 2*A, 100 from t3;
explain extended select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 2 100.00 Using where; Full scan on NULL key
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."a" AS "a","test"."t2"."b" AS "b","test"."t2"."oref" AS "oref",<in_optimizer>(("test"."t2"."a","test"."t2"."b"),<exists>(<index_lookup>(<cache>("test"."t2"."a") in t1 on a checking NULL where (("test"."t1"."c" = "test"."t2"."oref") and trigcond(((<cache>("test"."t2"."a") = "test"."t1"."a") or isnull("test"."t1"."a"))) and trigcond(((<cache>("test"."t2"."b") = "test"."t1"."b") or isnull("test"."t1"."b")))) having (trigcond(<is_not_null_test>("test"."t1"."a")) and trigcond(<is_not_null_test>("test"."t1"."b")))))) AS "Z" from "test"."t2"
select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
a b oref Z
NULL 1 100 0
NULL 2 100 NULL
create table t4 (x int);
insert into t4 select A.a + 10*B.a from t1 A, t1 B;
explain extended
select a,b, oref,
(a,b) in (select a,b from t1,t4 where c=t2.oref) Z
from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
2 DEPENDENT SUBQUERY t1 ALL a NULL NULL NULL 10 80.00 Using where
2 DEPENDENT SUBQUERY t4 ALL NULL NULL NULL NULL 100 100.00 Using where; Using join buffer
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."a" AS "a","test"."t2"."b" AS "b","test"."t2"."oref" AS "oref",<in_optimizer>(("test"."t2"."a","test"."t2"."b"),<exists>(select "test"."t1"."a" AS "a","test"."t1"."b" AS "b" from "test"."t1" join "test"."t4" where (("test"."t1"."c" = "test"."t2"."oref") and trigcond(((<cache>("test"."t2"."a") = "test"."t1"."a") or isnull("test"."t1"."a"))) and trigcond(((<cache>("test"."t2"."b") = "test"."t1"."b") or isnull("test"."t1"."b")))) having (trigcond(<is_not_null_test>("test"."t1"."a")) and trigcond(<is_not_null_test>("test"."t1"."b"))))) AS "Z" from "test"."t2"
select a,b, oref,
(a,b) in (select a,b from t1,t4 where c=t2.oref) Z
from t2;
a b oref Z
NULL 1 100 0
NULL 2 100 NULL
drop table t1,t2,t3,t4;
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values
('aa', 10, 2, 1),
('aa', 10, 1, 1),
('aa', 20, 2, 1),
('bb', 10, 3, 1),
('cc', 10, 4, 2),
('cc', 20, 3, 2),
('ee', 10, 2, 1),
('ee', 10, 1, 2),
('ff', 20, 2, 2),
('ff', 20, 1, 2);
create table t2 (oref char(4), a int, b int);
insert into t2 values
('ee', NULL, 1),
('bb', 2, 1),
('ff', 2, 2),
('cc', 3, NULL),
('bb', NULL, NULL),
('aa', 1, 1),
('dd', 1, NULL);
alter table t1 add index idx(ie1,ie2);
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=3 and b is null ;
oref a b Z
cc 3 NULL NULL
insert into t2 values ('new1', 10,10);
insert into t1 values ('new1', 1234, 10, NULL);
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10;
oref a b Z
new1 10 10 NULL
explain extended
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 8 100.00 Using where
2 DEPENDENT SUBQUERY t1 ALL idx NULL NULL NULL 11 81.82 Using where
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."oref" AS "oref","test"."t2"."a" AS "a","test"."t2"."b" AS "b",<in_optimizer>(("test"."t2"."a","test"."t2"."b"),<exists>(select "test"."t1"."ie1" AS "ie1","test"."t1"."ie2" AS "ie2" from "test"."t1" where (("test"."t1"."oref" = "test"."t2"."oref") and trigcond(((<cache>("test"."t2"."a") = "test"."t1"."ie1") or isnull("test"."t1"."ie1"))) and trigcond(((<cache>("test"."t2"."b") = "test"."t1"."ie2") or isnull("test"."t1"."ie2")))) having (trigcond(<is_not_null_test>("test"."t1"."ie1")) and trigcond(<is_not_null_test>("test"."t1"."ie2"))))) AS "Z" from "test"."t2" where (("test"."t2"."b" = 10) and ("test"."t2"."a" = 10))
drop table t1, t2;
create table t1 (oref char(4), grp int, ie int);
insert into t1 (oref, grp, ie) values
('aa', 10, 2),
('aa', 10, 1),
('aa', 20, NULL),
('bb', 10, 3),
('cc', 10, 4),
('cc', 20, NULL),
('ee', 10, NULL),
('ee', 10, NULL),
('ff', 20, 2),
('ff', 20, 1);
create table t2 (oref char(4), a int);
insert into t2 values
('ee', NULL),
('bb', 2),
('ff', 2),
('cc', 3),
('aa', 1),
('dd', NULL),
('bb', NULL);
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
oref a Z
ee NULL NULL
bb 2 0
ff 2 1
cc 3 NULL
aa 1 1
dd NULL 0
bb NULL NULL
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
oref a
aa 1
ff 2
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
oref a
bb 2
dd NULL
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
oref a Z
ee NULL NULL
bb 2 0
ff 2 0
cc 3 NULL
aa 1 1
dd NULL 0
bb NULL NULL
select oref, a from t2 where
a in (select min(ie) from t1 where oref=t2.oref group by grp);
oref a
aa 1
select oref, a from t2 where
a not in (select min(ie) from t1 where oref=t2.oref group by grp);
oref a
bb 2
ff 2
dd NULL
update t1 set ie=3 where oref='ff' and ie=1;
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
grp) Z from t2;
oref a Z
ee NULL NULL
bb 2 0
ff 2 1
cc 3 NULL
aa 1 1
dd NULL 0
bb NULL NULL
select oref, a from t2 where a in (select min(ie) from t1 where
oref=t2.oref group by grp);
oref a
ff 2
aa 1
select oref, a from t2 where a not in (select min(ie) from t1 where
oref=t2.oref group by grp);
oref a
bb 2
dd NULL
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by
grp having min(ie) > 1) Z from t2;
oref a Z
ee NULL 0
bb 2 0
ff 2 1
cc 3 0
aa 1 0
dd NULL 0
bb NULL NULL
select oref, a from t2 where a in (select min(ie) from t1 where
oref=t2.oref group by grp having min(ie) > 1);
oref a
ff 2
select oref, a from t2 where a not in (select min(ie) from t1 where
oref=t2.oref group by grp having min(ie) > 1);
oref a
ee NULL
bb 2
cc 3
aa 1
dd NULL
alter table t1 add index idx(ie);
explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7
2 DEPENDENT SUBQUERY t1 ALL idx NULL NULL NULL 10 Using where
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
oref a Z
ee NULL NULL
bb 2 0
ff 2 1
cc 3 NULL
aa 1 1
dd NULL 0
bb NULL NULL
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
oref a
aa 1
ff 2
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
oref a
bb 2
dd NULL
alter table t1 drop index idx;
alter table t1 add index idx(oref,ie);
explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7
2 DEPENDENT SUBQUERY t1 ref_or_null idx idx 12 test.t2.oref,func 10 Using where; Using index; Full scan on NULL key
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
oref a Z
ee NULL NULL
bb 2 0
ff 2 1
cc 3 NULL
aa 1 1
dd NULL 0
bb NULL NULL
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
oref a
ff 2
aa 1
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
oref a
bb 2
dd NULL
explain
select oref, a,
a in (select min(ie) from t1 where oref=t2.oref
group by grp having min(ie) > 1) Z
from t2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7
2 DEPENDENT SUBQUERY t1 ref idx idx 7 test.t2.oref 5 Using where; Using temporary; Using filesort
select oref, a,
a in (select min(ie) from t1 where oref=t2.oref
group by grp having min(ie) > 1) Z
from t2;
oref a Z
ee NULL 0
bb 2 0
ff 2 1
cc 3 0
aa 1 0
dd NULL 0
bb NULL NULL
select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref
group by grp having min(ie) > 1);
oref a
ff 2
select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref
group by grp having min(ie) > 1);
oref a
ee NULL
bb 2
cc 3
aa 1
dd NULL
drop table t1,t2;
create table t1 (oref char(4), grp int, ie1 int, ie2 int);
insert into t1 (oref, grp, ie1, ie2) values
('aa', 10, 2, 1),
('aa', 10, 1, 1),
('aa', 20, 2, 1),
('bb', 10, 3, 1),
('cc', 10, 4, 2),
('cc', 20, 3, 2),
('ee', 10, 2, 1),
('ee', 10, 1, 2),
('ff', 20, 2, 2),
('ff', 20, 1, 2);
create table t2 (oref char(4), a int, b int);
insert into t2 values
('ee', NULL, 1),
('bb', 2, 1),
('ff', 2, 2),
('cc', 3, NULL),
('bb', NULL, NULL),
('aa', 1, 1),
('dd', 1, NULL);
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
oref a b Z
ee NULL 1 NULL
bb 2 1 0
ff 2 2 1
cc 3 NULL NULL
bb NULL NULL NULL
aa 1 1 1
dd 1 NULL 0
select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
oref a b
aa 1 1
ff 2 2
select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
oref a b
bb 2 1
dd 1 NULL
select oref, a, b,
(a,b) in (select min(ie1),max(ie2) from t1
where oref=t2.oref group by grp) Z
from t2;
oref a b Z
ee NULL 1 0
bb 2 1 0
ff 2 2 0
cc 3 NULL NULL
bb NULL NULL NULL
aa 1 1 1
dd 1 NULL 0
select oref, a, b from t2 where
(a,b) in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
oref a b
aa 1 1
select oref, a, b from t2 where
(a,b) not in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp);
oref a b
ee NULL 1
bb 2 1
ff 2 2
dd 1 NULL
alter table t1 add index idx(ie1,ie2);
explain select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7
2 DEPENDENT SUBQUERY t1 ALL idx NULL NULL NULL 10 Using where
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
oref a b Z
ee NULL 1 NULL
bb 2 1 0
ff 2 2 1
cc 3 NULL NULL
bb NULL NULL NULL
aa 1 1 1
dd 1 NULL 0
select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
oref a b
aa 1 1
ff 2 2
select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
oref a b
bb 2 1
dd 1 NULL
explain extended
select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 100.00
2 DEPENDENT SUBQUERY t1 ALL idx NULL NULL NULL 10 80.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.oref' of SELECT #2 was resolved in SELECT #1
Note 1003 select "test"."t2"."oref" AS "oref","test"."t2"."a" AS "a","test"."t2"."b" AS "b",<in_optimizer>(("test"."t2"."a","test"."t2"."b"),<exists>(select "test"."t1"."ie1" AS "ie1","test"."t1"."ie2" AS "ie2" from "test"."t1" where (("test"."t1"."oref" = "test"."t2"."oref") and trigcond(((<cache>("test"."t2"."a") = "test"."t1"."ie1") or isnull("test"."t1"."ie1"))) and trigcond(((<cache>("test"."t2"."b") = "test"."t1"."ie2") or isnull("test"."t1"."ie2")))) having (trigcond(<is_not_null_test>("test"."t1"."ie1")) and trigcond(<is_not_null_test>("test"."t1"."ie2"))))) AS "Z" from "test"."t2"
drop table t1,t2;
create table t1 (oref char(4), grp int, ie int primary key);
insert into t1 (oref, grp, ie) values
('aa', 10, 2),
('aa', 10, 1),
('bb', 10, 3),
('cc', 10, 4),
('cc', 20, 5),
('cc', 10, 6);
create table t2 (oref char(4), a int);
insert into t2 values
('ee', NULL),
('bb', 2),
('cc', 5),
('cc', 2),
('cc', NULL),
('aa', 1),
('bb', NULL);
explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using where; Full scan on NULL key
select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
oref a Z
ee NULL 0
bb 2 0
cc 5 1
cc 2 0
cc NULL NULL
aa 1 1
bb NULL NULL
select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
oref a
aa 1
cc 5
select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
oref a
ee NULL
bb 2
cc 2
explain
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 7
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 6 Using where; Using temporary; Using filesort
select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2;
oref a Z
ee NULL 0
bb 2 0
cc 5 1
cc 2 0
cc NULL NULL
aa 1 1
bb NULL NULL
drop table t1,t2;
create table t1 (a int, b int);
insert into t1 values (0,0), (2,2), (3,3);
create table t2 (a int, b int);
insert into t2 values (1,1), (3,3);
select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
a b Z
0 0 0
2 2 0
3 3 1
insert into t2 values (NULL,4);
select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1;
a b Z
0 0 0
2 2 0
3 3 1
drop table t1,t2;
CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
INSERT INTO t1 VALUES (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'),(3,3,'j'), (3,2,'k'), (3,1,'l'),
(1,9,'m');
CREATE TABLE t2 (a int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
INSERT INTO t2 SELECT * FROM t1;
SELECT a, MAX(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b))
as test FROM t1 GROUP BY a;
a MAX(b) test
1 9 m
2 3 h
3 4 i
SELECT * FROM t1 GROUP by t1.a
HAVING (MAX(t1.b) > (SELECT MAX(t2.b) FROM t2 WHERE t2.c < t1.c
HAVING MAX(t2.b+t1.a) < 10));
a b c
SELECT a,b,c FROM t1 WHERE b in (9,3,4) ORDER BY b,c;
a b c
1 3 c
2 3 h
3 3 j
1 4 d
3 4 i
1 9 m
SELECT a, MAX(b),
(SELECT COUNT(DISTINCT t.c) FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
LIMIT 1)
as cnt,
(SELECT t.b FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
as t_b,
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
as t_b,
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) ORDER BY t.c LIMIT 1)
as t_b
FROM t1 GROUP BY a;
a MAX(b) cnt t_b t_b t_b
1 9 1 9 m m
2 3 1 3 h h
3 4 1 4 i i
SELECT a, MAX(b),
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1) as test
FROM t1 GROUP BY a;
a MAX(b) test
1 9 m
2 3 h
3 4 i
DROP TABLE t1, t2;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (b int, PRIMARY KEY(b));
INSERT INTO t1 VALUES (1), (NULL), (4);
INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6);
EXPLAIN EXTENDED
SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00 Using index
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1003 select "test"."t1"."a" AS "a" from "test"."t1" join "test"."t2" where (("test"."t2"."b" = "test"."t1"."a") and (not(<in_optimizer>("test"."t1"."a",<exists>(select 1 AS "Not_used" from "test"."t1" where ((<cache>("test"."t2"."b") = "test"."t1"."a") or isnull("test"."t1"."a")) having <is_not_null_test>("test"."t1"."a"))))))
SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
a
SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4));
a
1
4
DROP TABLE t1,t2;
CREATE TABLE t1 (id int);
CREATE TABLE t2 (id int PRIMARY KEY);
CREATE TABLE t3 (id int PRIMARY KEY, name varchar(10));
INSERT INTO t1 VALUES (2), (NULL), (3), (1);
INSERT INTO t2 VALUES (234), (345), (457);
INSERT INTO t3 VALUES (222,'bbb'), (333,'ccc'), (111,'aaa');
EXPLAIN
SELECT * FROM t1
WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
WHERE t3.name='xxx' AND t2.id=t3.id);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
2 DEPENDENT SUBQUERY t2 eq_ref PRIMARY PRIMARY 4 func 1 Using where; Using index; Full scan on NULL key
2 DEPENDENT SUBQUERY t3 eq_ref PRIMARY PRIMARY 4 func 1 Using where; Full scan on NULL key
SELECT * FROM t1
WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
WHERE t3.name='xxx' AND t2.id=t3.id);
id
2
NULL
3
1
SELECT (t1.id IN (SELECT t2.id FROM t2,t3
WHERE t3.name='xxx' AND t2.id=t3.id)) AS x
FROM t1;
x
0
0
0
0
DROP TABLE t1,t2,t3;
CREATE TABLE t1 (a INT NOT NULL);
INSERT INTO t1 VALUES (1),(-1), (65),(66);
CREATE TABLE t2 (a INT UNSIGNED NOT NULL PRIMARY KEY);
INSERT INTO t2 VALUES (65),(66);
SELECT a FROM t1 WHERE a NOT IN (65,66);
a
1
-1
SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
a
1
-1
EXPLAIN SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
2 DEPENDENT SUBQUERY t2 unique_subquery PRIMARY PRIMARY 4 func 1 Using index; Using where
DROP TABLE t1, t2;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES(1);
CREATE TABLE t2 (placeholder CHAR(11));
INSERT INTO t2 VALUES("placeholder");
SELECT ROW(1, 2) IN (SELECT t1.a, 2) FROM t1 GROUP BY t1.a;
ROW(1, 2) IN (SELECT t1.a, 2)
1
SELECT ROW(1, 2) IN (SELECT t1.a, 2 FROM t2) FROM t1 GROUP BY t1.a;
ROW(1, 2) IN (SELECT t1.a, 2 FROM t2)
1
DROP TABLE t1, t2;
create table t1 (a int, b decimal(13, 3));
insert into t1 values (1, 0.123);
select a, (select max(b) from t1) into outfile "subselect.out.file.1" from t1;
delete from t1;
load data infile "subselect.out.file.1" into table t1;
select * from t1;
a b
1 0.123
drop table t1;
End of 5.0 tests
|