Optimise main page query to remove a billion SQL queries.