summaryrefslogtreecommitdiff
path: root/mysql-test/main/rowid_filter.test
blob: 70770237ee1fffdafeadf5efbe009f93a4f9c74b (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
460
461
462
--disable_warnings
DROP DATABASE IF EXISTS dbt3_s001;
--enable_warnings

CREATE DATABASE dbt3_s001;

use dbt3_s001;

--disable_query_log
--disable_result_log
--disable_warnings
--source include/dbt3_s001.inc
--enable_warnings
--enable_result_log
--enable_query_log

CREATE INDEX i_l_quantity ON lineitem(l_quantity);

CREATE INDEX i_o_totalprice ON orders(o_totalprice);

set @save_use_stat_tables= @@use_stat_tables;

set @@use_stat_tables=preferably;

--disable_result_log
--disable_warnings
ANALYZE TABLE lineitem, orders;
--enable_warnings
--enable_result_log

show create table lineitem;
show create table orders;

set optimizer_use_condition_selectivity=2;

let $with_filter=
set statement optimizer_switch='rowid_filter=on' for;

let $without_filter=
set statement optimizer_switch='rowid_filter=off' for;

select
  100 *
  (select count(*) from lineitem
   WHERE l_shipdate BETWEEN '1997-01-01' AND '1997-06-30' AND l_quantity > 45
  )
  /
  (select count(*) from lineitem
   where l_shipdate BETWEEN '1997-01-01' AND '1997-06-30')
  as correct_r_filtered_when_using_l_shipdate;

let $q1=
SELECT l_orderkey, l_linenumber, l_shipdate, l_quantity FROM lineitem
  WHERE  l_shipdate BETWEEN '1997-01-01' AND '1997-06-30' AND
         l_quantity > 45;

eval $with_filter EXPLAIN $q1;
eval $with_filter EXPLAIN FORMAT=JSON $q1;
eval $with_filter ANALYZE $q1;
--source include/analyze-format.inc
eval $with_filter ANALYZE FORMAT=JSON $q1;
--sorted_result
eval $with_filter $q1;

eval $without_filter EXPLAIN $q1;
eval $without_filter EXPLAIN FORMAT=JSON $q1;
eval $without_filter ANALYZE $q1;
--source include/analyze-format.inc
eval $without_filter ANALYZE FORMAT=JSON $q1;
--sorted_result
eval $without_filter $q1;

let $q2=
SELECT o_orderkey, l_linenumber, l_shipdate, o_totalprice
  FROM orders JOIN lineitem ON o_orderkey=l_orderkey
  WHERE  l_shipdate BETWEEN '1997-01-01' AND '1997-01-31' AND
         o_totalprice between 200000 and 230000;

eval $with_filter EXPLAIN $q2;
eval $with_filter EXPLAIN FORMAT=JSON $q2;
eval $with_filter ANALYZE $q2;
--source include/analyze-format.inc
eval $with_filter ANALYZE FORMAT=JSON $q2;
--sorted_result
eval $with_filter $q2;

eval $without_filter EXPLAIN $q2;
eval $without_filter EXPLAIN FORMAT=JSON $q2;
eval $without_filter ANALYZE $q2;
--source include/analyze-format.inc
eval $without_filter ANALYZE FORMAT=JSON $q2;
--sorted_result
eval $without_filter $q2;

let $q3=
SELECT o_orderkey, l_linenumber, l_shipdate, l_quantity, o_totalprice
  FROM orders JOIN lineitem ON o_orderkey=l_orderkey
  WHERE  l_shipdate BETWEEN '1997-01-01' AND '1997-06-30' AND
         l_quantity > 45 AND
         o_totalprice between 180000 and 230000;

eval $with_filter EXPLAIN $q3;
eval $with_filter EXPLAIN FORMAT=JSON $q3;
eval $with_filter ANALYZE $q3;
--source include/analyze-format.inc
eval $with_filter ANALYZE FORMAT=JSON $q3;
--sorted_result
eval $with_filter $q3;

eval $without_filter EXPLAIN $q3;
eval $without_filter EXPLAIN FORMAT=JSON $q3;
eval $without_filter ANALYZE $q3;
--source include/analyze-format.inc
eval $without_filter ANALYZE FORMAT=JSON $q3;
--sorted_result
eval $without_filter $q3;

# Check different optimization
eval $with_filter EXPLAIN SELECT STRAIGHT_JOIN o_orderkey, l_linenumber, l_shipdate, l_quantity, o_totalprice
  FROM lineitem JOIN orders ON o_orderkey=l_orderkey
  WHERE  l_shipdate BETWEEN '1997-01-01' AND '1997-06-30' AND
         l_quantity > 45 AND
         o_totalprice between 180000 and 230000;

let $q4=
SELECT o_orderkey, l_linenumber, l_shipdate, o_totalprice
  FROM orders JOIN lineitem ON o_orderkey=l_orderkey
  WHERE l_shipdate BETWEEN '1997-01-01' AND '1997-06-30' AND
        o_totalprice between 200000 and 230000;

eval $with_filter EXPLAIN $q4;
eval $with_filter EXPLAIN FORMAT=JSON $q4;
eval $with_filter ANALYZE $q4;
--source include/analyze-format.inc
eval $with_filter ANALYZE FORMAT=JSON $q4;
--sorted_result
eval $with_filter $q4;

eval $without_filter EXPLAIN $q4;
eval $without_filter EXPLAIN FORMAT=JSON $q4;
eval $without_filter ANALYZE $q4;
--source include/analyze-format.inc
eval $without_filter ANALYZE FORMAT=JSON $q4;
--sorted_result
eval $without_filter $q4;

--echo #
--echo # MDEV-18413: find constraint correlated indexes
--echo #

ALTER TABLE lineitem ADD CONSTRAINT l_date CHECK(l_shipdate < l_receiptdate);

--echo # Filter on l_shipdate is not used because it participates in
--echo # the same constraint as l_receiptdate.
--echo # Access is made on l_receiptdate.
let $q5=
SELECT l_shipdate, l_receiptdate, o_totalprice
FROM orders, lineitem
WHERE o_orderkey=l_orderkey AND
      l_shipdate BETWEEN '1996-10-01' AND '1996-10-10' AND
      l_receiptdate BETWEEN '1996-10-05' AND '1996-10-10' AND
      o_totalprice BETWEEN 200000 AND 250000;

eval $with_filter EXPLAIN $q5;
eval $with_filter EXPLAIN FORMAT=JSON $q5;
eval $with_filter ANALYZE $q5;
--source include/analyze-format.inc
eval $with_filter ANALYZE FORMAT=JSON $q5;
--sorted_result
eval $with_filter $q5;

eval $without_filter EXPLAIN $q5;
eval $without_filter EXPLAIN FORMAT=JSON $q5;
eval $without_filter ANALYZE $q5;
--source include/analyze-format.inc
eval $without_filter ANALYZE FORMAT=JSON $q5;
--sorted_result
eval $without_filter $q5;

ALTER TABLE orders ADD COLUMN o_totaldiscount double;
UPDATE orders SET o_totaldiscount = o_totalprice*(o_custkey/1000);
CREATE INDEX i_o_totaldiscount on orders(o_totaldiscount);

ALTER TABLE orders ADD CONSTRAINT o_price CHECK(o_totalprice > o_totaldiscount);

--echo # Filter on o_totalprice is not used because it participates in
--echo # the same constraint as o_discount.
--echo # Access is made on o_discount.
let $q6=
SELECT o_totaldiscount, o_totalprice, l_shipdate
FROM orders, lineitem
WHERE o_orderkey=l_orderkey AND
      o_totaldiscount BETWEEN 18000 AND 20000 AND
      o_totalprice BETWEEN 200000 AND 220000 AND
      l_shipdate BETWEEN '1996-10-01' AND '1996-12-01';

eval $with_filter EXPLAIN $q6;
eval $with_filter EXPLAIN FORMAT=JSON $q6;
eval $with_filter ANALYZE $q6;
--source include/analyze-format.inc
eval $with_filter ANALYZE FORMAT=JSON $q6;
--sorted_result
eval $with_filter $q6;

eval $without_filter EXPLAIN $q6;
eval $without_filter EXPLAIN FORMAT=JSON $q6;
eval $without_filter ANALYZE $q6;
--source include/analyze-format.inc
eval $without_filter ANALYZE FORMAT=JSON $q6;
--sorted_result
eval $without_filter $q6;

CREATE VIEW v1 AS
SELECT * FROM orders
WHERE o_orderdate BETWEEN '1992-12-01' AND '1997-01-01';

let $q7=
SELECT o_totaldiscount, o_totalprice, l_shipdate
FROM v1, lineitem
WHERE o_orderkey=l_orderkey AND
      o_totaldiscount BETWEEN 18000 AND 20000 AND
      o_totalprice BETWEEN 200000 AND 220000 AND
      l_shipdate BETWEEN '1996-10-01' AND '1996-12-01';

eval $with_filter EXPLAIN $q7;
--replace_regex /"filtered": [0-9e\.\-+]*,/"filtered": "REPLACED",/
eval $with_filter EXPLAIN FORMAT=JSON $q7;
--replace_column 11 #
eval $with_filter ANALYZE $q7;
--replace_regex /("(r_(total|table|other)_time_ms|r_buffer_size|r_filling_time_ms|filtered)": )[^, \n]*/\1"REPLACED"/
eval $with_filter ANALYZE FORMAT=JSON $q7;
--sorted_result
eval $with_filter $q7;

eval $without_filter EXPLAIN $q7;
--replace_regex /"filtered": [0-9e\.\-+]*,/"filtered": "REPLACED",/
eval $without_filter EXPLAIN FORMAT=JSON $q7;
--replace_column 11 #
eval $without_filter ANALYZE $q7;
--replace_regex /("(r_(total|table|other)_time_ms|r_buffer_size|r_filling_time_ms|filtered)": )[^, \n]*/\1"REPLACED"/
eval $without_filter ANALYZE FORMAT=JSON $q7;
--sorted_result
eval $without_filter $q7;

ALTER TABLE lineitem DROP CONSTRAINT l_date;
ALTER TABLE orders DROP CONSTRAINT o_price;
ALTER TABLE orders DROP COLUMN o_totaldiscount;
DROP VIEW v1;

DROP DATABASE dbt3_s001;

use test;

--echo #
--echo # MDEV-18816: potential range filter for one join table with
--echo #             impossible WHERE for another
--echo #

create table t1 (
  pk int not null primary key, c2 varchar(10) , i1 int,key (c2)
) engine=myisam;
insert into t1 values  (1,'a',-5),(2,'a',null);

create table t2 (
 pk int, i1 int, c1 varchar(30) , key c1 (c1(30)), key i1 (i1)
) engine=myisam;
insert into t2 values
  (1,-5,'a'),(2,null,'a'),(3,null,'a'),(4,null,'a'),(5,5,'a'),(6,null,'a'),
  (7,4,'a'),(8,55,'a'),(9,null,'a'),(10,null,'a'),(11,null,'a'),(12,-5,'a'),
  (13,-5,'a'),(14,null,'a'),(15,null,'a'),(16,-5,'a'),(17,-5,'a');

let $q=
select 1
  from t1
       left join
       t2 join t1 as t1_a on t2.i1 = t1_a.pk
       on t1.c2 = t2.c1
where t1_a.pk is null and t1_a.i1 != 3;

eval $q;
eval explain extended $q;

drop table t1,t2;

--echo #
--echo # MDEV-18640: TABLE::prune_range_rowid_filters: Conditional jump or
--echo #             move depends on uninitialized value
--echo #

CREATE TABLE t1 (
  pk INT, i INT, PRIMARY KEY (pk), KEY (pk,i)
) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,10), (7,70), (2,20);

SELECT * FROM t1 WHERE pk < 5;

DROP TABLE t1;

--echo #
--echo # MDEV-18956: Possible rowid filter for subquery for which
--echo #             in_to_exists strategy has been chosen
--echo #

CREATE TABLE t1 (pk int) engine=myisam ;
INSERT INTO t1 VALUES (1),(2);

CREATE TABLE t2 (
  pk int auto_increment PRIMARY KEY,
  i1 int, i2 int, c2 varchar(1),
  KEY (i1), KEY (i2)
) engine=myisam;

INSERT INTO t2 VALUES
  (1,8,6,'t'),(2,5,7,'i'),(3,4,4,'h'),(4,207,38,'d'),(5,183,206,'b'),
  (6,7,null,'o'),(7,1,2,'j'),(8,17,36,'s'),(9,4,5,'q'),(10,0,6,'l'),
  (11,1,9,'j'),(12,5,6,'y'),(13,null,0,'i'),(14,7,7,'x'),(15,5,2,'u');

SELECT * FROM t1 HAVING (7, 9) IN (SELECT t2.i1, t2.i2 FROM t2 WHERE t2.i1 = 3);
EXPLAIN EXTENDED
SELECT * FROM t1 HAVING (7, 9) IN (SELECT t2.i1, t2.i2 FROM t2 WHERE t2.i1 = 3);

