Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Last active January 1, 2016 21:48
Show Gist options
  • Select an option

  • Save dpaluy/216e5593fc48036f805d to your computer and use it in GitHub Desktop.

Select an option

Save dpaluy/216e5593fc48036f805d to your computer and use it in GitHub Desktop.
pg_stat_statements for where to index

Once enabled, it automatically records all queries run against your database and records often and how long they took.

Enable: create extension pg_stat_statements

Note: it has a performance cost to leaving this always on, but it’s pretty small.

SELECT 
  (total_time / 1000 / 60) as total_minutes, 
  (total_time/calls) as average_time, 
  query 
FROM pg_stat_statements 
ORDER BY 1 DESC 
LIMIT 100;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment