A simple but non-obvious one-line change (ANY(ARRAY[…]) to ANY(VALUES(…))) in a (bad) PostgreSQL 9.0 query cuts query time from 20s to 0.2s. Starting with low-level metrics we make our way to your best friend: EXPLAIN ANALYZE. The amount of time invested will pay off a hundred times over. The Postgres community is your second best friend.
Related Articles:
https://www.datadoghq.com/blog/100x-faster-postgres-performance-by-changing-1-line/