DROP TABLE t1,t2;

--echo #
--echo # MDEV-19255: rowid range filter built for range condition
--echo #             that uses in expensive subquery
--echo #

CREATE TABLE t1 (
  pk1 INT PRIMARY KEY, a1 INT, b1 VARCHAR(1), KEY(b1)
) ENGINE=MyISAM;
INSERT INTO t1 VALUES
(10,0,'z'),(11,3,'j'),(12,8,'f'),(13,8,'p'),(14,6,'w'),(15,0,'c'),(16,1,'j'),
(17,1,'f'),(18,5,'v'),(19,3,'f'),(20,2,'q'),(21,8,'y'),(22,0,'a'),(23,9,'w'),
(24,3,'e'),(25,1,'b'),(26,9,'r'),(27,2,'k'),(28,5,'c'),(29,3,'k'),(30,9,'b'),
(31,8,'j'),(32,1,'t'),(33,8,'n'),(34,3,'z'),(35,0,'u'),(36,3,'a'),(37,3,'g'),
(38,1,'f'),(39,6,'p'),(40,6,'m'),(41,6,'t'),(42,7,'i'),(43,4,'h'),(44,3,'d'),
(45,2,'b'),(46,1,'o'),(47,2,'j'),(48,6,'s'),(49,5,'q'),(50,6,'l'),(51,9,'j'),
(52,6,'y'),(53,0,'i'),(54,7,'x'),(55,2,'u'),(56,6,'t'),(57,4,'b'),(58,5,'m'),
(59,4,'x'),(60,8,'x'),(61,6,'v'),(62,8,'m'),(63,4,'j'),(64,8,'z'),(65,2,'a'),
(66,9,'i'),(67,4,'g'),(68,8,'h'),(69,1,'p'),(70,8,'a'),(71,0,'x'),(72,2,'s'),
(73,6,'k'),(74,0,'m'),(75,6,'e'),(76,9,'y'),(77,7,'d'),(78,7,'w'),(79,6,'y'),
(80,9,'s'),(81,9,'x'),(82,6,'l'),(83,9,'f'),(84,8,'x'),(85,1,'p'),(86,7,'y'),
(87,6,'p'),(88,1,'g'),(89,3,'c'),(90,5,'h'),(91,3,'p'),(92,2,'b'),(93,1,NULL),
(94,3,NULL),(95,2,'y'),(96,7,'s'),(97,7,'x'),(98,6,'i'),(99,9,'t'),(100,5,'j'),
(101,0,'u'),(102,7,'r'),(103,2,'x'),(104,8,'e'),(105,8,'i'),(106,5,'q'),
(107,8,'z'),(108,3,'k'),(109,65,NULL);

CREATE TABLE t2 (pk2 INT PRIMARY KEY, a2 INT, b2 VARCHAR(1)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1,1,'x');
INSERT INTO t2 SELECT * FROM t1;

let $q=
SELECT * FROM t1 INNER JOIN t2 ON ( pk1 <> pk2 AND pk1 = a2 )
  WHERE b1 <= ( SELECT MAX(b2) FROM t2 WHERE pk2 <= 1 );

eval $q;
eval EXPLAIN EXTENDED $q;
eval EXPLAIN FORMAT=JSON $q;

DROP TABLE t1,t2;

--echo #
--echo # MDEV-21794: Optimizer flag rowid_filter leads to long query
--echo #
create table t10(a int);
insert into t10 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t11(a int);
insert into t11 select A.a + B.a* 10 + C.a * 100 from t10 A, t10 B, t10 C;

CREATE TABLE t1 (
  el_id int(10) unsigned NOT NULL ,
  el_index blob NOT NULL,
  el_index_60 varbinary(60) NOT NULL,
  filler blob,

  PRIMARY KEY (el_id),
  KEY el_index (el_index(60)),
  KEY el_index_60 (el_index_60,el_id)
);

insert into t1
select
  A.a+1000*B.a,
  A.a+1000*B.a + 10000,
  A.a+1000*B.a + 10000,
  'filler-data-filler-data'
