postgresql中执行计划

distributedb=# explain select * from t_user;
                          QUERY PLAN                          
--------------------------------------------------------------
 Seq Scan on t_user  (cost=0.00..810.79 rows=23479 width=235)
seq scan表示顺序扫描t_user表,即全表扫描t_user表。0.00表示启动成本,即返回第一行需要的cost值,810返回所有的数据的成本,rows表示会返回23479行,width表示每行平均宽度235字节
cost描述一个sql的执行代价是多少默认情况下
1顺序扫描一个数据块 cost值定为1
2随机扫描一个数据库,cost值定为4
3处理一个数据行的cpu,cost定为0.01
4处理一个索引行的cpu,cost为0.005
distributedb=# explain (analyze true,buffers true) select * from t_user;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Seq Scan on t_user  (cost=0.00..810.79 rows=23479 width=235) (actual time=44.968..916.580 rows=23479 loops=1)
   Buffers: shared hit=576
 Planning time: 0.081 ms
 Execution time: 918.253 ms
在t good表上面创建索引
distributedb=# create index idx_t_goods on t_goods(id);
CREATE INDEX
distributedb=# explain select * from t_goods where id=250;
                       QUERY PLAN                        
---------------------------------------------------------
 Seq Scan on t_goods  (cost=0.00..1.18 rows=1 width=196)
   Filter: (id = 250)

distributedb=# explain select * from t_agency_role where id<5;
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on t_agency_role  (cost=0.00..1.21 rows=6 width=510)
   Filter: (id < 5)
filter代表着条件过滤  ,实验表明如果数据不多,10 行左右的话即使有索引也会走全表扫描
扫描分为seq scan(全表扫面)index scan(索引扫描)bitmap index scan(位图索引扫描)
条件过滤,一般在where上面加上过滤条件,当扫描数据行时,会找出满足过滤条件的的行,条件过滤在执行计划中显示filter,如果条件的列上面有索引,可能会走索引,不会走过滤。
还有nestloop join ,hash join , merge join
统计信息的收集
pgstat子进程是postgresql中专门的统计信息收集器进程,收集的信息主要用于查询优化时的代价,表和索引的行数和块数等统计信息记录在系统表pg_class中,其他的的统计信息主要收集在系统表pg—statistic中
postgres 17665     1  0 Jun29 ?        00:01:26 /usr/pgsql-9.4/bin/postgres
postgres 17666 17665  0 Jun29 ?        00:00:10 postgres: logger process   
postgres 17668 17665  0 Jun29 ?        00:00:12 postgres: checkpointer process  postgres 17669 17665  0 Jun29 ?        00:00:04 postgres: writer process   
postgres 17670 17665  0 Jun29 ?        00:00:16 postgres: wal writer process   
postgres 17671 17665  0 Jun29 ?        00:08:19 postgres: autovacuum launcher process   
postgres 17672 17665  0 Jun29 ?        00:00:04 postgres: archiver process   last was 0000000A00000008000000D0.00000028.ba
postgres 17673 17665  0 Jun29 ?        00:03:02 postgres: stats collector process 
在默认的postgresql的配置中 autovacuum守护进程四打开的,它能自动的分析表,在表的大部分内容运行analyze命令,准确的统计信息将帮助优化器生成最优的执行计划,从而改善查询的性能
analyze verbose tablename
analyze命令会在表上加一个读锁,对于大表analyze只读取表的部分内容做一个























来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29898569/viewspace-1735283/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29898569/viewspace-1735283/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值