Making a Postgres Query 1,000 Times Faster