diff --git a/mysql-test/suite/innodb/r/innodb_mysql.result b/mysql-test/suite/innodb/r/innodb_mysql.result index 31f6dafd082..54c6f1ef262 100644 --- a/mysql-test/suite/innodb/r/innodb_mysql.result +++ b/mysql-test/suite/innodb/r/innodb_mysql.result @@ -1193,7 +1193,7 @@ INSERT INTO t1 SELECT a + 32, MOD(a + 32, 20), 1 FROM t1; INSERT INTO t1 SELECT a + 64, MOD(a + 64, 20), 1 FROM t1; EXPLAIN SELECT b, SUM(c) FROM t1 GROUP BY b; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 index NULL b 5 NULL 128 +1 SIMPLE t1 ALL NULL NULL NULL NULL 128 Using temporary; Using filesort EXPLAIN SELECT SQL_BIG_RESULT b, SUM(c) FROM t1 GROUP BY b; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 128 Using filesort diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 4f0fa3cdb00..c7ed2ebd810 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -30550,6 +30550,13 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table, add("fanout", fanout); } + /* + Force using an index for sorting if there was no ref key + and FORCE INDEX was used. + */ + if (table->force_index && ref_key < 0) + read_time= DBL_MAX; + Json_writer_array possible_keys(thd,"possible_keys"); for (nr=0; nr < table->s->keys ; nr++) { @@ -30702,8 +30709,7 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table, there is either a group by or a FORCE_INDEX - If the new cost is better than read_time */ - if (((table->force_index || group) && best_key < 0 && ref_key < 0) || - range_cost < read_time) + if (range_cost < read_time) { read_time= range_cost; possible_key.add("chosen", true);