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

JZ.*_*JZ. 3 mysql indexing group-by count 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)
Run Code Online (Sandbox Code Playgroud)

Ami*_*Ami 7

为了避免使用filesort,你需要在(flow_permanent_id,entry_id)上使用复合索引,以便MySQL可以使用WHERE和GROUP BY的索引.