【问题标题】:Mysql not using index on group by and order byMysql 没有在 group by 和 order by 上使用索引
【发布时间】: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


【解决方案1】:

我复制了你的场景,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 上对此进行了测试。

如果不知道完整的表结构和你想用它检索什么(指定字段而不是“*”),很难优化这个查询(删除使用文件排序和临时)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多