Postgresql 性能 - 索引页点击率

Mat*_*irl 1 postgresql performance database-tuning database-indexes

我运行以下查询来估计从内存读取的索引页(缓冲区命中)与从磁盘读取的索引页的比率

select
    t.schemaname, 
    t.relname as "Table Name", 
    io_i.indexrelname as "Index Name", 
    case when (io_i.idx_blks_hit <> 0 or io_i.idx_blks_read <> 0) then 
    round(io_i.idx_blks_hit/(io_i.idx_blks_hit::numeric + 
    io_i.idx_blks_read::numeric), 4) else null end as "Index Hit Ratio" 
from 
    pg_stat_user_tables t
    join pg_statio_user_indexes io_i on io_i.relid = t.relid 
order by "Index Hit Ratio" desc;
Run Code Online (Sandbox Code Playgroud)

我有几个指数,这个比率太低(低于 0.7)。请告知可能的原因以及如何改进。

Lau*_*lbe 5

shared_buffers不够大,无法包含所有索引,因此查询会命中磁盘(或文件系统缓存,这是可以的)。

这些索引可能不经常使用,因此 PostgreSQL 不缓存它们是有道理的。检查idx_scan视图pg_stat_user_indexes以查看自上次统计重置以来扫描索引的频率。

如果您可以设置shared_buffers足够高以包含整个数据库,请这样做。否则,请阅读手册,其中有一些设置建议shared_buffers

除此之外,只要性能良好并且I/O系统不过载,我就不会做任何事情。如果遇到问题,请尝试获取更多 RAM。如果该选项已用尽,请获得更快的存储。