有没有办法让postgres显示查询的实际i/o

Chr*_*vey 9 postgresql

我知道使用EXPLAIN ANALYZE我可以得到预测的成本和实际执行时间(它们是不同的单位,唉!),但有没有办法让Postgres告诉我它有多少I/O(逻辑或物理)必须做什么来满足查询?

(我正在为Sybase或MS SQL Server寻找"set statistics io on"的等价物.)

Gre*_*ith 10

从PostgreSQL 9.0开始,您可以执行:

EXPLAIN (ANALYZE ON, BUFFERS ON) SELECT ...
Run Code Online (Sandbox Code Playgroud)

它将向您展示语句如何与PostgreSQL的缓存进行交互.在报告缓存未命中的情况下,这将是一个OS调用来读取内容.您不能确定它是物理I/O,因为它可能位于OS缓存中.但这可能更像是你在寻找的内容而不是试图查看pg_stat_*信息.


Chr*_*ian 10

这个答案与特定的查询语句没有直接关系,但是在搜索显示"磁盘与缓存"的方法时帮助那些在此结束的人:

-- perform a "select pg_stat_reset();" when you want to reset counter statistics
with 
all_tables as
(
SELECT  *
FROM    (
    SELECT  'all'::text as table_name, 
        sum( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk, 
        sum( (coalesce(heap_blks_hit,0)  + coalesce(idx_blks_hit,0)  + coalesce(toast_blks_hit,0)  + coalesce(tidx_blks_hit,0))  ) as from_cache    
    FROM    pg_statio_all_tables  --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
    ) a
WHERE   (from_disk + from_cache) > 0 -- discard tables without hits
),
tables as 
(
SELECT  *
FROM    (
    SELECT  relname as table_name, 
        ( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk, 
        ( (coalesce(heap_blks_hit,0)  + coalesce(idx_blks_hit,0)  + coalesce(toast_blks_hit,0)  + coalesce(tidx_blks_hit,0))  ) as from_cache    
    FROM    pg_statio_all_tables --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
    ) a
WHERE   (from_disk + from_cache) > 0 -- discard tables without hits
)
SELECT  table_name as "table name",
    from_disk as "disk hits",
    round((from_disk::numeric / (from_disk + from_cache)::numeric)*100.0,2) as "% disk hits",
    round((from_cache::numeric / (from_disk + from_cache)::numeric)*100.0,2) as "% cache hits",
    (from_disk + from_cache) as "total hits"
FROM    (SELECT * FROM all_tables UNION ALL SELECT * FROM tables) a
ORDER   BY (case when table_name = 'all' then 0 else 1 end), from_disk desc
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Sea*_*ean 5

不幸的是,没有像SET STATISTICS IO ONfor那样简单的东西PostgreSQL。但是,可以通过pg_statio_*系统目录获得 IO 统计信息。它并不完美,因为数据不限于会话,但是如果您想了解查询的效率以及在洁净室环境中的效率,它对于大多数问题都足够好。

http://www.postgresql.org/docs/current/static/monitoring-stats.html