Suppose I have query for fetching the latest 10 books for a given author like this:
SELECT *
FROM books
WHERE author_id = @author_id
ORDER BY published DESC, id
LIMIT 10
Now if I have a list of n
authors I want to get the latest books for, then I can run this query n
times. Note that n
is reasonably small. However, this seems like an optimization opportunity.
Is there are single query that can efficiently fetch the latest 10 books for n
given authors?
This query doesn't work (only fetches 10, not n * 10
books):
SELECT *
FROM books
WHERE author_id = ANY(@author_ids)
ORDER BY published DESC, id
LIMIT 10