How to speed up group by I am trying to speed up the following query, which currently takes 14 seconds:

SELECT
  COUNT(*) AS employee_count, e.jurisdiction_id, e.year
FROM
  employee_employee e
GROUP BY
  e.jurisdiction_id, e.year
ORDER BY
  employee_count ASC;


I add an index by running:

CREATE INDEX ON employee_employee (jurisdiction_id, year);


But that did very little to improve the timing because the GROUP BY is taking the longest to execute. Is there a way to index a GROUP BY function?
Taking a simple approach:

Execute inside psql, enable \timing and run your query. Also check query execution plan:

explain select &lt;your query&gt;;


Then run vacuum for your table:

vacuum (full, analyze, verbose) employee_employee;


And check if query execution plan was improved, you'll see what's the costly stage:

explain select &lt;your query&gt;;


Considering that your query is a simple query, there's no much to do beyond that inside the database.
Optionally evaluate if a materialized view fits your need.