How to speed up group by

by caseym   Last Updated January 13, 2018 03:06 AM

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?

Tags : postgresql


Related Questions






performance issue in postgres

Updated May 30, 2017 06:06 AM