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

poor query plan: ideal index from ORDER BY field is not used when extra columns retrieved - mrr=on and join_cache_level>2

    XMLWordPrintable

Details

    Description

      base tables and content:

      | customers | CREATE TABLE `customers` (
        `customers_id` int(11) NOT NULL AUTO_INCREMENT,
        `customers_username` varchar(60) NOT NULL DEFAULT '',
        PRIMARY KEY (`customers_id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=50001 DEFAULT CHARSET=latin1 |
       
      | customers_info | CREATE TABLE `customers_info` (
        `customers_id` bigint(20) unsigned NOT NULL DEFAULT '0',
        `account_created_date` datetime DEFAULT NULL,
        PRIMARY KEY (`customers_id`),
        KEY `account_created_date_migrated_status` (`account_created_date`)
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
       
      insert into customers (customers_id) select * from  seq_1_to_50000;
      insert into customers_info select customers_id,NOW() - INTERVAL  customers_id DAY from customers;  

      An ideal query plan:

      explain select customers.customers_id, customers_info.account_created_date from customers JOIN customers_info USING(customers_id) ORDER BY account_created_date DESC LIMIT 1;
      +------+-------------+----------------+--------+---------------+--------------------------------------+---------+----------------------------------+------+--------------------------+
      | id   | select_type | table          | type   | possible_keys | key                                  | key_len | ref                              | rows | Extra                    |
      +------+-------------+----------------+--------+---------------+--------------------------------------+---------+----------------------------------+------+--------------------------+
      |    1 | SIMPLE      | customers_info | index  | PRIMARY       | account_created_date_migrated_status | 9       | NULL                             |    1 | Using index              |
      |    1 | SIMPLE      | customers      | eq_ref | PRIMARY       | PRIMARY                              | 4       | test.customers_info.customers_id |    1 | Using where; Using index |
      +------+-------------+----------------+--------+---------------+--------------------------------------+---------+----------------------------------+------+--------------------------+
      2 rows in set (0.02 sec)

      When we add another field to result, customers.customers_username then we end up doing a temporary and filesort.

      MariaDB [test]> explain select customers.customers_id, customers.customers_username, customers_info.account_created_date from customers JOIN customers_info USING(customers_id) ORDER BY account_created_date DESC LIMIT 1
          -> ;
      +------+-------------+----------------+--------+---------------+--------------------------------------+---------+----------------------------------+-------+--------------------------------------------------------------------+
      | id   | select_type | table          | type   | possible_keys | key                                  | key_len | ref                              | rows  | Extra                                                              |
      +------+-------------+----------------+--------+---------------+--------------------------------------+---------+----------------------------------+-------+--------------------------------------------------------------------+
      |    1 | SIMPLE      | customers_info | index  | PRIMARY       | account_created_date_migrated_status | 9       | NULL                             | 50386 | Using index; Using temporary; Using filesort                       |
      |    1 | SIMPLE      | customers      | eq_ref | PRIMARY       | PRIMARY                              | 4       | test.customers_info.customers_id |     1 | Using where; Using join buffer (flat, BKAH join); Key-ordered scan |

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              danblack Daniel Black
              Votes:
              1 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.