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

Wrong index selected by optimizer for WHERE + ORDER BY

    XMLWordPrintable

Details

    Description

      For a query like:

      SELECT 
          `a`.`id`,
          `a`.`person`
      FROM
          `parts` AS `a`
              LEFT JOIN
          `persons` AS `person` ON `a`.`person` = `person`.`id`
              LEFT JOIN
          `bosses` AS `boss` ON `boss`.`id` = `person`.`boss`
              LEFT JOIN
          `logins` AS `login` ON `login`.`id` = `boss`.`login`
      WHERE
          (`login`.`type` = '2')
      ORDER BY `a`.`id` DESC
      LIMIT 30;
      

      The query optimizer returns:

      id select_type table type possible_keys key key_len ref rows Extra
      1 SIMPLE login ref PRIMARY,type_KEY type_KEY 47 const 23 Using where; Using index; Using temporary; Using filesort
      1 SIMPLE boss ref PRIMARY,login_KEY login_KEY 47 login.id 1 Using index
      1 SIMPLE person ref PRIMARY,boss_KEY boss_KEY 47 boss.id 1 Using index
      1 SIMPLE a ref person_KEY person_KEY 47 person.id 6474  

      The query takes a couple of seconds to complete. If I just add an index hint to PRIMARY on the parts table (alias `a`), the query takes 0.00 sec to complete. Every attribute used in the example is indexed.

      Attachments

        Activity

          People

            psergei Sergei Petrunia
            nochum Nochum Sossonko
            Votes:
            0 Vote for this issue
            Watchers:
            2 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.