Skip to content

PopSQL: limited support.

Migrate now →

Statements by total time

The statements that cost the most in aggregate: mean time × calls. Optimising the top three usually moves the whole server.

PostgreSQL · SQL
SELECT LEFT(query, 160) AS statement,
       calls,
       ROUND(total_exec_time::numeric, 0) AS total_ms,
       ROUND(mean_exec_time::numeric, 2) AS mean_ms,
       rows,
       ROUND(100.0 * shared_blks_hit / NULLIF(shared_blks_hit + shared_blks_read, 0), 1) AS cache_hit_pct
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20

Needs something on the server: The pg_stat_statements extension must be installed and in shared_preload_libraries.

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack — queries, dashboard and checks — at once.