summaryrefslogtreecommitdiffstats
path: root/mysql-test/main/opt_tvc.test
blob: e08f0a3f66412df3a74a36eef5dd66179f3e342e (plain)
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
#
# MDEV-12176 Transform [NOT] IN predicate with long list of values INTO [NOT] IN subquery
#
source include/have_debug.inc;
source include/default_optimizer_switch.inc;
source include/have_sequence.inc;

create table t1 (a int, b int);

insert into t1
values (1,2), (4,6), (9,7),
       (1,1), (2,5), (7,8);

create table t2 (a int, b int, c int);

insert into t2
values (1,2,3), (5,1,2), (4,3,7),
       (8,9,0), (10,7,1), (5,5,1);

create table t3 (a int, b varchar(16), index idx(a));
insert into t3 values
  (1, "abc"), (3, "egh"), (8, "axxx"), (10, "abc"),
  (2, "ccw"), (8, "wqqe"), (7, "au"), (9, "waa"),
  (3, "rass"), (9, "ert"), (9, "lok"), (8, "aww"),
  (1, "todd"), (3, "rew"), (8, "aww"), (3, "sw"),
  (11, "llk"), (7, "rbw"), (1, "sm"), (2, "jyp"),
  (4, "yq"), (5, "pled"), (12, "ligin"), (12, "toww"),
  (6, "mxm"), (15, "wanone"), (9, "sunqq"), (2, "abe");

--echo # optimization is not used

let $query= select * from t1 where a in (1,2);
eval $query;
eval explain extended $query;

--echo # set minimum number of values in VALUEs list when optimization works to 2

set @@in_predicate_conversion_threshold= 2;

--echo # single IN-predicate in WHERE-part

let $query= select * from t1 where a in (1,2);

let $optimized_query=
select * from t1
where a in
      (
        select *
        from (values (1),(2)) as tvc_0
      );

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

--echo # AND-condition with IN-predicates in WHERE-part

let $query=
select * from t1
where a in (1,2) and
      b in (1,5);

let $optimized_query=
select * from t1
where a in
(
  select *
  from (values (1),(2)) as tvc_0
)
and b in
(
  select *
  from (values (1),(5)) as tvc_1
);

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

--echo # subquery with IN-predicate

let $query=
select * from t1
where a in
(
  select a
  from t2 where b in (3,4)
);

let $optimized_query=
select * from t1
where a in
(
  select a from t2
  where b in
  (
    select *
    from (values (3),(4)) as tvc_0
  )
);

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

--echo # derived table with IN-predicate

let $query=
select * from
(
  select *
  from t1
  where a in (1,2)
) as dr_table;

let $optimized_query=
select * from
(
  select *
  from t1
  where a in
  (
    select *
    from (values (1),(2))
    as tvc_0
  )
) as dr_table;

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

--echo # non-recursive CTE with IN-predicate

let $cte_query=
with tvc_0 as
(
  select *
  from t1
  where a in (1,2)
)
select * from tvc_0;

eval $cte_query;
eval $optimized_query;
eval explain extended $cte_query;
eval explain extended $optimized_query;

--echo # VIEW with IN-predicate

create view v1 as
  select *
  from t1
  where a in (1,2);

create view v2 as
  select *
  from t1
  where a in
  (
    select *
    from (values (1),(2))
    as tvc_0
  )
;

let $query= select * from v1;
let $optimized_query= select * from v2;

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

drop view v1,v2;

--echo # subselect defined by derived table with IN-predicate

let $query=
select * from t1
where a in
(
  select 1
  from
  (
    select *
    from t1
    where a in (1,2)
  )
  as dr_table
);

let $optimized_query=
select * from t1
where a in
(
  select 1
  from
  (
    select *
    from t1
    where a in
    (
      select *
      from (values (1),(2))
      as tvc_0
    )
  )
  as dr_table
);

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

--echo # derived table with IN-predicate and group by

let $query=
select * from
(
  select max(a),b
  from t1
  where b in (3,5)
  group by b
) as dr_table;

let $optimized_query=
select * from
(
  select max(a),b
  from t1
  where b in
  (
    select *
    from (values (3),(5))
    as tvc_0
  )
  group by b
) as dr_table;

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

--echo # prepare statement

prepare stmt from "select * from t1 where a in (1,2)";
execute stmt;
execute stmt;
deallocate prepare stmt;

--echo # use inside out access from tvc rows

let $query= select * from t3 where a in (1,4);
set @@in_predicate_conversion_threshold= default;
eval $query;
eval explain extended $query;
set @@in_predicate_conversion_threshold= 2;
eval $query;
eval explain extended $query;

--echo # use vectors in IN predeicate

set @@in_predicate_conversion_threshold= 4;

let $query=
select * from t1 where (a,b) in ((1,2),(3,4));

eval $query;
eval explain extended $query;
set @@in_predicate_conversion_threshold= 2;

--echo # trasformation works for the one IN predicate and doesn't work for the other

set @@in_predicate_conversion_threshold= 5;

let $query=
select * from t2
where (a,b) in ((1,2),(8,9)) and
      (a,c) in ((1,3),(8,0),(5,1));

eval $query;
eval explain extended $query;
set @@in_predicate_conversion_threshold= 2;

--echo #
--echo # mdev-14281: conversion of NOT IN predicate into subquery predicate
--echo # 

let $query=
select * from t1
where (a,b) not in ((1,2),(8,9), (5,1));
let $optimized_query=
select * from t1
where (a,b) not in (select * from (values (1,2),(8,9), (5,1)) as tvc_0);

