Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-25431

Make optimizer capable of reversing GROUP BY direction to eliminate filesort requirement for ORDER BY

    XMLWordPrintable

Details

    • Task
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • None
    • None
    • 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 |
      +------+-------------+-------+-------+---------------+------+---------+------+------+-------------+
      

      Attachments

        Activity

          People

            cvicentiu Vicențiu Ciorbaru
            cvicentiu Vicențiu Ciorbaru
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.