简体   繁体   English

使用ORDER BY和多个联接的MySQL查询优化

[英]MySQL Query optimization with ORDER BY and multiple joins

I have the following tables (All InnoDb): 我有以下表格(所有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`)
)

And the following query 和以下查询

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

The query analyser shows this (for brevity I omit select_type and key_len. select_type is SIMPLE) 查询分析器将显示此内容(为简便起见,我省略了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

I believe that the reason that I get a "Using temporary; Using filesort" in the first line is because "I am joining many tables and the columns in the ORDER BY" are not from the first non-constant tables to retrieve rows" 我相信第一行中出现“使用临时;使用文件排序”的原因是因为“我正在联接许多表,而ORDER BY中的列不是从第一个非常数表中检索行”

Does anyone have any idea if this query can be optimised to not use temp/filesort? 是否有人可以优化此查询以不使用temp / filesort?

Everything you are selecting is from user0_ , so you appear to be using the join s for filtering. 您选择的所有内容都来自user0_ ,因此您似乎正在使用join进行过滤。 Assuming that you do not care about multiple rows, you can rewrite the query using exists . 假设您不关心行,则可以使用exists重写查询。 This might allow the optimizer to get rid of the file sort: 这可能使优化器摆脱文件排序:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM