この種の質問が以前に尋ねられたことは知っていますが、どれも私を解決に導きませんでした。
これが私のクエリです:
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 枚のユニークな写真について最新のコメントを取得しようとしています。たとえば、1 枚の写真に 5 つのコメントがある場合、最後の 1 枚だけを表示します。
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 | |
+--------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
何か案は?ありがとう!