【发布时间】:2014-05-13 15:54:49
【问题描述】:
我的表用户具有以下列。
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)
在查询中强制使用新索引后,仍然得到相同的结果。
我不明白为什么在创建新索引后显示“使用文件排序”。
【问题讨论】:
-
查询使用 GROUP BY。为什么?另外,就个人而言,我更喜欢在执行 EXPLAINs 以粘贴到此处时使用 \G。我觉得更容易阅读。 (当然还要提供适当的 DDL)
-
请附上表
users的完整定义。
标签: mysql performance optimization indexing