数据库
首页 > 数据库> > 带顺序的MySQL查询太慢

带顺序的MySQL查询太慢

作者:互联网

我知道以前曾问过这种问题,但是没有一个问题使我找到了解决方案.

这是我的查询:

SELECT p.photoid, c.comment, p.path, p.smallfile, p.bigfile, c.userid, c.dateposted, u.username, c.likephoto 
  FROM bs_photocomments c, photos p, user u 
  WHERE c.active = 1 
    AND u.userid = c.userid 
    AND p.photoid = c.photoid 
    AND c.id = (SELECT ID FROM bs_photocomments WHERE photoid = p.photoid ORDER BY ID DESC LIMIT 1) 
  ORDER BY c.id DESC LIMIT 2

关于最后一个AND的一些解释:

基本上,我正在尝试获取20张独特照片的最新评论.例如,如果一张照片有5条评论,我只希望显示最后一条.

ORDER BY c.id是杀手..只有18万条评论和10万张照片.如果我删除该ORDER BY,查询会立即返回,但显然不会返回我想要的结果.

以下是索引:

+------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table            | Non_unique | Key_name           | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| bs_photocomments |          0 | PRIMARY            |            1 | id          | A         |      183269 |     NULL | NULL   |      | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx1 |            1 | photoid     | A         |      183269 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx2 |            1 | active      | A         |           4 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx3 |            1 | dateposted  | A         |      183269 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx4 |            1 | userid      | A         |        4953 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx5 |            1 | puserid     | A         |        4072 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx6 |            1 | id          | A         |      183269 |     NULL | NULL   |      | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx6 |            2 | photoid     | A         |      183269 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx7 |            1 | photoid     | A         |      183269 |     NULL | NULL   | YES  | BTREE      |         |
| bs_photocomments |          1 | photocomments_idx7 |            2 | userid      | A         |      183269 |     NULL | NULL   | YES  | BTREE      |         |
+------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+


+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table  | Non_unique | Key_name     | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| photos |          0 | PRIMARY      |            1 | photoid      | A         |       57736 |     NULL | NULL   |      | BTREE      |         |
| photos |          1 | photos_idx1  |            1 | userid       | A         |        5773 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx2  |            1 | dateuploaded | A         |       57736 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx3  |            1 | camera       | A         |          17 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx4  |            1 | focallength  | A         |         308 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx5  |            1 | category     | A         |          96 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx6  |            1 | active       | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photo_idx7   |            1 | aperture     | A         |         354 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx8  |            1 | lenstype     | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx9  |            1 | film         | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |
| photos |          1 | photos_idx10 |            1 | originalfile | A         |       57736 |     NULL | NULL   | YES  | BTREE      |         |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+

有任何想法吗?谢谢!

解决方法:

此查询将返回(最多)20张不同的照片(“ by_photocomments”表上的ID值由升序标识的“ recent-cy”)的最“最近”的注释.返回的20张照片将是具有“最新”评论的照片,并应从最“最新”的评论开始排序. (注意:仅考虑bs_photocomments中具有active = 1的行,忽略bs_photocomments中的所有其他行,就好像它们甚至不在表中一样.)

SELECT p.photoid
     , c.comment
     , p.path
     , p.smallfile
     , p.bigfile
     , c.userid
     , c.dateposted
     , u.username
     , c.likephoto
  JOIN ( 
         SELECT n.photoid
              , MAX(n.id) AS max_id
           FROM bs_photocomments n
          WHERE n.active = 1 
          GROUP BY n.photoid
          ORDER BY max_id DESC
          LIMIT 20
       ) m
  JOIN bs_photocomments c
    ON c.id = m.max_id
  JOIN photos p
    ON p.photoid = c.photoid
  JOIN user u
    ON u.userid = c.userid
 ORDER BY c.id DESC
 LIMIT 20

“技巧”是使用内联视图获取每个photoid的“最新”注释(内联视图的别名为m).一旦有了,只需从bs_photocomments中获取该行,以及从用户和照片中获取相关的行.

我相信原始查询中真正的性能“杀手”是联接谓词中的相关子查询.就对bs_photocomments表中的每一行执行该查询,甚至对photos表中的每一行运行该查询而言,这将是非常昂贵的.

为了获得最佳性能,您将需要“覆盖索引”,而不是每个单独列上的(无用的)索引.至少,您需要在bs_photocomments上具有以photoid作为前导索引的索引,然后是id.包括活动列将意味着可以在不访问任何数据页的情况下满足“使用索引”的内联视图查询.

... ON bs_photocomments (photoid, id, active)

使用该索引,MySQL应该能够相当有效地获取每个photoid的最大id.同样,有一个索引

... ON bs_photocomments (id, comment, userid, dateposted, likephoto)

这意味着针对bs_photocomments表(c)的外部查询也可以“使用索引”得到满足. (MySQL可能能够从索引满足ORDER BY,而无需进行排序操作.(您需要检查EXPLAIN输出,以查看“使用文件排序”是否出现在Extra列中.)

同样,这些覆盖索引可能会稍微提高查询的性能. (如果在不访问基础表中的页面的情况下通过索引满足查询,则EXPLAIN输出将显示“正在使用索引”.)

  ON photos (photoid, path, smallfile, bigfile)

  ON user (userid, username)

每个单独列上的大多数单独索引都是无用的;这些索引中的任何一个都不太可能被使用,因此它们所做的只是消耗资源.

标签:query-performance,mysql
来源: https://codeday.me/bug/20191031/1975495.html