我正在尝试解决服务器上的高负载问题,今天由于某种原因 MySQL 占用了过多的 CPU 时间。我启用了慢查询,发现这个查询和其他类似的查询。
这些表有大约 700k 行。
SELECT SUM( likes ) AS likes, image_id FROM post_files_likes WHERE image_id NOT IN(563593,591800,578109,581180,515832,646310,670601,626185,689090,80019,399472,468198) AND date > DATE_SUB( '2013-08-19' , INTERVAL 1 MONTH ) GROUP BY image_id ORDER BY likes DESC LIMIT 12;
`
mysql> describe post_files_likes
-> ;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| image_id | int(10) | NO | MUL | NULL | |
| likes | int(11) | NO | | NULL | |
| date | date | NO | | NULL | |
+----------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> EXPLAIN SELECT SUM( likes ) AS likes, image_id FROM post_files_likes WHERE image_id NOT IN(563593,591800,578109,581180,515832,646310,670601,626185,689090,80019,399472,468198) AND date > DATE_SUB( '2013-08-19' , INTERVAL 1 MONTH ) GROUP BY image_id ORDER BY likes DESC LIMIT 12;
+----+-------------+------------------+-------+---------------------+------------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+-------+---------------------+------------+---------+------+--------+----------------------------------------------+
| 1 | SIMPLE | post_files_likes | range | image_id,image_id_2 | image_id_2 | 4 | NULL | 709885 | Using where; Using temporary; Using filesort |
+----+-------------+------------------+-------+---------------------+------------+---------+------+--------+----------------------------------------------+
1 row in set (2.92 sec)
我已经运行了几次这个查询,得到了 0 到 30 秒之间的任何时间。
这个查询有什么根本错误吗?还是由于 mysql 的其他问题,此查询需要很长时间?
编辑
SHOW INDEX FROM post_files_likes;
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| post_files_likes | 0 | PRIMARY | 1 | id | A | 710969 | NULL | NULL | | BTREE | |
| post_files_likes | 0 | image_id | 1 | image_id | A | 355484 | NULL | NULL | | BTREE | |
| post_files_likes | 0 | image_id | 2 | date | A | 710969 | NULL | NULL | | BTREE | |
| post_files_likes | 1 | image_id_2 | 1 | image_id | A | 355484 | NULL | NULL | | BTREE | |
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
4 rows in set (0.05 sec)
编辑添加索引
mysql> SHOW INDEX FROM post_files_likes;
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| post_files_likes | 0 | PRIMARY | 1 | id | A | 711181 | NULL | NULL | | BTREE | |
| post_files_likes | 0 | image_id | 1 | image_id | A | NULL | NULL | NULL | | BTREE | |
| post_files_likes | 0 | image_id | 2 | date | A | 711181 | NULL | NULL | | BTREE | |
| post_files_likes | 1 | image_id_2 | 1 | image_id | A | 237060 | NULL | NULL | | BTREE | |
| post_files_likes | 1 | likes | 1 | likes | A | 445 | NULL | NULL | | BTREE | |
| post_files_likes | 1 | likes | 2 | date | A | 4709 | NULL | NULL | | BTREE | |
| post_files_likes | 1 | likes | 3 | image_id | A | 711181 | NULL | NULL | | BTREE | |
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+