使用ORDER BY和多个连接进行MySQL查询优化

时间:2015-05-06 08:05:16

标签: mysql sql

我有以下表格(所有InnoDb):

 CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`num` INT(11) NOT NULL,
`date` DATETIME NOT NULL DEFAULT '2014-01-01 00:00:00',
`year` VARCHAR(4) NOT NULL DEFAULT '1999',
PRIMARY KEY (`id`),
INDEX `yearIDX` (`year`, `num`),
INDEX `date` (`year`, `date`),
INDEX `dateonly` (`date`),
)


CREATE TABLE `log` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`unit_id` BIGINT(20) NULL DEFAULT NULL
PRIMARY KEY (`id`),
INDEX `logunitIDX` (`unit_id`),
CONSTRAINT `logunitIDX` FOREIGN KEY (`unit_id`) REFERENCES `unit` (`id`)
)

CREATE TABLE `user_log` (
`user_id` BIGINT(20) NOT NULL,
`log_id` BIGINT(20) NOT NULL,
`user_log_idx` INT(11) NOT NULL,
PRIMARY KEY (`user_id`, `user_log_idx`),
INDEX `user_log_key` (`user_id`),
INDEX `user_log` (`log_id`),
INDEX `Index 4` (`user_id`, `log_id`, `user_log_idx`),
CONSTRAINT `user_log` FOREIGN KEY (`log_id`) REFERENCES `log` (`id`),
CONSTRAINT `user_log_key` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
)

CREATE TABLE `unit` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`code` SMALLINT(6) NOT NULL
PRIMARY KEY (`id`),
UNIQUE INDEX `codeIDX_U` (`code`)
)

以下查询

SELECT 
    user0_.id AS e1_26_, 
    user0_.`num` AS registry4_26_, 
    user0_.`date` AS date5_26_
FROM `user` user0_
INNER JOIN `user_log` userlog1 
ON  user0_.id=userlog1.`user_id`
INNER JOIN `log` userlo2 
ON userlog1.`log_id`=userlo2.id
INNER JOIN `unit` unit3_ 
ON userlo2.`unit_id`=unit3_.id
WHERE 
user0_.`year`='2014' 
AND 
(unit3_.`code` in ('41','42','43','45'))
ORDER BY user0_.`date` DESC
LIMIT 400

查询分析器显示了这一点(为简洁起见,我省略了select_type和key_len。select_type是SIMPLE)

id table      type   possible_keys key       ref        rows Extra
1  unit3_     range  PRIMARY,
                     codeIDX_U     codeIDX_U  Null       10    Using where; 
                                                               Using index; 
                                                               Using temporary;
                                                               Using filesort
1  userlo2    ref    PRIMARY,
                     logunitIDX,
                     Index 6       logunitIDX unit3_.id  1194   Using where; 
                                                               Using index
1  userlog1   ref    PRIMARY,
                     user_log_key,
                     user_log,
                     Index 4        user_log   userlo2.id 1     Using index

1  user0_   eq_ref  PRIMARY,
                    yearIDX,
                    date            PRIMARY    userlog1.user_id 1   Using where

我相信我得到一个"使用临时的原因;使用filesort"在第一行是因为"我加入很多 表和ORDER BY中的列#34;不是从第一个非常量表中检索行"

有没有人知道这个查询是否可以优化为不使用temp / filesort?

1 个答案:

答案 0 :(得分:1)

您选择的所有内容均来自user0_,因此您似乎正在使用join进行过滤。假设您不关心多个行,则可以使用exists重写查询。这可能允许优化器去除文件排序:

SELECT u0.id AS e1_26_, u0.`num` AS registry4_26_, u0.`date` AS date5_26_
FROM `user` u0
WHERE EXISTS (SELECT 1
              FROM `user_log` userlog1 INNER JOIN
                   `log` userlo2 
                   ON userlog1.`log_id`= userlo2.id INNER JOIN
                   `unit` unit3_ 
                   ON userlo2.`unit_id`= unit3_.id
                   WHERE u0.id = userlog1.`user_id` AND
                         unit3_.`code` in ('41', '42', '43', '45')
             ) AND 
      u0.`year` = '2014' 
ORDER BY u0.`date` DESC
LIMIT 400;