我知道以前有人问过这种问题,但这些问题都没有让我找到解决方案。
这是我的查询:
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 | |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
有任何想法吗?谢谢!