PostgreSQL中COUNT的各条件下(1亿条数据)例子

test=# insert into tbl_time1 select generate_series(1,100000000),clock_timestamp(),now();
INSERT 0 100000000
Time : 525833.218 ms
约:8.7分钟
 

COUNT,没有索引,1亿条数据。

 

test=# select count (1) from tbl_time1;
    count  
-----------
  100000000
(1 row)
Time : 3070658.058 ms
约:51.2分钟
 

添加主键索引耗时

test=# alter table tbl_time1 add primary key (id);
ALTER TABLE
Time : 981276.804 ms
约:16.4分钟
 

COUNT,有索引(主键),1亿条数据,注意 where id > 0 的条件

这个有 where id > 0
test=#  select count (id) from tbl_time1 where id > 0;
    count  
-----------
  100000000
(1 row)
Time : 244243.112 ms
约:4.071分钟

COUNT,有索引(主键),1亿条数据,注意没有 where id > 0 的条件

这个无 where id > 0
test=#  select count (id) from tbl_time1;
    count  
-----------
  100000000
(1 row)
Time : 548650.606 ms
约:9.144分钟
 

通过修改配置文件调优

enable_bitmapscan = off
enable_hashagg = on
enable_hashjoin = on
enable_indexscan = on
enable_indexonlyscan = on
#enable_material = on
#enable_mergejoin = on
#enable_nestloop = on
enable_seqscan = off
#enable_sort = on
enable_tidscan = off
 
test=# select count (id) from tbl_time1 where id > 0;
    count 
-----------
100000000
(1 row)
Time : 87501.151 ms
约:1.456分钟

转载于:https://www.cnblogs.com/tantanba/p/6117830.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值