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 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分鍾


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM