我知道之前已经提出过这样的问题,但没有一个能让我找到解决方案。
这是我的问题:
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
是杀手。只有180k评论和100k张照片。如果我删除了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 | |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
有什么想法吗?谢谢!
答案 0 :(得分:4)
此查询将返回(最多)20张不同照片的最近“最近”评论(在bs_photocomments表中通过升序ID值识别“recent-cy”)。返回的20张照片将是那些“最近”评论的照片,并且可以从最“最近”的评论开始订购。 (注意:只考虑具有active=1
的bs_photocomments中的行,忽略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表中每一行的查询,甚至为照片表中的每一行运行它而言,这将非常昂贵。
为获得最佳性能,您需要在每个单独的列上“覆盖索引”而不是(无用)索引。至少,您需要bs_photocomments的索引,其中photoid
为主要索引,后跟id
。包括active
列将意味着内联视图查询可以满足“使用索引”,而无需访问任何数据页。
... 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
列中是否显示“Using filesort”。)
此外,这些覆盖索引可能会略微提高查询的性能。 (如果从索引中满足查询而不访问基础表中的页面,则EXPLAIN输出将显示“使用索引”。)
ON photos (photoid, path, smallfile, bigfile)
ON user (userid, username)
每个列上的大多数单独索引都是无用的;
。这些索引不太可能被使用,所以他们所做的就是消耗资源。