PostgreSQL9.6:Parallel sequential scans 初體驗

Oracle 支持強大的並行功能,建立索引,表分析,數據加載時能夠開啓並行,這項功能讓不少數據庫產品垂涎, 做爲開源數據庫 PostgreSQL 在並行方面也在努力嘗試,很早以前 PostgreSQL 幾乎不支持任何並行的做業,到了 9.5 版本 PostgreSQL 支持並行的 vacuum,到了 9.6 後, PostgreSQL 支持並行的順序掃描,這是使人振奮的消息,由於這極大的提高了 PostgreSQL 統計分析SQL的性能,因爲硬件環境限制,今天簡單體驗下,如下實驗在筆記本虛擬機上進行。html

一 關於 max_parallel_degree (integer) 參數

這個參數配置決定了每一個 parallel query 並行操做容許的最大後臺進程數,這個值的設置受後臺進程數參數 max_worker_processes 限制。node

二 PostgreSQL9.6 Beta1 測試

--設置 max_parallel_degreesql

[pg96@db1 ~]$ grep "max_parallel_degree" $PGDATA/postgresql.conf
max_parallel_degree = 4                 # max number of worker processes per node

--建立測試表數據庫

[pg96@db1 ~]$ psql francs francs
psql (9.6beta1)
Type "help" for help.

francs=> create table test_big1(id serial, name character varying(64),create_time timestamp(0) without time zone);
CREATE TABLE

francs=> insert into test_big1(id,name)select n, n||'_test' from generate_series(1,5000000)n;
INSERT 0 5000000

--執行計劃服務器

francs=> explain analyze select count(*) from test_big1;
-------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=45560.42..45560.43 rows=1 width=8) (actual time=4236.468..4236.469 rows=1 loops=1)
   ->  Gather  (cost=45560.00..45560.41 rows=4 width=8) (actual time=4232.517..4232.556 rows=5 loops=1)
         Workers Planned: 4
         Workers Launched: 4
         ->  Partial Aggregate  (cost=44560.00..44560.01 rows=1 width=8) (actual time=4182.972..4182.973 rows=1 loops=5)
               ->  Parallel Seq Scan on test_big1  (cost=0.00..41435.00 rows=1250000 width=0) (actual time=0.034..2450.966 rows=1000000 loops=5)
 Planning time: 112.309 ms
 Execution time: 4236.920 ms
(8 rows)

備註:執行屢次,執行時間大概都在4秒多點,從執行計劃中看到走了並行順序掃描「Parallel Seq Scan on test_big1」,再細看「Workers Launched: 4」,表示開啓了四個並行進程。oop

--查看並行順序掃描進程
圖片描述
備註:圖中可看到出現了四個 parallel worker 進程。post

三 PostgreSQL9.5 測試

測試以前先把 PostgreSQL 9.6 的數據庫關了,在確保相等狀況下進行測試。性能

--建立測試表測試

[pg95@db1 ~]$ psql fdb fdb
psql (9.5alpha1)
Type "help" for help.

fdb=> create table test_big1(id serial, name character varying(64),create_time timestamp(0) without time zone);
CREATE TABLE

fdb=> insert into test_big1(id,name)select n, n||'_test' from generate_series(1,5000000)n;
INSERT 0 5000000

fdb=> explain analyze select count(*) from test_big1;
                                                         QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=91435.00..91435.01 rows=1 width=0) (actual time=8389.093..8389.094 rows=1 loops=1)
   ->  Seq Scan on test_big1  (cost=0.00..78935.00 rows=5000000 width=0) (actual time=9.958..4781.116 rows=5000000 loops=1)
 Planning time: 2.436 ms
 Execution time: 8391.758 ms
(4 rows)

備註:屢次執行,時間在 8 秒左右。ui

四 總結

因爲硬件關係緣由,本測試只在筆記本虛擬機上測試,在這個全表掃描測試場景下, PostgreSQL 9.6 是 PostgreSQL9.5 性能的兩倍,今天僅是初步體驗並行掃描,能夠預測若是在X86服務器上測試,這個性能倍數會高一些,後續測試並行掃描的其它場景;我的認爲 PostgreSQL 對並行順序掃描的支持,在統計分析性能方面的提高前進了一大步。

五 參考

Robert Haas: PostgreSQL 9.6 with Parallel Query vs. TPC-H
max_parallel_degree (integer)

相關文章
相關標籤/搜索