繁体   English   中英

mysql索引和优化,使用where; 暂时使用; 使用filesort

[英]mysql indexes and optimizations, Using where; using temporary; using filesort

哪些表和列应该有索引? 我有一个关于flow_permanent_id和entry_id的索引,但似乎是在使用filesort?

我该怎么做才能优化这个?

mysql> explain SELECT COUNT(*) AS count_all, entry_id AS entry_id FROM `votes` WHERE `votes`.`flow_permanent_id` = '4fab490cdc1c82cfa800000a' GROUP BY entry_id;
+----+-------------+-------+------+----------------------------------+----------------------------------+---------+-------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys                    | key                              | key_len | ref   | rows | Extra                                        |
+----+-------------+-------+------+----------------------------------+----------------------------------+---------+-------+------+----------------------------------------------+
|  1 | SIMPLE      | votes | ref  | index_votes_on_flow_permanent_id | index_votes_on_flow_permanent_id | 74      | const |    1 | Using where; Using temporary; Using filesort |
+----+-------------+-------+------+----------------------------------+----------------------------------+---------+-------+------+----------------------------------------------+
1 row in set (0.00 sec)

为了避免使用filesort,你需要一个复合索引( flow_permanent_identry_id ),以便MySQL可以使用WHERE和GROUP BY的索引。

首先 - 使用COUNT(1)而不是COUNT(*),在某些情况下可以提高性能。 切勿使用COUNT(*)。

第二:看起来你使用索引,它列在EXPLAIN输出的'key'列中。 您的“GROUP BY”是导致filesort的东西。 通常它是ORDER BY那是罪魁祸首,但我看到GROUP BY也能做到这一点。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM