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

时间:2011-06-02 16:58:07

标签: postgresql

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

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

4 个答案:

答案 0 :(得分:10)

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

-- 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

enter image description here

答案 1 :(得分:9)

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

EXPLAIN (ANALYZE ON, BUFFERS ON) SELECT ...

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

答案 2 :(得分:1)

遗憾的是,SET STATISTICS IO ON没有像PostgreSQL那样简单的事情。但是,通过pg_statio_*系统目录可以获得IO统计数据。这并不完美,因为数据没有限定在会话范围内,但如果您希望查看查询的有效性并且处于干净的房间环境中,那么它对大多数问题都能正常运行。

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

答案 3 :(得分:1)

并不是因为PostgreSQL在很大程度上依赖于OS缓存,它无法知道那里发生了什么。 pg_catalog中的pg_statio *系列视图保持运行命中和实际读取的计数,但这些读取可能已经达到操作系统缓存。