MySQL Order By 减慢连接速度

Posted

技术标签:

【中文标题】MySQL Order By 减慢连接速度【英文标题】:MySQL Order By Slows down Joins 【发布时间】:2012-08-17 16:24:08 【问题描述】:

is 查询有问题

SELECT *
FROM files f
LEFT JOIN xfiles x ON x.hash = f.bhash
LEFT JOIN cate c ON c.id = f.cate
LEFT JOIN users u ON u.id = f.user
LEFT JOIN userslev ul ON u.id_lev = ul.id
LEFT JOIN (

SELECT DISTINCT x.hash, s.iis
FROM anno x
JOIN seb s ON s.iis = x.icc
WHERE x.le =0
AND (
x.eve =0
OR x.eve =2
)
GROUP BY  `x`.`hash`
) AS tabel2 ON tabel2.hash = f.bhash
WHERE f.see + IFNULL( x.see, 0 ) + f.lee + IFNULL( x.lee, 0 ) >0
ORDER BY f.added DESC

显示第 0 - 29 行(总共 22,083 行,查询耗时 9.6523 秒)

如您所见,需要 9 秒!

但没有'order by'

SELECT *
FROM files f
LEFT JOIN xfiles x ON x.hash = f.bhash
LEFT JOIN cate c ON c.id = f.cate
LEFT JOIN users u ON u.id = f.user
LEFT JOIN userslev ul ON u.id_lev = ul.id
LEFT JOIN (

SELECT DISTINCT x.hash, s.iis
FROM anno x
JOIN seb s ON s.iis = x.icc
WHERE x.le =0
AND (
x.eve =0
OR x.eve =2
)
GROUP BY  `x`.`hash`
) AS tabel2 ON tabel2.hash = f.bhash
WHERE f.see + IFNULL( x.see, 0 ) + f.lee + IFNULL( x.lee, 0 ) >0

显示第 0 - 29 行(总共 22,083 行,查询耗时 0.0241 秒)

只有 0.0241 秒 :)

............

有没有办法让“订购”与美好时光?

............

索引:

Indexes: (files)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  ihash   27815   A       
 Edit    Drop   filename    BTREE   No  No  filename    27815   A       
 Edit    Drop   cate    BTREE   No  No  cate    45  A       
 Edit    Drop   user    BTREE   No  No  user    1264    A       
 Edit    Drop   bhash   BTREE   No  No  bhash (20)  27815   A       
 Edit    Drop   added   BTREE   No  No  added   27815   A       
 Edit    Drop   filename_2  FULLTEXT    No  No  filename    1           



Indexes: (xfiles)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  id  8196    A       
 Edit    Drop   hash    BTREE   Yes No  hash    8196    A       
 Edit    Drop   see BTREE   No  No  see 37  A       
 Edit    Drop   leec    BTREE   No  No  lee25   A   


Indexes: (cate)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  id  51  A   


Indexes: (users)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  id  39776   A       
 Edit    Drop   username    BTREE   Yes No  username    39776   A       
 Edit    Drop   id_lev  BTREE   No  No  id_lev  19  A       


Indexes: (userslev)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   base    BTREE   Yes No  id  22  A       


Indexes: (anno)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  id  23636   A       
 Edit    Drop   icc BTREE   No  No  icc 2626    A       
 Edit    Drop   eve BTREE   No  No  eve 8   A       
 Edit    Drop   hash    BTREE   No  No  hash    5909    A       
 Edit    Drop   le  BTREE   No  No  le  875 A   


Indexes: (seb)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
 Edit    Drop   PRIMARY BTREE   Yes No  id  58  A       
 Edit    Drop   iis BTREE   Yes No  iis 58  A   YES 

解释:

id          select_type     table       type            possible_keys       key         key_len         ref         rows    Extra
1               PRIMARY     f           ALL             NULL                NULL        NULL            NULL        27819   Using temporary; Using filesort
1               PRIMARY     x           eq_ref          hash                hash        20              f.bhash     1       Using where
1               PRIMARY     c           eq_ref          PRIMARY             PRIMARY     4               f.cate      1   
1               PRIMARY     u           eq_ref          PRIMARY             PRIMARY     4               f.user      1   
1               PRIMARY     ul          eq_ref          base                base        4               u.id_lev    1   
1               PRIMARY     <derived2>  ALL             NULL                NULL        NULL            NULL        1176    
2               DERIVED     s           index           iis                 iis         5               NULL        58      Using index; Using temporary; Using filesort
2               DERIVED     x           ref             icc,eve,le          icc         4               s.iis       9       Using where

【问题讨论】:

您尝试索引f.added 列吗? @raina77ow 已编入索引,但我忘记放了 也许索引按 ASC 排序,而您需要对结果进行 DESC 排序 尝试用WHERE (f.see&gt;0 OR x.see&gt;0 OR f.lee&gt;0 OR x.lee&gt;0)替换WHERE f.see + IFNULL( x.see, 0 ) + f.lee + IFNULL( x.lee, 0 ) &gt;0 @Giuseppe 尝试了 ASC,没有变化 【参考方案1】:

这里有很多关于优化订单的信息: http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

您能否发布这两个解释,以便我们了解查询计划的不同之处?

【讨论】:

以上是关于MySQL Order By 减慢连接速度的主要内容,如果未能解决你的问题,请参考以下文章

当我 ORDER BY 计算列时,查询会显着减慢 - 可以加快速度吗?

为啥没有聚合的结束 Group By 会减慢我的查询速度?

连接表中的 mySQL ORDER BY

MySQL ORDER BY 跨多个连接表的列

为啥需要“ORDER BY”才能从 MySQL 连接中获得正确的结果?

MySQL实验 内连接优化order by+limit 以及添加索引再次改进