Cache an expensive query with a materialized view with historical data
(except for the latest two days), then union it with the live data. This
way, we get the best of both worlds: fast query times and up-to-date
data.
Working with Data Duplication
Pick the latest record per group with DISTINCT ON and ORDER BY