|
| 1 | + |
| 2 | +# MDEV-36410 wrong result with index_merge on indexes having descending primary key |
| 3 | +# |
| 4 | +set optimizer_trace='enabled=on'; |
| 5 | +SET @save_sort_buffer_size=@@sort_buffer_size; |
| 6 | +SET SESSION sort_buffer_size = 1024*16; |
| 7 | +CREATE TABLE t1 ( |
| 8 | +id bigint(20) NOT NULL, |
| 9 | +title varchar(255) NOT NULL, |
| 10 | +status tinyint(4) DEFAULT 0, |
| 11 | +country_code varchar(5) DEFAULT NULL, |
| 12 | +PRIMARY KEY (id), |
| 13 | +KEY idx_status (status), |
| 14 | +KEY idx_country_code_status_id (country_code,status,id DESC) |
| 15 | +) ENGINE=InnoDB; |
| 16 | +INSERT INTO t1(id,title,status,country_code) |
| 17 | +SELECT seq, CONCAT('abc', seq), seq%10, CONCAT('C', seq%5) FROM seq_1_to_500; |
| 18 | +# This must not use index_merge: |
| 19 | +EXPLAIN |
| 20 | +SELECT * FROM t1 WHERE country_code ='C1' and `status` =1; |
| 21 | +id select_type table type possible_keys key key_len ref rows Extra |
| 22 | +1 SIMPLE t1 ref idx_status,idx_country_code_status_id idx_status 2 const 50 Using where |
| 23 | +set @trace= (select JSON_EXTRACT(trace, '$**.range_scan_alternatives[*]') |
| 24 | +from INFORMATION_SCHEMA.OPTIMIZER_TRACE); |
| 25 | +select json_detailed(json_extract(@trace, '$[*].index')) as INDEXES; |
| 26 | +INDEXES |
| 27 | +[ |
| 28 | + "idx_status", |
| 29 | + "idx_country_code_status_id" |
| 30 | +] |
| 31 | +select json_detailed(json_extract(@trace, '$[*].rowid_ordered')) as ROR; |
| 32 | +ROR |
| 33 | +[ |
| 34 | + true, |
| 35 | + false |
| 36 | +] |
| 37 | +DROP table t1; |
| 38 | +# Now, try with indexes using ASC ordering and PK using DESC |
| 39 | +CREATE TABLE t1 ( |
| 40 | +id bigint(20) NOT NULL, |
| 41 | +title varchar(255) NOT NULL, |
| 42 | +status tinyint(4) DEFAULT 0, |
| 43 | +country_code varchar(5) DEFAULT NULL, |
| 44 | +PRIMARY KEY (id DESC), |
| 45 | +KEY idx_status (status), |
| 46 | +KEY idx_country_code_status_id (country_code,status,id) |
| 47 | +) ENGINE=InnoDB; |
| 48 | +INSERT INTO t1(id,title,status,country_code) |
| 49 | +SELECT seq, CONCAT('abc', seq), seq%10, CONCAT('C', seq%5) FROM seq_1_to_500; |
| 50 | +# Must not use index_merge: |
| 51 | +EXPLAIN |
| 52 | +SELECT * FROM t1 WHERE country_code ='C1' and status = 1; |
| 53 | +id select_type table type possible_keys key key_len ref rows Extra |
| 54 | +1 SIMPLE t1 ref idx_status,idx_country_code_status_id idx_status 2 const 50 Using where |
| 55 | +set @trace= (select JSON_EXTRACT(trace, '$**.range_scan_alternatives[*]') |
| 56 | +from INFORMATION_SCHEMA.OPTIMIZER_TRACE); |
| 57 | +select json_detailed(json_extract(@trace, '$[*].index')) as INDEXES; |
| 58 | +INDEXES |
| 59 | +[ |
| 60 | + "idx_status", |
| 61 | + "idx_country_code_status_id" |
| 62 | +] |
| 63 | +select json_detailed(json_extract(@trace, '$[*].rowid_ordered')) as ROR; |
| 64 | +ROR |
| 65 | +[ |
| 66 | + true, |
| 67 | + false |
| 68 | +] |
| 69 | +DROP TABLE t1; |
| 70 | +# Now, try with indexes using DESC ordering and PK using DESC |
| 71 | +CREATE TABLE t1 ( |
| 72 | +id bigint(20) NOT NULL, |
| 73 | +title varchar(255) NOT NULL, |
| 74 | +status tinyint(4) DEFAULT 0, |
| 75 | +country_code varchar(5) DEFAULT NULL, |
| 76 | +PRIMARY KEY (id DESC), |
| 77 | +KEY idx_status (status), |
| 78 | +KEY idx_country_code_status_id (country_code,status,id DESC) |
| 79 | +) ENGINE=InnoDB; |
| 80 | +INSERT INTO t1(id,title,status,country_code) |
| 81 | +SELECT seq, CONCAT('abc', seq), seq%10, CONCAT('C', seq%5) FROM seq_1_to_500; |
| 82 | +# Must not use index_merge: |
| 83 | +EXPLAIN |
| 84 | +SELECT * FROM t1 WHERE country_code ='C1' and status = 1; |
| 85 | +id select_type table type possible_keys key key_len ref rows Extra |
| 86 | +1 SIMPLE t1 ref idx_status,idx_country_code_status_id idx_status 2 const 50 Using where |
| 87 | +set @trace= (select JSON_EXTRACT(trace, '$**.range_scan_alternatives[*]') |
| 88 | +from INFORMATION_SCHEMA.OPTIMIZER_TRACE); |
| 89 | +select json_detailed(json_extract(@trace, '$[*].index')) as INDEXES; |
| 90 | +INDEXES |
| 91 | +[ |
| 92 | + "idx_status", |
| 93 | + "idx_country_code_status_id" |
| 94 | +] |
| 95 | +select json_detailed(json_extract(@trace, '$[*].rowid_ordered')) as ROR; |
| 96 | +ROR |
| 97 | +[ |
| 98 | + true, |
| 99 | + false |
| 100 | +] |
| 101 | +DROP TABLE t1; |
| 102 | +SET sort_buffer_size= @save_sort_buffer_size; |
0 commit comments