Postgres 中的查询读取了多少磁盘页面?

Set*_*ero 3 sql postgresql optimization query-planner

我想知道运行单个 Postgres 查询时从磁盘(而不是从缓存)读取了多少页(表 + 索引,如果有)。如果有任何方法可以从 EXPLAIN ANALYZE 中提取此信息就更好了。

小智 5

当您添加选项时,该信息可用buffersexplain (analyze, buffers) select ...

例如

explain (analyze, buffers)
select *
from salaries s
  join employees e on e.emp_no = s.emp_no
where s.emp_no in ('10001', '20001', '30001', '40001', '50001', '99999', '99996');

QUERY PLAN                                                                                                                         
-----------------------------------------------------------------------------------------------------------------------------------
Nested Loop  (cost=0.85..1016.67 rows=81 width=51) (actual time=0.152..18.530 rows=96 loops=1)                                     
  Buffers: shared hit=407 read=5                                                                                                   
  I/O Timings: read=15.340                                                                                                         
  ->  Index Scan using salaries_pkey on salaries s  (cost=0.43..349.03 rows=81 width=20) (actual time=0.082..0.332 rows=96 loops=1)
        Index Cond: ((emp_no)::text = ANY ('{10001,20001,30001,40001,50001,99999,99996}'::text[]))                                 
        Buffers: shared hit=28                                                                                                     
  ->  Index Scan using employees_pkey on employees e  (cost=0.42..8.24 rows=1 width=31) (actual time=0.187..0.187 rows=1 loops=96) 
        Index Cond: ((emp_no)::text = (s.emp_no)::text)                                                                            
        Buffers: shared hit=379 read=5                                                                                             
        I/O Timings: read=15.340                                                                                                   
Planning Time: 256.640 ms                                                                                                          
Execution Time: 18.628 ms                                                                                                          
Run Code Online (Sandbox Code Playgroud)

您可以看到总共需要 412 个页面(=块)。其中 5 个必须从文件系统中获取(“read=5”)——这 5 个是因为索引扫描而需要的employees_pkey

  • 我假设“从磁盘读取(而不是从缓存)”指的是 Postgres 缓存。我没有看到问题中提到的文件系统缓存。但是不可能从执行计划中获取该信息(而且我怀疑如果不使用操作系统的一些低级调试工具就可以轻松实现) (2认同)