You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bug#34306497 - Low limit heuristic used unnecessarily with descending scans
Issue:
The low limit heuristic is not working well for order by desc as it
chooses the wrong index.
Analysis:
Because of the low limit we relook at the access method that is chosen
which in this case is the index. If the chosen range scan can provide
the order we skip the heuristic. At this point since const optimization
is not done the optimizer thinks the current range scan cannot provide
order so it relooks at other options. Since the primary key can provide
the order it chooses that one.
Fix:
Since const optimisation is not done when we check if an index can
provide order, the key part of the index and the order by field did not
match. The fix is to check if the index can provide the order by
skipping constant key parts preceding to the specified order by
expression. Later we check if the best range scan can provide order by
reversing it.
Change-Id: I1f967670c195f49fe39284e17e2d3c70030ad466
Copy file name to clipboardExpand all lines: mysql-test/r/order_by_limit.result
+85Lines changed: 85 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -555,3 +555,88 @@ Warnings:
555
555
Note 1003 /* select#1 */ select `test`.`test`.`a` AS `a`,`test`.`test`.`b` AS `b`,`test`.`test`.`c` AS `c`,`test`.`test`.`d` AS `d` from `test`.`test` where ((`test`.`test`.`c` = DATE'2022-06-13') and (`test`.`test`.`a` > 222)) order by `test`.`test`.`c`,`test`.`test`.`a` limit 10
556
556
DROP PROCEDURE insertProc;
557
557
DROP TABLE test;
558
+
#
559
+
# Bug#34306497 - Low limit heuristic used unnecessarily with descending scans
a(i) AS (SELECT 0 UNION ALL SELECT i+1 FROM a WHERE i < 9 ),
565
+
b(i) AS (SELECT x.i + y.i * 10 + z.i * 100 FROM a x, a y, a z)
566
+
SELECT b.i, b.i %2 FROM b ORDER BY i);
567
+
ANALYZE TABLE t1;
568
+
Table Op Msg_type Msg_text
569
+
test.t1 analyze status OK
570
+
EXPLAIN SELECT * FROM t1 WHERE f2 = 1 AND f1 <= 100 ORDER BY f1 DESC LIMIT 1;
571
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
572
+
1 SIMPLE t1 NULL range PRIMARY,f2 f2 9 NULL 50 100.00 Using where; Backward index scan; Using index
573
+
Warnings:
574
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` where ((`test`.`t1`.`f2` = 1) and (`test`.`t1`.`f1` <= 100)) order by `test`.`t1`.`f1` desc limit 1
575
+
SELECT * FROM t1 WHERE f2 = 1 AND f1 <= 100 ORDER BY f1 DESC LIMIT 1;
576
+
f1 f2
577
+
99 1
578
+
CREATE TABLE t2 (
579
+
f1 INTEGER,
580
+
f2 INTEGER,
581
+
f3 INTEGER,
582
+
f4 INTEGER,
583
+
f5 INTEGER,
584
+
PRIMARY KEY (f1), KEY(f2,f3,f4,f5,f1));
585
+
INSERT INTO t2 (
586
+
WITH RECURSIVE a (i) AS (SELECT 0 UNION ALL SELECT i+1 FROM a WHERE i < 9 ),
587
+
b (i) AS (SELECT x.i + y.i * 10 + z.i * 100 FROM a x, a y, a z)
588
+
SELECT b.i, b.i%2, b.i%3, b.i%4, b.i%5 FROM b ORDER BY i);
589
+
ANALYZE TABLE t2;
590
+
Table Op Msg_type Msg_text
591
+
test.t2 analyze status OK
592
+
EXPLAIN SELECT * FROM t2
593
+
WHERE f3 = 1 AND f2 = 1 AND f4 = 3 AND f5 IN(2,3) ORDER BY f4 DESC LIMIT 1;
594
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
595
+
1 SIMPLE t2 NULL range f2 f2 20 NULL 33 100.00 Using where; Backward index scan; Using index
596
+
Warnings:
597
+
Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t2`.`f4` AS `f4`,`test`.`t2`.`f5` AS `f5` from `test`.`t2` where ((`test`.`t2`.`f4` = 3) and (`test`.`t2`.`f2` = 1) and (`test`.`t2`.`f3` = 1) and (`test`.`t2`.`f5` in (2,3))) order by `test`.`t2`.`f4` desc limit 1
598
+
SELECT * FROM t2
599
+
WHERE f3 = 1 AND f2 = 1 AND f4 = 3 AND f5 IN(2,3) ORDER BY f4 DESC LIMIT 1;
600
+
f1 f2 f3 f4 f5
601
+
943 1 1 3 3
602
+
EXPLAIN SELECT * FROM t2
603
+
WHERE f2 = 1 AND f3 = 2 AND f4 = 3 AND f5 IN(2,3) ORDER BY f3,f4 DESC LIMIT 1;
604
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
605
+
1 SIMPLE t2 NULL range f2 f2 20 NULL 33 100.00 Using where; Using index
606
+
Warnings:
607
+
Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t2`.`f4` AS `f4`,`test`.`t2`.`f5` AS `f5` from `test`.`t2` where ((`test`.`t2`.`f4` = 3) and (`test`.`t2`.`f3` = 2) and (`test`.`t2`.`f2` = 1) and (`test`.`t2`.`f5` in (2,3))) order by `test`.`t2`.`f3`,`test`.`t2`.`f4` desc limit 1
608
+
SELECT * FROM t2
609
+
WHERE f2 = 1 AND f3 = 2 AND f4 = 3 AND f5 IN(2,3) ORDER BY f3,f4 DESC LIMIT 1;
610
+
f1 f2 f3 f4 f5
611
+
47 1 2 3 2
612
+
EXPLAIN SELECT * FROM t2
613
+
WHERE f2 = 1 AND f3 > 1 AND f4 = 3 AND f5 IN(2,3) ORDER BY f2,f3 DESC LIMIT 1;
614
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
615
+
1 SIMPLE t2 NULL range f2 f2 10 NULL 166 2.00 Using where; Backward index scan; Using index
616
+
Warnings:
617
+
Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t2`.`f4` AS `f4`,`test`.`t2`.`f5` AS `f5` from `test`.`t2` where ((`test`.`t2`.`f4` = 3) and (`test`.`t2`.`f2` = 1) and (`test`.`t2`.`f3` > 1) and (`test`.`t2`.`f5` in (2,3))) order by `test`.`t2`.`f2`,`test`.`t2`.`f3` desc limit 1
618
+
SELECT * FROM t2
619
+
WHERE f2 = 1 AND f3 > 1 AND f4 = 3 AND f5 IN(2,3) ORDER BY f2,f3 DESC LIMIT 1;
620
+
f1 f2 f3 f4 f5
621
+
983 1 2 3 3
622
+
EXPLAIN SELECT * FROM t2
623
+
WHERE f2 = 1 AND f3 > 1 AND f4 = 3 ORDER BY f2,f3,f5 DESC LIMIT 1;
624
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
625
+
1 SIMPLE t2 NULL range f2 f2 10 NULL 166 10.00 Using where; Using index; Using filesort
626
+
Warnings:
627
+
Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t2`.`f4` AS `f4`,`test`.`t2`.`f5` AS `f5` from `test`.`t2` where ((`test`.`t2`.`f4` = 3) and (`test`.`t2`.`f2` = 1) and (`test`.`t2`.`f3` > 1)) order by `test`.`t2`.`f2`,`test`.`t2`.`f3`,`test`.`t2`.`f5` desc limit 1
628
+
SELECT * FROM t2
629
+
WHERE f2 = 1 AND f3 > 1 AND f4 = 3 ORDER BY f2,f3,f5 DESC LIMIT 1;
630
+
f1 f2 f3 f4 f5
631
+
59 1 2 3 4
632
+
EXPLAIN SELECT * FROM t2
633
+
WHERE f2 = 1 AND f3 > 1 AND f4 = 3 ORDER BY f2 DESC ,f3 DESC ,f5 DESC LIMIT 1;
634
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
635
+
1 SIMPLE t2 NULL range f2 f2 10 NULL 166 10.00 Using where; Backward index scan; Using index
636
+
Warnings:
637
+
Note 1003 /* select#1 */ select `test`.`t2`.`f1` AS `f1`,`test`.`t2`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t2`.`f4` AS `f4`,`test`.`t2`.`f5` AS `f5` from `test`.`t2` where ((`test`.`t2`.`f4` = 3) and (`test`.`t2`.`f2` = 1) and (`test`.`t2`.`f3` > 1)) order by `test`.`t2`.`f2` desc,`test`.`t2`.`f3` desc,`test`.`t2`.`f5` desc limit 1
638
+
SELECT * FROM t2
639
+
WHERE f2 = 1 AND f3 > 1 AND f4 = 3 ORDER BY f2 DESC ,f3 DESC ,f5 DESC LIMIT 1;
0 commit comments