PostgreSQL index monitor——监控表上索引使用频率

249 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路


我们需要去监控哪些大表的查询并没有去使用索引:

SELECT relname,
   CASE idx_scan
     WHEN 0 THEN 'Insufficient data'
     ELSE (100 * idx_scan / (seq_scan + idx_scan))::text
   END percent_of_times_index_used,
   n_live_tup rows_in_table
 FROM
   pg_stat_user_tables
 ORDER BY
   n_live_tup DESC;
        relname        | percent_of_times_index_used | rows_in_table
-----------------------+-----------------------------+---------------
 account               | 11                          |          5409
 deploy                | 69                          |         58276
 app                   | 93                          |          5345
 team                  | 98                          |          1801
 firewall_rule         | 9                           |           984
 ...

percent_of_times_index_used表示表上使用索引查询的频率,建议对于大表这个值要在90%以上。

PostgreSQL index monitor——监控未使用和重复索引
PostgreSQL index monitor——监控哪些表需要创建索引