<?xml version="1.0" encoding="utf-8"?>
<launchpad-bug id="1002146">
  <date_last_updated>2012-05-21 05:39:29.376139+00:00</date_last_updated>
  <api_links>
    <bug_api_link>https://api.launchpad.net/1.0/bugs/1002146</bug_api_link>
    <bug_owner_link>https://api.launchpad.net/1.0/~igorb-seattle</bug_owner_link>
    <milestone_link>https://api.launchpad.net/1.0/maria/+milestone/5.5</milestone_link>
    <linked_branches_collection_link>https://api.launchpad.net/1.0/bugs/1002146/linked_branches</linked_branches_collection_link>
    <activity_link>https://api.launchpad.net/1.0/bugs/1002146/activity</activity_link>
  </api_links>
  <bug_web_link>https://bugs.launchpad.net/bugs/1002146</bug_web_link>
  <owner>Igor Babaev</owner>
  <assignee>Igor Babaev</assignee>
  <milestone_title>Maria 5.5</milestone_title>
  <duplicate_link></duplicate_link>
  <duplicate_bug_id></duplicate_bug_id>
  <title>Unneeded filesort when executing a GROUP BY query</title>
  <status>Confirmed</status>
  <importance>Medium</importance>
  <created>2012-05-21 05:38:47.604235+00:00</created>
  <description>
<![CDATA[If to create tables t1 and t2 in MariaDB 5.5 as with the following commands

CREATE TABLE t1 (
  col_int_key INT,
  pk INT,
  PRIMARY KEY (pk),
  KEY (col_int_key)
) ENGINE=INNODB;
INSERT INTO t1 VALUES (2,3),(3,2),(3,5),(4,6);
CREATE TABLE t2 (
  col_int_key INT,
  pk INT,
  PRIMARY KEY (pk),
  KEY (col_int_key)
) ENGINE=INNODB;
INSERT INTO t2 VALUES (0,9),(3,10),(4,6),(6,1),(100,3),(200,5);

and

set join_cache_level=0;

then the execution plan for the query

SELECT t2.col_int_key AS field1
FROM t2 USE INDEX(col_int_key) STRAIGHT_JOIN t1 ON t2.col_int_key
WHERE t2.pk < 7 AND t2.col_int_key <> 7
GROUP BY field1;

will use unneeded filesort:

MariaDB [test]> explain SELECT t2.col_int_key AS field1 FROM t2 USE INDEX(col_int_key) STRAIGHT_JOIN t1 ON t2.t_key WHERE t2.pk < 7 AND t2.col_int_key <> 7 GROUP BY field1;
+------+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------------------------------------+
| id   | select_type | table | type  | possible_keys | key         | key_len | ref  | rows | Extra                                               |
+------+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------------------------------------+
|    1 | SIMPLE      | t2    | range | col_int_key   | col_int_key | 5       | NULL |    5 | Using where; Usinx; Using temporary; Using filesort |
|    1 | SIMPLE      | t1    | index | NULL          | PRIMARY     | 4       | NULL |    4 | Using index                                         |
+------+-------------+-------+-------+---------------+-------------+---------+------+------+-----------------------------------------------------+]]>  </description>
  <activities>
    <activity datechanged="2012-05-21T05:38:47.604235+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>bug</whatchanged>
      <person>Igor Babaev</person>
      <message>added bug</message>
    </activity>
    <activity datechanged="2012-05-21T05:39:04.462526+00:00">
      <oldvalue>
<![CDATA[New]]>      </oldvalue>
      <newvalue>
<![CDATA[Confirmed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Igor Babaev</person>
      <message></message>
    </activity>
    <activity datechanged="2012-05-21T05:39:15.994634+00:00">
      <oldvalue>
<![CDATA[Undecided]]>      </oldvalue>
      <newvalue>
<![CDATA[Medium]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Igor Babaev</person>
      <message></message>
    </activity>
    <activity datechanged="2012-05-21T05:39:23.568228+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[Igor Babaev (igorb-seattle)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Igor Babaev</person>
      <message></message>
    </activity>
    <activity datechanged="2012-05-21T05:39:28.424053+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[5.5]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Igor Babaev</person>
      <message></message>
    </activity>
  </activities>
</launchpad-bug>
