Mysql不使用group by和order by

时间:2014-05-13 15:54:49

标签: mysql performance optimization indexing

我的表用户有以下列。

id,name,updated_at

使用解释计划查询

mysql> explain select * from users group by users.id order by users.updated_at desc limit 10;
+----+-------------+-------------+------+---------------+------+---------+------+--------+----------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows   | Extra          |
+----+-------------+-------------+------+---------------+------+---------+------+--------+----------------+
|  1 | SIMPLE      | users | ALL  | NULL          | NULL | NULL    | NULL | 190551 | Using filesort |
+----+-------------+-------------+------+---------------+------+---------+------+--------+----------------+
1 row in set (0.00 sec)

创建了新索引

create index test_id_updated_at on users (id, updated_at);

创建新索引后,仍然会在解释计划中获得相同的结果。

mysql> explain select * from users group by users.id order by users.updated_at desc limit 10;
+----+-------------+-------------+------+---------------+------+---------+------+--------+----------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows   | Extra          |
+----+-------------+-------------+------+---------------+------+---------+------+--------+----------------+
|  1 | SIMPLE      | users | ALL  | NULL          | NULL | NULL    | NULL | 190551 | Using filesort |
+----+-------------+-------------+------+---------------+------+---------+------+--------+----------------+
1 row in set (0.00 sec)

在查询中强制使用新索引后,仍然会得到相同的结果。

我不明白为什么它会说'使用filesort'在创建新索引之后。

1 个答案:

答案 0 :(得分:0)

我复制了你的cenario,而mysql使用了索引:

mysql> explain SELECT * FROM test_index GROUP BY id ORDER BY updated_at DESC;
+----+-------------+------------+------+---------------+------+---------+------+--------+---------------------------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows   | Extra                           |
+----+-------------+------------+------+---------------+------+---------+------+--------+---------------------------------+
|  1 | SIMPLE      | test_index | ALL  | NULL          | NULL | NULL    | NULL | 393520 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+--------+---------------------------------+
1 row in set (0.00 sec)

mysql> CREATE INDEX index1 ON test_index(id, updated_at);
Query OK, 0 rows affected (1.85 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain SELECT * FROM test_index GROUP BY id ORDER BY updated_at DESC;
+----+-------------+------------+-------+---------------+--------+---------+------+--------+---------------------------------+
| id | select_type | table      | type  | possible_keys | key    | key_len | ref  | rows   | Extra                           |
+----+-------------+------------+-------+---------------+--------+---------+------+--------+---------------------------------+
|  1 | SIMPLE      | test_index | index | NULL          | index1 | 12      | NULL | 393520 | Using temporary; Using filesort |
+----+-------------+------------+-------+---------------+--------+---------+------+--------+---------------------------------+
1 row in set (0.00 sec)

也许你的mysql版本?在5.5上测试过。

很难优化此查询(删除使用的filesort和临时),而不知道完整的表结构以及要用此检索的内容(指定字段而不是“*”)

相关问题