I run a social network. Most of our features follow a similar pattern: a user -> their friends -> something the friends did ordered by timestamp and limited.
An example query:
SELECT pictures.* FROM pictures
INNER JOIN friends ON friends.friend_id = pictures.user_id
WHERE friends.user_id = ?
ORDER BY pictures.created_at DESC
LIMIT 20
Obviously, this sort of query is always going to filesort (and possibly create a temp table), no matter how it's indexed (provided there's more than one expected value for friends.user_id).
I'm no expert in MySQL performance optimization. But, I have read the book and followed mysqlperformanceblog and all that for years. Yet, I find myself stumped on optimizing a query like this.
Any pointers would be great. Thanks in advance.
An example query:
SELECT pictures.* FROM pictures
INNER JOIN friends ON friends.friend_id = pictures.user_id
WHERE friends.user_id = ?
ORDER BY pictures.created_at DESC
LIMIT 20
Obviously, this sort of query is always going to filesort (and possibly create a temp table), no matter how it's indexed (provided there's more than one expected value for friends.user_id).
I'm no expert in MySQL performance optimization. But, I have read the book and followed mysqlperformanceblog and all that for years. Yet, I find myself stumped on optimizing a query like this.
Any pointers would be great. Thanks in advance.
Comment