设为首页 收藏本站
查看: 1129|回复: 0

[经验分享] 我对PostgreSQL 中 index scan 与 seq scan 的对比学习

[复制链接]

尚未签到

发表于 2016-11-22 00:31:20 | 显示全部楼层 |阅读模式
  开始
  数据量很小的时候,我们可以看到,seq scan 比 index scan 更加有效。那是因为 index scan 至少要发生两次I/O,一次是 读取索引块, 一次是读取数据块。当index 很大的时候,情况可能会更加复杂。



postgres=# select a.relpages, a.reltuples, a.relfilenode,a.reltype,b.typname from pg_class a, pg_type b where a.relname like 'gaotab%' and a.reltype=b.oid;   
relpages | reltuples | relfilenode | reltype | typname     
----------+-----------+-------------+---------+---------   
1 |      100 |       16396 |   16386 | gaotab   
  数据量为 100条记录。
  预估成本:



postgres=# set session enable_seqscan=false;                    
SET                    
postgres=# explain select name from gaotab where id=50;                    
QUERY PLAN                                                  
---------------------------------------------------------------------                    
Index Scan using idx_id on gaotab  (cost=0.00..8.27 rows=1 width=5)                    
Index Cond: (id = 50)                    
(2 rows)                    


postgres=# set session enable_seqscan=true;        
SET        
postgres=# explain select name from gaotab where id=50;        
QUERY PLAN                              
------------------------------------------------------        
Seq Scan on gaotab  (cost=0.00..2.25 rows=1 width=5)        
Filter: (id = 50)        
(2 rows)        
  实际执行:



postgres=# set session enable_seqscan=false;        
SET        
postgres=# explain analyze select name from gaotab where id=50;        
QUERY PLAN                           
--------------------------------------------------------------------------------        
-------------------------------        
Index Scan using idx_id on gaotab  (cost=0.00..8.27 rows=1 width=5) (actual tim        
e=0.112..0.113 rows=1 loops=1)        
Index Cond: (id = 50)        
Total runtime: 0.133 ms        
(3 rows)        


postgres=# set session enable_seqscan=true;        
SET        
postgres=# explain analyze select name from gaotab where id=50;        
QUERY PLAN                                   
--------------------------------------------------------------------------------        
----------------        
Seq Scan on gaotab  (cost=0.00..2.25 rows=1 width=5) (actual time=0.014..0.018         
rows=1 loops=1)        
Filter: (id = 50)        
Rows Removed by Filter: 99        
Total runtime: 0.034 ms        
(4 rows)        
  等到数据量大的时候,就是截然不同了。
  数据为1000条记录时,通过查询可以看到,已经跨越了7个page:



postgres=# analyze;   
ANALYZE   
postgres=# select a.relpages, a.reltuples, a.relfilenode,a.reltype,b.typname from pg_class a, pg_type b where a.relname like 'gaotab%' and a.reltype=b.oid;   
relpages | reltuples | relfilenode | reltype | typname     
----------+-----------+-------------+---------+---------   
7 |      1000 |       16396 |   16386 | gaotab   
(1 row)   
postgres=#     
  再次预估成本,此时seq scan 已经开始变得不划算了:



postgres=# set session enable_seqscan=false;
SET
postgres=# explain select name from gaotab where id=50;
QUERY PLAN                              
---------------------------------------------------------------------
Index Scan using idx_id on gaotab  (cost=0.00..8.27 rows=1 width=6)
Index Cond: (id = 50)
(2 rows)
postgres=# set session enable_seqscan=true;
SET
postgres=# explain select name from gaotab where id=50;
QUERY PLAN                              
---------------------------------------------------------------------
Index Scan using idx_id on gaotab  (cost=0.00..8.27 rows=1 width=6)
Index Cond: (id = 50)
(2 rows)
postgres=# set session enable_indexscan=false;
SET
postgres=# explain select name from gaotab where id=50;
QUERY PLAN                              
---------------------------------------------------------------------
Bitmap Heap Scan on gaotab  (cost=4.26..8.27 rows=1 width=6)
Recheck Cond: (id = 50)
->  Bitmap Index Scan on idx_id  (cost=0.00..4.26 rows=1 width=0)
Index Cond: (id = 50)
(4 rows)
postgres=# set session enable_bitmapscan=false;
SET
postgres=# explain select name from gaotab where id=50;
QUERY PLAN                       
-------------------------------------------------------
Seq Scan on gaotab  (cost=0.00..19.50 rows=1 width=6)
Filter: (id = 50)
(2 rows)
postgres=#
  实际执行



postgres=# set session enable_seqscan=false;
SET
postgres=# explain analyze select name from gaotab where id=50;
QUERY PLAN                                      
------------------------------------------------------------------------------------------------------------
-----------------------
Index Scan using idx_id on gaotab  (cost=10000000000.00..10000000008.27 rows=1 width=6) (actual time=0.020.
.0.022 rows=1 loops=1)
Index Cond: (id = 50)
Total runtime: 0.051 ms
(3 rows)
postgres=# set session enable_seqscan=true;
SET
postgres=# set session enable_indexscan=false;
SET
postgres=# set session enable_bitmapscan=false;
SET
postgres=# explain analyze select name from gaotab where id=50;
QUERY PLAN                                            
-------------------------------------------------------------------------------------------------
Seq Scan on gaotab  (cost=0.00..19.50 rows=1 width=6) (actual time=0.015..0.095 rows=1 loops=1)
Filter: (id = 50)
Rows Removed by Filter: 999
Total runtime: 0.109 ms
(4 rows)
postgres=#
  [作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]
  结束

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303489-1-1.html 上篇帖子: pg_stat_activity存储postgresql当前连接个数 下篇帖子: postgresql pitr 热备 note(待整理)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表