Reputation: 341
I have two queries like this:
SELECT ... FROM ... WHERE (status IN (1,2,3) OR status IN (3,4,5)) AND ... AND status IN (2,3)
and this one:
SELECT ... FROM ... WHERE ... AND status IN (2,3)
These queries are executed in almost same time that makes me think that SQL engine optimizes first query and makes somthing like in second example.
Am I right or wrong? Is there any profit to optimize first query manually to the second one, or I can rely on SQL engine?
Upvotes: 1
Views: 54
Reputation: 108766
PostgreSQL probably does the right thing here as long as the items in your IN()
clauses are constants.
To verify that, you can run both queries prefixed with EXPLAIN ANALYZE and examine the execution plans. Completely understanding execution plans takes some work. But PostgreSQL probably reduces all those IN()
clauses to a single lookup predicate.
Upvotes: 2