I use a query that usually runs in a second, but sometimes takes 10 to 40 seconds to complete. I actually don’t quite understand how the subquery works, I just know that it works because it gives me 15 lines for each faverprofileid file.
I register slow queries and tell me that 5823244 rows were checked, which is odd, because in any of the tables there is nowhere near many rows (the favorites table has the largest number of 50,000 rows).
Can someone suggest me some pointers? Is this a subquery problem and need to use filesort?
EDIT: Following the explanation shows that the users table does not use an index (even if the identifier is the primary key). The section "Advanced" says: the use of temporary; Using filesort.
SELECT F.id,F.created,U.username,U.fullname,U.id,I.* FROM favorites AS F INNER JOIN users AS U ON F.faver_profile_id = U.id INNER JOIN items AS I ON F.notice_id = I.id WHERE faver_profile_id IN (360,379,95,315,278,1) AND F.removed = 0 AND I.removed = 0 AND F.collection_id is null AND I.nudity = 0 AND (SELECT COUNT(*) FROM favorites WHERE faver_profile_id = F.faver_profile_id AND created > F.created AND removed = 0 AND collection_id is null) < 15 ORDER BY F.faver_profile_id, F.created DESC;
source share