postgresql 查看索引是否有效及索引使用情况

查看索引是否有效

1.未使用索引前

jinbo=# explain analyze select * from jinbo.student where score < 80 and score < 90 and sex = 1;
                                             QUERY PLAN                                             
----------------------------------------------------------------------------------------------------
 Seq Scan on student  (cost=0.00..220.45 rows=4 width=23) (actual time=0.030..2.195 rows=1 loops=1)
   Filter: ((score < 80) AND (score < 90) AND (sex = 1))
   Rows Removed by Filter: 11453
 Planning time: 0.119 ms
 Execution time: 2.217 ms
(5 rows)

使用索引后

jinbo=# create index on jinbo.student(score, sex);
CREATE INDEX
jinbo=# explain analyze select * from jinbo.student where score < 80 and score < 90 and sex = 1;
                                                          QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on student  (cost=4.40..10.54 rows=2 width=23) (actual time=0.063..0.064 rows=1 loops=1)
   Recheck Cond: ((score < 80) AND (score < 90) AND (sex = 1))
   Heap Blocks: exact=1
   ->  Bitmap Index Scan on student_score_sex_idx  (cost=0.00..4.40 rows=2 width=0) (actual time=0.058..0.058 rows=1 loops=1)
         Index Cond: ((score < 80) AND (score < 90) AND (sex = 1))
 Planning time: 0.398 ms
 Execution time: 0.093 ms
(7 rows)

查看索引使用情况

查看索引使用的统计情况的表:

pg_stat_user_indexes

jinbo=# \d+ pg_stat_user_indexes;
                     View "pg_catalog.pg_stat_user_indexes"
    Column     |  Type  | Collation | Nullable | Default | Storage | Description 
---------------+--------+-----------+----------+---------+---------+-------------
 relid         | oid    |           |          |         | plain   | 
 indexrelid    | oid    |           |          |         | plain   | 
 schemaname    | name   |           |          |         | plain   | 
 relname       | name   |           |          |         | plain   | 
 indexrelname  | name   |           |          |         | plain   | 
 idx_scan      | bigint |           |          |         | plain   | 
 idx_tup_read  | bigint |           |          |         | plain   | 
 idx_tup_fetch | bigint |           |          |         | plain   | 
View definition:
 SELECT pg_stat_all_indexes.relid,
    pg_stat_all_indexes.indexrelid,
    pg_stat_all_indexes.schemaname,
    pg_stat_all_indexes.relname,
    pg_stat_all_indexes.indexrelname,
    pg_stat_all_indexes.idx_scan,
    pg_stat_all_indexes.idx_tup_read,
    pg_stat_all_indexes.idx_tup_fetch
   FROM pg_stat_all_indexes
  WHERE (pg_stat_all_indexes.schemaname <> ALL (ARRAY['pg_catalog'::name, 'information_schema'::name])) AND pg_stat_all_indexes.schemaname !~ '^pg_toast'::text;

pg_stat_all_indexes

jinbo=# \d+ pg_stat_all_indexes 
                      View "pg_catalog.pg_stat_all_indexes"
    Column     |  Type  | Collation | Nullable | Default | Storage | Description 
---------------+--------+-----------+----------+---------+---------+-------------
 relid         | oid    |           |          |         | plain   | 
 indexrelid    | oid    |           |          |         | plain   | 
 schemaname    | name   |           |          |         | plain   | 
 relname       | name   |           |          |         | plain   | 
 indexrelname  | name   |           |          |         | plain   | 
 idx_scan      | bigint |           |          |         | plain   | 
 idx_tup_read  | bigint |           |          |         | plain   | 
 idx_tup_fetch | bigint |           |          |         | plain   | 
View definition:
 SELECT c.oid AS relid,
    i.oid AS indexrelid,
    n.nspname AS schemaname,
    c.relname,
    i.relname AS indexrelname,
    pg_stat_get_numscans(i.oid) AS idx_scan,
    pg_stat_get_tuples_returned(i.oid) AS idx_tup_read,
    pg_stat_get_tuples_fetched(i.oid) AS idx_tup_fetch
   FROM pg_class c
     JOIN pg_index x ON c.oid = x.indrelid
     JOIN pg_class i ON i.oid = x.indexrelid
     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
  WHERE c.relkind = ANY (ARRAY['r'::"char", 't'::"char", 'm'::"char"]);

查看使用记录

    jinbo=# select * from pg_stat_all_indexes where schemaname = 'jinbo' and relname = 'student';
     relid  | indexrelid | schemaname | relname |     indexrelname      | idx_scan | idx_tup_read | idx_tup_fetch 
    --------+------------+------------+---------+-----------------------+----------+--------------+---------------
     111451 |     228633 | jinbo      | student | student_score_sex_idx |        3 |            3 |             2
     
//---------------------------------------------------分割线---------------------------------------------

jinbo=# select * from pg_stat_user_indexes where schemaname = 'jinbo' and relname = 'student';
 relid  | indexrelid | schemaname | relname |     indexrelname      | idx_scan | idx_tup_read | idx_tup_fetch 
--------+------------+------------+---------+-----------------------+----------+--------------+---------------
 111451 |     228633 | jinbo      | student | student_score_sex_idx |        3 |            3 |             2
(1 row)
    原文作者:majinbo111
    原文地址: https://blog.csdn.net/u011944141/article/details/98056440
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