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

Use DESC indexes for MIN/MAX optimization

    XMLWordPrintable

Details

    Description

      As of c10e10c6, opt_sum.cc is still stubbed for DESC indexes

            if (part->key_part_flag & HA_REVERSE_SORT)
              break; // TODO MDEV-13756
      

      Given that we are about to close MDEV-13756, it looks strange: either it was forgotten and needs to be enabled, or maybe it requires a better reference.

      Given the above, the optimization expectedly doesn't work.

      --source include/have_sequence.inc
       
      create or replace table t1 (id int, key(id));
      insert into t1 select seq from seq_1_to_100 order by rand(1);
      explain extended select max(id) from t1 where id > 50;
       
      create or replace table t1 (id int, key(id desc));
      insert into t1 select seq from seq_1_to_100 order by rand(1);
      explain extended select max(id) from t1 where id > 50;
       
      # Cleanup
      drop table t1;
      

      With the ASC index, it is

      preview-10.8-MDEV-13756-desc-indexes c10e10c6

      +------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------+
      | id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                        |
      +------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------+
      |    1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Select tables optimized away |
      +------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------+
      

      With the DESC index, it is

      +------+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+
      | id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra                    |
      +------+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+
      |    1 | SIMPLE      | t1    | range | id            | id   | 5       | NULL | 50   |   100.00 | Using where; Using index |
      +------+-------------+-------+-------+---------------+------+---------+------+------+----------+--------------------------+
      

      Attachments

        Issue Links

          Activity

            People

              ycp Yuchen Pei
              elenst Elena Stepanova
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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