PostgreSQL中COUNT的各条件下(1亿条数据)例子
插入一亿条数据
(示例数据库: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 100000000Time: 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 TABLETime: 981276.804 ms约:16.4分钟 |
COUNT,有索引(主键),1亿条数据,注意 where id > 0 的条件
|
1
2
3
4
5
6
7
8
|
这个有 where id > 0test=# 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 > 0test=# 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 = offenable_hashagg = onenable_hashjoin = onenable_indexscan = onenable_indexonlyscan = on#enable_material = on#enable_mergejoin = on#enable_nestloop = onenable_seqscan = off#enable_sort = onenable_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分钟 |