Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Rajaneeshs/e5b8cd8caef6124b0a7df212031ec490 to your computer and use it in GitHub Desktop.
Save Rajaneeshs/e5b8cd8caef6124b0a7df212031ec490 to your computer and use it in GitHub Desktop.
PSQL Slowest Average Queries
# See the 10 slowest queries with over a 1000 calls
SELECT query, calls, (total_time/calls)::integer AS avg_time_ms
FROM pg_stat_statements
WHERE calls > 1000
ORDER BY avg_time_ms DESC
LIMIT 10;
# query | calls | avg_time_ms
# ----------+---------+-------------
# INSERT .. | 52323 | 12
# SELECT .. | 116948 | 10
# INSERT .. | 116948 | 4
# SELECT .. | 116948 | 4
# INSERT .. | 116948 | 3
# SELECT .. | 116948 | 1
# UPDATE .. | 116949 | 1
# SELECT .. | 116947 | 1
# DELETE .. | 116946 | 1
# SELECT .. | 1465 | 1
# (10 rows)
# See the 10 slowest SELECT queries with over a 1000 calls
SELECT query, calls, (total_time/calls)::integer AS avg_time_ms
FROM pg_stat_statements
WHERE calls > 1000
AND query iLIKE '%SELECT%'
ORDER BY avg_time_ms DESC
LIMIT 10;
# query | calls | avg_time_ms
# ----------+---------+-------------
# SELECT .. | 52323 | 12
# SELECT .. | 116948 | 10
# SELECT .. | 116948 | 4
# SELECT .. | 116948 | 4
# SELECT .. | 116948 | 3
# SELECT .. | 116948 | 1
# SELECT .. | 116949 | 1
# SELECT .. | 116947 | 1
# SELECT .. | 116946 | 1
# SELECT .. | 1465 | 1
# (10 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment