Estou tentando solucionar um problema de alta carga em um servidor. Hoje, por alguma razão, o MySQL está ocupando um tempo excessivo de CPU. Eu ativei consultas lentas e encontrei essa consulta e outras semelhantes.
As tabelas têm cerca de 700 mil linhas.
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)
Eu executei essa consulta algumas vezes e fiquei em qualquer lugar entre 0 e 30 segundos.
Existe algo fundamentalmente errado com esta consulta? ou esta consulta demora muito devido a outros problemas com o mysql?
EDITAR
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)
EDIT Índices adicionados
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 | |
+------------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+