如何使这个SQL查询更快?

时间:2011-09-26 19:26:44

标签: mysql performance

我有以下查询:

SELECT DISTINCT `movies_manager_movie`.`id`, 
                 `movies_manager_movie`.`title`, 
                 `movies_manager_movie`.`original_title`, 
                 `movies_manager_movie`.`synopsis`, 
                 `movies_manager_movie`.`keywords`, 
                 `movies_manager_movie`.`release_date`, 
                 `movies_manager_movie`.`rating`, 
                 `movies_manager_movie`.`poster_web_url`, 
                 `movies_manager_movie`.`has_poster`, 
                 `movies_manager_movie`.`number`, 
                 `movies_manager_movie`.`has_sources`, 
                 `movies_manager_movie`.`season_id`, 
                 `movies_manager_movie`.`created`, 
                 `movies_manager_movie`.`updated`, 
                 `movies_manager_moviecache`.`activity_name` 

FROM `movies_manager_movie` 

LEFT OUTER JOIN `movies_manager_moviecache` ON (`movies_manager_movie`.`id` = `movies_manager_moviecache`.`movie_id`) 

WHERE (`movies_manager_movie`.`has_sources` = 1  
       AND (`movies_manager_moviecache`.`team_member_id` IN (

          SELECT U0.`id` FROM `movies_manager_movieteammember` U0 
          INNER JOIN `movies_manager_movieteammemberactivity` U1 ON (U0.`id` = U1.`team_member_id`) 
          WHERE U1.`movie_id` = 3588 ) 
                AND `movies_manager_movie`.`number` IS NULL
       ) 

    AND NOT (`movies_manager_movie`.`id` = 3588 )) 
    ORDER BY `movies_manager_moviecache`.`activity_name` DESC LIMIT 3;

此查询最多可能需要3秒钟,因为我使用最新的MySQL版本,每个MyIsam表中的索引到处都不超过35行,所以我非常惊讶。

我尽可能地缓存了所有东西,但我至少每天运行20000次,大约等待加载16小时。而且我非常确定我的用户(也不是Google Bot)都不会感谢每个页面加载的4个等待时间。

我该怎么做才能让它更快?

我考虑过从电影复制到moviecache的字段,因为电影缓存的所有目的都是将规范化为复杂连接。

我尝试将子查询内联到ID列表,但令人惊讶地将查询时间加倍。

表:

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment | 
| title          | varchar(120) | NO   | UNI | NULL    |                | 
| original_title | varchar(120) | YES  |     | NULL    |                | 
| synopsis       | longtext     | YES  |     | NULL    |                | 
| keywords       | varchar(120) | YES  |     | NULL    |                | 
| release_date   | date         | YES  |     | NULL    |                | 
| rating         | int(11)      | NO   |     | NULL    |                | 
| poster_web_url | varchar(255) | YES  |     | NULL    |                | 
| has_poster     | tinyint(1)   | NO   |     | NULL    |                | 
| number         | int(11)      | YES  |     | NULL    |                | 
| season_id      | int(11)      | YES  | MUL | NULL    |                | 
| created        | datetime     | NO   |     | NULL    |                | 
| updated        | datetime     | NO   |     | NULL    |                | 
| has_sources    | tinyint(1)   | NO   |     | NULL    |                | 
+----------------+--------------+------+-----+---------+----------------+
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| id                  | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name                | varchar(120) | NO   | UNI | NULL    |                | 
| biography           | longtext     | YES  |     | NULL    |                | 
| birth_date          | date         | YES  |     | NULL    |                | 
| picture_web_url     | varchar(255) | YES  |     | NULL    |                | 
| allocine_link       | varchar(255) | YES  |     | NULL    |                | 
| created             | datetime     | NO   |     | NULL    |                | 
| updated             | datetime     | NO   |     | NULL    |                | 
| has_picture         | tinyint(1)   | NO   |     | NULL    |                | 
| biography_linkyfied | longtext     | YES  |     | NULL    |                | 
+---------------------+--------------+------+-----+---------+----------------+

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment | 
| movie_id       | int(11)      | NO   | MUL | NULL    |                | 
| tag_slug       | varchar(100) | YES  | MUL | NULL    |                | 
| team_member_id | int(11)      | YES  | MUL | NULL    |                | 
| cast_rank      | int(11)      | YES  |     | NULL    |                | 
| activity_name  | varchar(30)  | YES  | MUL | NULL    |                | 
+----------------+--------------+------+-----+---------+----------------+

Mysql告诉我这是一个缓慢的查询:

# Query_time: 3  Lock_time: 0  Rows_sent: 9  Rows_examined: 454128

2 个答案:

答案 0 :(得分:1)

将movies_manager_movieteammemberactivity和movies_manager_movieteammember移动到您的主要连接语句中(这样您就可以在movies_manager_movie和其他3个表的内部联接产品之间执行左外部操作)。这可以大大加快您的查询速度。

答案 1 :(得分:1)

试试这个:

SELECT `movies_manager_movie`.`id`, 
    `movies_manager_movie`.`title`, 
    `movies_manager_movie`.`original_title`, 
    `movies_manager_movie`.`synopsis`, 
    `movies_manager_movie`.`keywords`, 
    `movies_manager_movie`.`release_date`, 
    `movies_manager_movie`.`rating`, 
    `movies_manager_movie`.`poster_web_url`, 
    `movies_manager_movie`.`has_poster`, 
    `movies_manager_movie`.`number`, 
    `movies_manager_movie`.`has_sources`, 
    `movies_manager_movie`.`season_id`, 
    `movies_manager_movie`.`created`, 
    `movies_manager_movie`.`updated`, 
    (
        SELECT `movies_manager_moviecache`.`activity_name` 
        FROM `movies_manager_moviecache` 
        WHERE (`movies_manager_movie`.`id` = `movies_manager_moviecache`.`movie_id` 
            AND (`movies_manager_moviecache`.`team_member_id` IN (
                SELECT U0.`id` FROM `movies_manager_movieteammember` U0 
                    INNER JOIN `movies_manager_movieteammemberactivity` U1 ON (U0.`id` = U1.`team_member_id`) 
                WHERE U1.`movie_id` = 3588 ) 
            AND `movies_manager_movie`.`number` IS NULL
        ) ) LIMIT 1) AS `activity_name` 
FROM `movies_manager_movie`     
WHERE (`movies_manager_movie`.`has_sources` = 1  
    AND NOT (`movies_manager_movie`.`id` = 3588 )) 
ORDER BY `activity_name`  DESC 
LIMIT 3;

让我知道它是如何执行的