惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
小众软件
小众软件
I
InfoQ
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Martin Fowler
Martin Fowler
月光博客
月光博客
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
V
Visual Studio Blog
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
GbyAI
GbyAI
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 马建康

liunx修改字体为宋体 OpenERP|odoo Web开发 PostgreSQL源码分析之shared buffer与磁盘文件 如何更改postgresql的最大连接数 [Ubuntu]明明白白安装中文字体 Ubuntu 所需要的中文字体美化操作步骤 Ubuntu 字体设置:使用Windows 字体 Ubuntu下中文完美解决方案 fonts.conf 中文手册 PostgreSql 合并多行记录 Ubuntu 字体设置:使用Windows 字体 Ubuntu 12.04 LTS安装Windows字体 Postgresql死锁的处理 彻底解决Odoo8.0单时区应用中的时区问题 Git详解之三 Git分支 ubuntu下修改文件夹权限 Ubuntu中Nginx的安装与配置 Linux下解决 id_rsa 权限不够 Ubuntu Server 13.10 安装配置图解教程
PostgreSQL中COUNT的各条件下(1亿条数据)例子
马建康 · 2015-07-20 · via 博客园 - 马建康

插入一亿条数据

(示例数据库:9.3.5)
参考资料:http://www.oschina.net/question/96003_70381

1

2

3

4

test=# insert into tbl_time1 select generate_series(1,100000000),clock_timestamp(),now();

INSERT 0 100000000

Time: 525833.218 ms

约:8.7分钟

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

1

2

3

4

5

6

7

test=# select count(1) from tbl_time1;

   count  

 100000000

(1 row)

Time: 3070658.058 ms

约:51.2分钟

添加主键索引耗时

1

2

3

4

test=# alter table tbl_time1 add primary key (id);

ALTER TABLE

Time: 981276.804 ms

约:16.4分钟

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

1

2

3

4

5

6

7

8

这个有 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 的条件

1

2

3

4

5

6

7

8

这个无 where id > 0

test=#  select count(id) from tbl_time1;

   count  

 100000000

(1 row)

Time: 548650.606 ms

约:9.144分钟

通过修改配置文件调优postgresql.conf

1

2

3

4

5

6

7

8

9

10

11

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

1

2

3

4

5

6

7

test=# select count(id) from tbl_time1 where id > 0;

   count 

100000000

(1 row)

Time: 87501.151 ms

约:1.456分钟