A UNION allows you to use two separate indexes and can speed up queries.
A UNION allows you to use two separate indexes and can speed up queries.
I've used that OR > UNION ALL trick a number of times to vastly improve performance on specific queries. It's crazy how much of an effect it can have.
I wish Postgres would implement a planner optimization to automatically run queries with an OR more efficiently (e.g. use the same plan as with a UNION/ALL where possible).
Found it: https://www.postgresql.org/message-id/flat/7f70bd5a-5d16-e05...
I just did some more digging, and it looks like there has been some more recent discussion in a different thread about the same topic though: https://www.postgresql.org/message-id/flat/567ED6CA.2040504%...
You can really do some complicated pipelining of data cleaning and filtering this way, much faster than resorting to a whole bunch of complicated inline SQL work in the clauses.
[edit] I should add, learn how to read an explain plan, this is vital for understanding and improving your queries.
There have been more recent attempts to improve OR clause performance in another thread: https://www.postgresql.org/message-id/flat/567ED6CA.2040504%...
Of course UNION [ALL] does often yield better performance, yes.