zoukankan      html  css  js  c++  java
  • 对PostgreSQL中 index only scan 的初步理解

    开始

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

    例子:

    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)

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

    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=# 

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

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

    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=# 

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

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

    这样就不够准确了。

    例如:

    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=# 

    学习

  • 相关阅读:
    shell脚本简单调试
    计算机揭秘之:网络分类和性能分析
    centos 7 虚拟机忘记密码
    算法时间复杂度计算
    strace命令 linux下调试神器
    Linux下core dump (段错误)
    dmesg + addr2line查看堆栈错误位置
    镜像 开源网站
    C语言---链表(包括学习过程中的思想活动)
    The C compiler "/usr/bin/cc" is not able to compile a simple test program. 解决方法
  • 原文地址:https://www.cnblogs.com/gaojian/p/2759061.html
Copyright © 2011-2022 走看看