2

I have a query that includes two WHERE clauses. Looks like this:

SELECT m 
FROM Media m 
WHERE m.userid = :id 
  AND m.timestamp = (SELECT MAX(mm.timestamp) 
                     FROM Media mm 
                     WHERE mm.userid = :id 
                       AND mm.source IN :sources 
                       AND mm.timestamp < :date)

What I want to know if this query will be faster with one index or should I create two seperate indexes for each WHERE clauses? Like:

  • 1st index for 1st WHERE = (userid, timestamp)
  • 2nd index for 2nd WHERE = (userid, source, timestamp)

EDIT:

I have created 2 indexes.

  • 1 - (userid, source, timestamp)
  • 2 - (userid, timestamp)

When I analyze the query, It always showing the second index used for the query.

1 Answer 1

4

Assuming that user.id is really userid, the perfect index would be

CREATE INDEX ON media(userid, source, timestamp);

That is perfect for the inner query, and the index is also good for the outer query.

To extend on that, the above assumes that all these conditions are selective, that is, they significantly reduce the number of result rows.

In your case, it seems that the condition mm.source IN :sources is not very selective, perhaps because there are only few distinct values for the column, or because you happen to query for a value that occurs frequently.

In that case, it is better to omit the column from the index, because that will make the index smaller without much loss. All other things equal, PostgreSQL will choose to scan the smaller index.

Sign up to request clarification or add additional context in comments.

6 Comments

Why will this lead to a nested loop? The inner query does not use any outer values, and will return at most a single result.
@Bergi would you say that this is still the best option?
@gozluklu_marti Unless you have multiple media with the same timestamp, I'd probably not use a nested query at all. I would always benchmark, and recommend to have a look at the EXPLAIN ANALYZE.
@LaurenzAlbe if you read my updated question, your index is not the best one according to Postgres :)
I have added an explanation for that. The reason is the selectivity of the conditions, which I didn't know about.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.