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)

相关文章
相关标签/搜索