且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对PostgreSQL中 index only scan 的初步理解

更新时间:2022-09-13 18:52:16

开始

所谓index only scan ,就是因为 建立 index时,所包含的字段集合,囊括了我们 查询语句中的字段,这样,提取出相应的 index ,就不必再次提取数据块了。

例子:

对PostgreSQL中 index only scan 的初步理解
postgres=# \d gaotab;
           Table "public.gaotab"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | 
 name   | character varying(20) | 
 deptno | integer               | 
 age    | integer               | 

postgres=# create index idx_id_dept on gaotab(id,deptno);
CREATE INDEX
postgres=# analyze gaotab;
ANALYZE
postgres=#



postgres=# explain select id,deptno from gaotab where id=200;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Index Only Scan using idx_id_dept on gaotab  (cost=0.00..8.27 rows=1 width=8)
   Index Cond: (id = 200)
(2 rows)
对PostgreSQL中 index only scan 的初步理解

为了抵消cache 的影响,重新执行 explain analyze

对PostgreSQL中 index only scan 的初步理解
postgres=#  explain analyze select id,deptno from gaotab where id=200;
                                                        QUERY PLAN              
                                           
--------------------------------------------------------------------------------
-------------------------------------------
 Index Only Scan using idx_id_dept on gaotab  (cost=0.00..8.27 rows=1 width=8) (
actual time=30.912..30.915 rows=1 loops=1)
   Index Cond: (id = 200)
   Heap Fetches: 1
 Total runtime: 47.390 ms
(4 rows)

postgres=# 
对PostgreSQL中 index only scan 的初步理解

再看看查询中有 index 不包含的字段的情况:

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

对PostgreSQL中 index only scan 的初步理解
postgres=# explain select id,name from gaotab where id=200;
                                QUERY PLAN                                 
---------------------------------------------------------------------------
 Index Scan using idx_id_dept on gaotab  (cost=0.00..8.27 rows=1 width=10)
   Index Cond: (id = 200)
(2 rows)


postgres=# explain analyze select id,name from gaotab where id=200;
                                                      QUERY PLAN                
                                       
--------------------------------------------------------------------------------
---------------------------------------
 Index Scan using idx_id_dept on gaotab  (cost=0.00..8.27 rows=1 width=10) (actu
al time=47.043..47.044 rows=1 loops=1)
   Index Cond: (id = 200)
 Total runtime: 63.506 ms
(3 rows)

postgres=# 
对PostgreSQL中 index only scan 的初步理解

在这里,我们必须要注意的一点是:

如果是那种 带 where 条件的,如果 前面用了 explain ,后面又对同一条语句用 explain analyze 的话,就会受到缓存的影响。

这样就不够准确了。

例如:

对PostgreSQL中 index only scan 的初步理解
postgres=#  explain select id,deptno from gaotab where id=200;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Index Only Scan using idx_id_dept on gaotab  (cost=0.00..8.27 rows=1 width=8)
   Index Cond: (id = 200)
(2 rows)

postgres=#  explain analyze select id,deptno from gaotab where id=200;
                                                       QUERY PLAN               
                                         
--------------------------------------------------------------------------------
-----------------------------------------
 Index Only Scan using idx_id_dept on gaotab  (cost=0.00..8.27 rows=1 width=8) (
actual time=0.000..0.000 rows=1 loops=1)
   Index Cond: (id = 200)
   Heap Fetches: 1
 Total runtime: 0.006 ms
(4 rows)

postgres=# 
对PostgreSQL中 index only scan 的初步理解

学习






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/11/07/2759061.html,如需转载请自行联系原作者