mysql - 使用 JOIN 查询花费太多时间

标签 mysql performance join

我需要一些帮助来提高以下查询性能

SELECT * 
FROM   dle_pause 
       LEFT JOIN dle_post_plus 
              ON ( dle_pause.pause_postid = dle_post_plus.puuid ) 
       LEFT JOIN dle_post 
              ON ( dle_post_plus.news_id = dle_post.id ) 
       LEFT JOIN dle_playerfiles 
              ON ( dle_post.id = dle_playerfiles.post_id ) 
WHERE  pause_user = '2'; 

它需要集合中的3行(0.35秒)问题出在第三个连接上。其中一行没有 dle_post.id = dle_playerfiles.post_id,因此它会扫描整个表格。

看起来我已经拥有了所有需要的索引

+----+-------------+-----------------+--------+----------------------------------+---------+---------+-----------------------------------+--------+------------------------------------------------+
| id | select_type | table           | type   | possible_keys                    | key     | key_len | ref                               | rows   | Extra                                          |
+----+-------------+-----------------+--------+----------------------------------+---------+---------+-----------------------------------+--------+------------------------------------------------+
|  1 | SIMPLE      | dle_pause       | ALL    | pause_user                       | NULL    | NULL    | NULL                              |      3 | Using where                                    |
|  1 | SIMPLE      | dle_post_plus   | ref    | puuid                            | puuid   | 36      | func                              |      1 | Using where                                    |
|  1 | SIMPLE      | dle_post        | eq_ref | PRIMARY                          | PRIMARY | 4       | online_test.dle_post_plus.news_id |      1 | NULL                                           |
|  1 | SIMPLE      | dle_playerFiles | ALL    | ix_dle_playerFiles__post_id_type | NULL    | NULL    | NULL                              | 131454 | Range checked for each record (index map: 0x2) |
+----+-------------+-----------------+--------+----------------------------------+---------+---------+-----------------------------------+--------+------------------------------------------------+

最佳答案

如果您还没有对 dle_playerfiles 的 post_id 建立索引,则对其建立索引。
如果您已经为其添加了索引,则在最后一个连接的查询中写入“使用索引”,如下所示:

 SELECT * 
  FROM 
     dle_pause 
   LEFT JOIN dle_post_plus 
          ON ( dle_pause.pause_postid = dle_post_plus.puuid ) 
   LEFT JOIN dle_post 
          ON ( dle_post_plus.news_id = dle_post.id ) 
   LEFT JOIN dle_playerfiles **use index(post_id)**
          ON ( dle_post.id = dle_playerfiles.post_id ) 
   WHERE 
     pause_user = '2';

这也将使用第四个表的索引。现在你的解释显示它没有在第四个表上使用任何索引,因此扫描了 131454 行。

关于mysql - 使用 JOIN 查询花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823192/

相关文章:

r - 如何根据其他 data.table(随机)过滤 data.table

mysql - 在 MySQL 中插入十六进制值

java - JDBC 一对多连接与多选择

android - Phonegap 低性能问题

performance - Haskell:在性能方面,函数组合 (.) 与函数应用程序 ($)?

php - Laravel Eloquent 急切加载 : Join same table twice

mysql - 优化多个自JOIN还是重新设计DB?

java - 使用 JPA 报告重复条目来保留一个新的但相同的实体

php - 有人可以推荐我可以添加到我的网站的 php mysql 评级系统吗

performance - JMeter 在非 GUI 中具有更高的响应时间