Skip to content

Commit 3a82c25

Browse files
spetruniavuvova
authored andcommitted
Descending indexes code exposed a gap in fix for MDEV-25858.
Extend the fix for MDEV-25858 to handle non-reverse-ordered ORDER BY: If test_if_skip_sort_order() decides to use an index to produce rows in the required ordering, it should disable "Range Checked for Each Record". The fix needs to be backported to earlier versions.
1 parent 791146b commit 3a82c25

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

mysql-test/main/order_by_innodb.result

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ id id
180180
1 NULL
181181
2 1
182182
3 3
183-
create index for_latest_sort on t2 (d1 desc, d2 desc, id desc);
183+
create index for_latest_sort on t2 (d1, d2, id);
184184
select
185185
t1.id,t2.id
186186
from
@@ -198,6 +198,36 @@ id id
198198
1 NULL
199199
2 1
200200
3 3
201+
# Now, same as above but use a DESC index
202+
CREATE TABLE t3 (
203+
id int NOT NULL PRIMARY KEY,
204+
id2 int NOT NULL,
205+
d1 datetime,
206+
d2 timestamp NOT NULL,
207+
KEY id2 (id2)
208+
) engine=innodb;
209+
insert into t3 values
210+
(1,2,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
211+
(2,3,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
212+
(3,3,'2019-03-06 00:00:00','2019-03-05 00:00:00');
213+
create index for_latest_sort on t3 (d1 desc, d2 desc, id desc);
214+
select
215+
t1.id,t3.id
216+
from
217+
t1 left join
218+
t3 on t3.id2 = t1.id and
219+
t3.id = (select dd.id
220+
from t3 dd
221+
where
222+
dd.id2 = t1.id and
223+
d1 > '2019-02-06 00:00:00'
224+
order by
225+
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
226+
);
227+
id id
228+
1 NULL
229+
2 1
230+
3 3
201231
#
202232
# MDEV-27270: Wrong query plan with Range Checked for Each Record and ORDER BY ... LIMIT
203233
#
@@ -220,8 +250,8 @@ dd.d1, dd.d2, dd.id limit 1
220250
id select_type table type possible_keys key key_len ref rows Extra
221251
1 PRIMARY t1 index NULL PRIMARY 4 NULL # Using index
222252
1 PRIMARY t2 eq_ref PRIMARY,id2 PRIMARY 4 func # Using where
223-
2 DEPENDENT SUBQUERY dd index id2,for_latest_sort for_latest_sort 14 NULL # Using where
224-
drop table t1,t2;
253+
2 DEPENDENT SUBQUERY dd range id2,for_latest_sort for_latest_sort 6 NULL # Using where
254+
drop table t1,t2,t3;
225255
# End of 10.2 tests
226256
#
227257
# MDEV-26938 Support descending indexes internally in InnoDB

mysql-test/main/order_by_innodb.test

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ from
170170
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
171171
);
172172

173-
create index for_latest_sort on t2 (d1 desc, d2 desc, id desc);
173+
create index for_latest_sort on t2 (d1, d2, id);
174174

175175
select
176176
t1.id,t2.id
@@ -186,6 +186,38 @@ from
186186
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
187187
);
188188

189+
--echo # Now, same as above but use a DESC index
190+
191+
CREATE TABLE t3 (
192+
id int NOT NULL PRIMARY KEY,
193+
id2 int NOT NULL,
194+
d1 datetime,
195+
d2 timestamp NOT NULL,
196+
KEY id2 (id2)
197+
) engine=innodb;
198+
199+
insert into t3 values
200+
(1,2,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
201+
(2,3,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
202+
(3,3,'2019-03-06 00:00:00','2019-03-05 00:00:00');
203+
create index for_latest_sort on t3 (d1 desc, d2 desc, id desc);
204+
205+
206+
select
207+
t1.id,t3.id
208+
from
209+
t1 left join
210+
t3 on t3.id2 = t1.id and
211+
t3.id = (select dd.id
212+
from t3 dd
213+
where
214+
dd.id2 = t1.id and
215+
d1 > '2019-02-06 00:00:00'
216+
order by
217+
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
218+
);
219+
220+
189221
--echo #
190222
--echo # MDEV-27270: Wrong query plan with Range Checked for Each Record and ORDER BY ... LIMIT
191223
--echo #
@@ -207,7 +239,8 @@ from
207239
order by
208240
dd.d1, dd.d2, dd.id limit 1
209241
);
210-
drop table t1,t2;
242+
243+
drop table t1,t2,t3;
211244

212245
--echo # End of 10.2 tests
213246

mysql-test/suite/engines/funcs/r/ix_using_order.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SHOW CREATE TABLE t1;
1717
Table Create Table
1818
t1 CREATE TABLE `t1` (
1919
`c1` int(11) NOT NULL,
20-
KEY `i1` (`c1`) USING BTREE
20+
KEY `i1` (`c1` DESC) USING BTREE
2121
) ENGINE=ENGINE DEFAULT CHARSET=latin1
2222
DROP TABLE t1;
2323
SHOW TABLES;
@@ -40,7 +40,7 @@ SHOW CREATE TABLE t1;
4040
Table Create Table
4141
t1 CREATE TABLE `t1` (
4242
`c1` int(11) NOT NULL,
43-
UNIQUE KEY `i1` (`c1`) USING BTREE
43+
UNIQUE KEY `i1` (`c1` DESC) USING BTREE
4444
) ENGINE=ENGINE DEFAULT CHARSET=latin1
4545
DROP INDEX i1 ON t1;
4646
DROP TABLE t1;

0 commit comments

Comments
 (0)