PostgreSQL COUNT的各條件下(1億條數據)例子

插入一億條數據

(示例數據庫:9.3.5)web

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分鐘
oop

 

添加主鍵索引耗時

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分鐘
spa

 

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分鐘
.net

 

調優經過修改配置文件

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
orm

 

test=# select count(id) from tbl_time1 where id > 0;
   count
-----------
100000000
(1 row)
Time: 87501.151 ms
約:1.456分鐘索引

 

 

問題:內存

1.count都會掃全表很慢的count 超過幾十萬 都是掃全表的?  修改配置文件,是 Index Only Scan的 enable_indexonlyscan = on 優先get

2.沒辦法查詢控制在秒級嗎? 測試測試機是普通PC的,之前的AMD4核,8G內存,7200轉 的硬盤。不過,相信再調優下內存,我以爲應該是能夠的。

 

 

http://www.oschina.net/question/107976_178878

相關文章
相關標籤/搜索