eval $query;
eval $optimized_query;
eval explain extended $query;
eval explain extended $optimized_query;

let $query=
select * from t1
where b < 7 and (a,b) not in ((1,2),(8,9), (5,1));
eval $query;
eval explain extended $query;

let $query=
select * from t2
where (a,c) not in ((1,2),(8,9), (5,1));
eval $query;
eval explain extended $query;

drop table t1, t2, t3;

set @@in_predicate_conversion_threshold= default;

--echo #
--echo # MDEV-14947: conversion to TVC with only NULL values
--echo #

CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (3), (2), (7);

let $q=
SELECT * FROM t1 WHERE i IN (NULL, NULL, NULL, NULL, NULL);

eval $q;
eval EXPLAIN EXTENDED $q;

SET in_predicate_conversion_threshold= 5;

eval $q;
eval EXPLAIN EXTENDED $q;

SET in_predicate_conversion_threshold= default;

DROP TABLE t1;

--echo #
--echo # MDEV-14835: conversion to TVC with BIGINT or YEAR values
--echo #

#Enable after fix MDEV-31178
--disable_ps2_protocol
SET @@in_predicate_conversion_threshold= 2;

CREATE TABLE t1 (a BIGINT);
CREATE TABLE t2 (y YEAR);

INSERT INTO t1 VALUES (1), (2), (3);
INSERT INTO t2 VALUES (2009), (2010), (2011);

SELECT * FROM t1 WHERE a IN ('1','5','3');

SELECT * FROM t2 WHERE y IN ('2009','2011');

DROP TABLE t1,t2;

SET @@in_predicate_conversion_threshold= default;
--enable_ps2_protocol

--echo #
--echo # MDEV-17222: conversion to TVC with no names for constants
--echo #             conversion to TVC with the same constants in the first row
--echo #

SET @@in_predicate_conversion_threshold= 2;

CREATE TABLE t1 (f BINARY(16)) ENGINE=MYISAM;
INSERT INTO t1 VALUES
(x'BAE56AF2B1C2397D99D58E2A06761DDB'), (x'9B9B698BCCB939EE8F1EA56C1A2E5DAA'),
(x'A0A1C4FE39A239BABD3E0D8985E6BEA5');

SELECT COUNT(*) FROM t1 WHERE f IN
(x'9B9B698BCCB939EE8F1EA56C1A2E5DAA', x'E2362DBAB5EA30B5804917A0A7D881E2',
 x'B78B2EEAD13635088D93EA3309E24802', x'BAE56AF2B1C2397D99D58E2A06761DDB');

CREATE TABLE t2 (f1 BINARY(16), f2 BINARY(16)) ENGINE=MYISAM;
INSERT INTO t2 VALUES
(x'55FB3B14D6B83D39859E42533906350D', x'00F3458C47FA39DDBEAD918A13F8342E'),
(x'86052C062AAF368D84247ED0F6346A70', x'BF5C35045C6037C79E11026ABB9A3A4E');

SELECT COUNT(*) FROM t2 WHERE (f1,f2) IN
((x'9B9B698BCCB939EE8F1EA56C1A2E5DAA', x'E2362DBAB5EA30B5804917A0A7D881E2'),
 (x'B78B2EEAD13635088D93EA3309E24802', x'BAE56AF2B1C2397D99D58E2A06761DDB'),
 (x'55FB3B14D6B83D39859E42533906350D', x'00F3458C47FA39DDBEAD918A13F8342E'),
 (x'1606014E7C4A312F83EDC9D91BBFCACA', x'33F6068E56FD3A1D8326517F0D81CB5A'));

CREATE TABLE t3 (f1 int, f2 int) ENGINE=MYISAM;
INSERT INTO t3 VALUES (2,5), (2,3), (1,2), (7,8), (1,1);

SELECT * FROM t3 WHERE (f1,f2) IN ((2, 2), (1, 2), (3, 5), (1, 1));

DROP TABLE t1,t2,t3;

SET @@in_predicate_conversion_threshold= default;

--echo #
--echo # MDEV-20900: IN predicate to IN subquery conversion causes performance regression
--echo #

create table t1(a int, b int);
insert into t1 select seq-1, seq-1 from seq_1_to_10;

set in_predicate_conversion_threshold=2;

let $query= select * from t1 where t1.a IN ("1","2","3","4");
eval explain $query;
eval $query;

set in_predicate_conversion_threshold=0;
eval explain $query;
eval $query;

set in_predicate_conversion_threshold=2;
let $query= select * from t1 where (t1.a,t1.b) in (("1","1"),(2,2),(3,3),(4,4));
eval explain $query;
eval $query;

set in_predicate_conversion_threshold=0;
eval explain $query;
eval $query;

drop table t1;
SET @@in_predicate_conversion_threshold= default;

--echo #
--echo # MDEV-27937: Prepared statement with ? in the list if IN predicate
--echo #

set in_predicate_conversion_threshold=2;

create table t1 (id int, a int, b int);
insert into t1 values (1,3,30), (2,7,70), (3,1,10);

prepare stmt from "
select * from t1 where a in (7, ?, 5, 1);
";
execute stmt using 3;
deallocate prepare stmt;

prepare stmt from "
select * from t1 where (a,b) in ((7,70), (3,?), (5,50), (1,10));
";
execute stmt using 30;
deallocate prepare stmt;

drop table t1;

set in_predicate_conversion_threshold=default;

--echo # End of 10.3 tests