from
  t11 A, t10 B;
analyze table t1 persistent for all;

--echo # This must not use rowid_filter with key=el_index|el_index_60:
explain
select * from t1
where el_index like '10%' and (el_index_60 like '10%' or el_index_60 like '20%');

drop table t10, t11, t1;


--echo #
--echo # MDEV-22160: SIGSEGV in st_join_table::save_explain_data on SELECT
--echo #

set @save_optimizer_switch= @@optimizer_switch;
SET @@optimizer_switch="index_merge_sort_union=OFF";
CREATE TABLE t1 (a INT, b INT, INDEX(a), INDEX(b));
INSERT INTO t1 VALUES (0,0),(0,0),(1,0),(-1,1), (-2,1), (-2,3), (-3,4), (-2,4),(3,3),(3,4),(3,5),(8,8),(8,9),(1,0),(2,0),(0,0),(0,0);
explain
SELECT * FROM t1 WHERE a > 0 AND b=0;
SELECT * FROM t1 WHERE a > 0 AND b=0;
drop table t1;
SET @@optimizer_switch=@save_optimizer_switch;

set @@use_stat_tables=@save_use_stat_tables;

--echo #
--echo # MDEV-21633
--echo # Assertion `tmp >= 0' failed in best_access_path with rowid_filter=ON
--echo #

set @save_optimizer_switch= @@optimizer_switch;
SET optimizer_switch='rowid_filter=on';

CREATE TABLE t1 (
    pk INT AUTO_INCREMENT,
    a INT,
    b VARCHAR(8),
    KEY(a),
    PRIMARY KEY(pk),
    KEY (a,pk)
) ENGINE=MyISAM;

INSERT INTO t1 (a,b) VALUES
    (NULL,'d'),(9,'b'),(2,'x'),(5,'k'),(NULL,'d'),(3,'s'),(5,'k'),(1,'r'),
    (8,'l'),(3,'z'),(1,'c'),(1,'q'),(NULL,'x'),(NULL,'p'),(NULL,'z'),(7,'a'),
    (0,'i'),(3,'s'),(NULL,'h'),(4,'p'),(1,'i'),(4,'f'),(1,'c'),(NULL,'a'),
    (NULL,'x'),(1,'b'),(NULL,'n'),(NULL,'h'),(5,'i'),(6,'e'),(NULL,'i'),
    (7,'e'),(1,'r'),(NULL,'z'),(1,'i'),(14,'c'),(6,'u'),(3,'b'),(4,'z'),
    (2,'c'),(70,'d'),(NULL,'p'),(21,'j'),(6,'e'),(5,'c'),(13,'i'),(42,'d'),
    (80,'s'),(14,'t'),(9,'a'),(0,'2'),(0,NULL),(0,NULL),(0,NULL),(0,''),
    (0,''),(0,'1'),(0,''),(0,''),(0,''),(0,''),(0,NULL),(0,''),(0,''),(0,''),
    (0,NULL),(0,''),(0,''),(0,''),(0,''),(0,''),(0,''),(0,NULL),(0,NULL),
    (0,NULL),(0,''),(0,''),(0,''),(0,''),(0,NULL),(0,''),(0,NULL),(0,NULL),
    (0,''),(0,''),(0,''),(0,NULL),(0,''),(0,NULL),(0,''),(0,''),(0,''),(0,''),
    (0,''),(0,''),(0,''),(0,NULL),(0,''),(0,NULL),(0,'');

CREATE TABLE t2 (c INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1),(2),(3),(4),(5),(6);

SELECT * FROM t1 JOIN t2 WHERE a = c AND pk BETWEEN 4 AND 7 AND a BETWEEN 2 AND 12 AND b != 'foo';

explain SELECT * FROM t1 JOIN t2 WHERE a = c AND pk BETWEEN 4 AND 7 AND a BETWEEN 2 AND 12 AND b != 'foo';

SET optimizer_switch='rowid_filter=off';

SELECT * FROM t1 JOIN t2 WHERE a = c AND pk BETWEEN 4 AND 7 AND a BETWEEN 2 AND 12 AND b != 'foo';

SET @@optimizer_switch=@save_optimizer_switch;

# Cleanup
DROP TABLE t1, t2;