[MDEV-25431] Make optimizer capable of reversing GROUP BY direction to eliminate filesort requirement for ORDER BY Created: 2021-04-16  Updated: 2021-04-16

Status: Open
Project: MariaDB Server
Component/s: None
Fix Version/s: None

Type: Task Priority: Major
Reporter: Vicențiu Ciorbaru Assignee: Vicențiu Ciorbaru
Resolution: Unresolved Votes: 0
Labels: None


 Description   

The following 3 queries should have identical query plans (only using an index)

create table t1 (pk int primary key, a int, b int, key(a,b));
insert into t1 with recursive cte(a) as (select 1 as a union select a+1 from cte where a <100) select a, a*10, a*100 from cte;
explain select a, b, count(a) from t1 group by a,      b      order by a desc;
explain select a, b, count(a) from t1 group by a desc, b      order by a desc;
explain select a, b, count(a) from t1 group by a desc, b desc order by a desc;

MariaDB [test]> explain select a, b, count(a) from t1 group by a,      b      order by a desc;
+------+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                                        |
+------+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+
|    1 | SIMPLE      | t1    | index | NULL          | a    | 10      | NULL | 100  | Using index; Using temporary; Using filesort |
+------+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+
1 row in set (0.000 sec)
 
MariaDB [test]> explain select a, b, count(a) from t1 group by a desc, b      order by a desc;
+------+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                                        |
+------+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+
|    1 | SIMPLE      | t1    | index | NULL          | a    | 10      | NULL | 100  | Using index; Using temporary; Using filesort |
+------+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+
1 row in set (0.000 sec)
 
MariaDB [test]> explain select a, b, count(a) from t1 group by a desc, b desc order by a desc;
+------+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+-------+-------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | t1    | index | NULL          | a    | 10      | NULL | 100  | Using index |
+------+-------------+-------+-------+---------------+------+---------+------+------+-------------+


Generated at Thu Feb 08 09:37:39 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.