.. _使用并行:

使用并行
========

KingbaseES能使用多核CPU来加速一个SQL语句的执行时间，这种特性被称为并行查询。由于现实条件的限制或因为没有比并行查询计划更快的查询计划存在，很多查询并不能从并行查询获益。但是，对于那些可以从并行查询获益的查询来说，并行查询带来的速度提升是显著的。很多查询在使用并行查询时查询速度比之前快了超过两倍，有些查询是以前的四倍甚至更多的倍数。

本文档将介绍并行查询工作的细节，以及用户应当在什么情况下使用并行查询。

  .. figure:: images/sql-optimization_image20.png
     :scale: 50 %
     :alt: map to buried treasure

- 并行查询原理_
- 并行查询相关参数_
- 并行查询支持的范围_
- Worker进程数与查询性能关系_


并行查询原理
------------

以一个普通并行查询为例：

  .. code::

	explain SELECT * FROM student WHERE age =6;

	QUERY PLAN
	--------------------------------------------------------------------
	Gather (cost=1000.00..80481.52 rows=102334 width=8)
	    Workers Planned: 5
	    -> Parallel Seq Scan on student (cost=0.00..69248.12 rows=20467 width=8)
	        Filter: (age = 6)
	(4 行记录)



\通过EXPLAIN可以查看并行查询的执行计划。当用户输入一个查询语句时，查询分析，查询重写以及查询规划都和原来一样，只有到执行计划时，才开始真正进入并行查询环节。上面执行计划中，Gather节点只是执行计划的一个子节点，属于执行计划的一部分，当查询执行走到Gather节点时，会话进程会申请一定数量的worker进程（根据配置参数，以及成本确定）来进行并行查询过程。在这些worker进程中，有一个充当leader
worker的角色，负责收集汇总各个worker进程查询的结果。该leader
worker进程也会根据查询的数据量大小承担部分的并行查询部分。执行过程如下图所示：

  .. figure:: images/sql-optimization_image21.png
     :scale: 75 %
     :alt: map to buried treasure


Leader
worker进程和其他worker进程通过动态共享内存进行通信。其他worker进程（包括leader
worker进程）把分担查询的结果存储到共享内存中，然后由leader
worker进程进行汇总整个查询的结果。所以需要注意的是，由于并行查询需要使用了动态共享内存，所以dynamic_shared_memory_type参数需要设置为none以外的值。

并行查询相关参数
----------------

要想使用并行查询，需要在kinbase.conf里设置以下参数。

**并行度相关参数：**

-  max_worker_processes：

   设置系统支持的最大后台进程数，默认值是8，此参数调整后需要重启数据库生效。

-  max_parallel_workers：

   最大并行worker数。该数值不能大于max_worker_processes。

-  max_parallel_workers_per_gather：

   最大并行执行worker数。不能超过max_parrellel_workers。

-  max_parallel_maintenance_workers

   最大并行维护操作 worker数。不能超过max_parrellel_workers。

这4个参数之间的关系为：

  max_parallel_workers_per_gather+max_parallel_maintenance_workers <= max_parallel_workers <= max_worker_processes

**并行触发条件：**

-  min_parallel_table_scan_size：

   表的存储空间至少大于等于该值，才有可能触发并行，默认值8MB。

-  min_parallel_index_scan_size

   索引存储空间至少大于等于该数值，才有可能触发并行。默认512KB。

   可以通过该语句来获得表、索引的磁盘存储大小：

   select sys_size_pretty(sys_relation_size('student'));

**优化器控制开关：**

-  enable_parallel_append

   优化器控制开关，是否允许并行append plans。

-  enable_parallel_hash

   优化器控制开关，是否允许并行 hash plans。

**其他参数：**

-  parallel_leader_participation

   控制Gather、Gather merge节点是否能执行subplans。

-  parallel_setup_cost：

   表示启动woker process的启动成本，因为启动worker进程需要建立共享内存等操作，属于附带的额外成本。其值越小，数据库越有可能使用并行查询。默认为1000。

-  parallel_tuple_cost：

   woker进程处理完后的tuple要传输给上层node，即进程间查询结果的交换成本，即后台进程间传输一个元组的代价。其值越小，数据库越有可能使用并行。默认是0.1。

优化器主要根据max_worker_processes决定能够开启worker进程数后，根据目标表或者索引的大小（min_parallel_table_scan_size、min_parallel_index_scan_size），
以及根据parallel_setup_cost和parallel_tuple_cost算出来的并行总cost，
跟其他不使用并行方案比较后决定是否启用并行，以及本次查询的并行进程数（小于等于max_parallel_workers）。

并行查询支持的范围
------------------

KingbaseES支持并行的操作有：

-  DML

  -  全表扫描（Seq Scan）

  -  BTree索扫描（B-tree Index Scan）

  -  Bitmap扫描（Bitmap Heap Scan）

  -  哈希连接（Hash Join）

  -  嵌套循环连接（Nested Loop Join）

  -  归并连接（Merge Join）

  -  聚集（Aggregation）

  -  排序（Sort）

  -  Append

-  DDL

  -  创建索引

  -  Create table as select…

并行全表扫描
^^^^^^^^^^^^^

创建一个包含1000万行记录的表student，设置并行worker为2，由于表大，返回的记录百分比少，优化器使用了并行全部扫描：

  .. code::

	create table student (id int primary key, age int);
	insert into student select id, (random()*100)::integer as age from generate_series(1,10000000) as id;
	analyze student;
	explain select * from student where age = 6;

	QUERY PLAN
	-----------------------------------------------------------------
	Gather (cost=1000.00..102331.58 rows=50000 width=8)
	    Workers Planned: 2
	    -> Parallel Seq Scan on student (cost=0.00..96331.58 rows=20833 width=8)
	        Filter: (age = 6)
	(4 行记录)



\如果我们修改过滤条件，让返回的结果集变大（返回50%左右数据，约500万条），则优化器根据计算出来的cost对比，不再选择并行查询，而使用全部扫描：

  .. code::

	EXPLAIN SELECT * FROM student WHERE age < 50;

	QUERY PLAN
	-----------------------------------------------------------------
	Seq Scan on student (cost=0.00..169248.60 rows=5012113 width=8)
	    Filter: (age < 50)
	(2 行记录)



并行哈希连接
^^^^^^^^^^^^^^

再创建一个小一点表score，插入10万条记录，然后跟student做join：

  .. code::

	create table score(sid int, score int);
	insert into score select sid, (random()*100)::integer as score from generate_series(1, 100000) as sid;
	create index idx_sc on score(sid);
	analyze student;
	explain select * from score join student on score.sid = student.id;

	QUERY PLAN
	------------------------------------------------------------
	Gather (cost=48269.36..145982.13 rows=97616 width=16)
	    Workers Planned: 2
	    -> Hash Join (cost=47269.36..135220.53 rows=19523 width=16)
	        Hash Cond: (student.id = score.sid)
	        -> Parallel Seq Scan on student (cost=0.00..64247.95 rows=1999995 width=8)
	        -> Hash (cost=45667.16..45667.16 rows=97616 width=8)
	            -> Seq Scan on score (cost=0.00..45667.16 rows=97616 width=8)
	(7 行记录)



可以看到，并行查询也支持hash join。每个worker里面都是先扫描小表score计算hash，然后并行扫描大表student，再做hash join。最后将数据汇总到Gather节点合并最终的结果集。

并行嵌套循环连接
^^^^^^^^^^^^^^^^^^

  .. code::

	explain select * from score join student on score.sid = student.id;

	QUERY PLAN
	-------------------------------------------------------------------
	Gather (cost=1000.43..122011.83 rows=97616 width=16)
	    Workers Planned: 2
	    -> Nested Loop (cost=0.43..111250.23 rows=19523 width=16)
	        -> Parallel Seq Scan on score (cost=0.00..44886.23 rows=19523 width=8)
	        -> Index Scan using student_pkey on student (cost=0.43..3.39 rows=1 width=8)
	            Index Cond: (id = score.sid)
	(6 行记录)


\支持嵌套循环连接的并行查询。如果不开启并行，则使用普通的hash
join，性能上会比开启并行后低。

并行聚集
^^^^^^^^^^^^^^^

聚集函数也支持并行：

  .. code::

	explain select avg(age) from student;

	QUERY PLAN
	--------------------------------------------------------------
	Finalize Aggregate (cost=97331.80..97331.81 rows=1 width=32)
	    -> Gather (cost=97331.58..97331.79 rows=2 width=32)
	        Workers Planned: 2
	        -> Partial Aggregate (cost=96331.58..96331.59 rows=1 width=32)
	            -> Parallel Seq Scan on student (cost=0.00..85914.87 rows=4166687 width=4)
	(5 行记录)


并行排序
^^^^^^^^^^^^

下面例子中支持了并行排序和Gather merge。

  .. code::

	create table t1(id int, val int, name varchar(64));
	create index t1_idx on t1(id);
	insert into t1 select i, i%5000, 'Kingbase'||(i%5) from generate_series(1,3000000) as x(i);
	analyze t1;
	explain select * from t1 where val< 10 order by name;

	QUERY PLAN
	--------------------------------------------------------------------
	Gather Merge (cost=36003.18..36608.72 rows=5190 width=18)
	    Workers Planned: 2
	    -> Sort (cost=35003.16..35009.64 rows=2595 width=18)
	        Sort Key: name
	        -> Parallel Seq Scan on t1 (cost=0.00..34856.00 rows=2595 width=18)
	            Filter: (val < 10)
	(6行记录)


并行Append
^^^^^^^^^^^^^^^

对2个大表做union的时候，也支持并行append：

  .. code::

	create table student2 as select * from student;
	analyze student2;
	explain select * from student union all select * from student2 where age = 6;
	QUERY PLAN
	--------------------------------------------------------------------
	Append (cost=0.00..303155.94 rows=10100644 width=8)
	    -> Seq Scan on student (cost=0.00..144247.77 rows=9999977 width=8)
	    -> Gather (cost=1000.00..107398.28 rows=100667 width=8)
	        Workers Planned: 2
	        -> Parallel Seq Scan on student2 (cost=0.00..96331.58 rows=41945 width=8)
	            Filter: (age = 6)
	(6行记录)



Worker进程数与查询性能关系
--------------------------

并不是所有的查询都能适用于并行查询。因为考虑到系统的开销以及进程占用资源的情况，也并不是开启的worker数量越多，查询效率越高。下面以对同一个表做同样的查询，然后开启不同的worker数量来测试一下。

开启 1 个worker 进程：

  .. code::

	create table test(n bigint);
	insert into test values(generate_series(1, 4000000));
	analyze test;
	set max_parallel_workers_per_gather = 1;
	explain analyze select * from test where n = 4000000;

	QUERY PLAN
	--------------------------------------------------------------------
	Gather (cost=1000.00..59888.81 rows=1 width=4) (actual time=244.328..301.458 rows=1 loops=1)
	    Workers Planned: 1
	    Workers Launched: 1
	    -> Parallel Seq Scan on test (cost=0.00..58888.71 rows=1 width=4) (actual time=268.845..297.372 rows=0 loops=2)
	        Filter: (n = 4000000)
	        Rows Removed by Filter: 2500000
	Planning time: 0.119 ms
	Execution time: 302.026 ms
	(8 rows)


开启 2 个worker 进程：

  .. code::

	set max_parallel_workers_per_gather = 2;
	explain analyze select * from test where n = 4000000;

	QUERY PLAN
	--------------------------------------------------------------------
	Gather (cost=1000.00..49165.77 rows=1 width=4) (actual time=218.212..218.287 rows=1 loops=1)
	    Workers Planned: 2
	    Workers Launched: 2
	    -> Parallel Seq Scan on test (cost=0.00..48165.67 rows=1 width=4) (actual time=200.619..213.684 rows=0 loops=3)
	        Filter: (n = 4000000)
	        Rows Removed by Filter: 1666666
	Planning time: 0.117 ms
	Execution time: 219.005 ms



开启 4 个worker 进程，但是实际上基于成本考虑只启用了3个：

  .. code::

	set max_parallel_workers_per_gather = 4;
	explain analyze select * from test where n = 4000000;

	QUERY PLAN
	--------------------------------------------------------------------
	Gather (cost=1000.00..43285.39 rows=1 width=4) (actual time=190.967..191.017 rows=1 loops=1)
	    Workers Planned: 3
	    Workers Launched: 3
	    -> Parallel Seq Scan on test (cost=0.00..42285.29 rows=1 width=4) (actual time=174.275..182.767 rows=0 loops=4)
	        Filter: (n = 4000000)
	        Rows Removed by Filter: 1250000
	Planning time: 0.119 ms
	Execution time: 191.817 ms
	(8 rows)

\通过create table test(n int) with (parallel_workers = 4);
强制启动4个worker进程测试：

  .. code::

	drop table if exists test;
	create table test(n int) with (parallel_workers = 4);
	insert into test values(generate_series(1, 4000000));
	analyze test;
	explain analyze select * from test where n = 4000000;

	QUERY PLAN
	--------------------------------------------------------------------
	Gather (cost=1000.00..38749.10 rows=1 width=4) (actual time=181.145..181.315 rows=1 loops=1)
	    Workers Planned: 4
	    Workers Launched: 4
	    -> Parallel Seq Scan on test (cost=0.00..37749.00 rows=1 width=4) (actual time=163.565..169.837 rows=0 loops=5)
	        Filter: (n = 4000000)
	        Rows Removed by Filter: 1000000
	Planning time: 0.050 ms
	Execution time: 185.391 ms


\对比查询结果，可以看出，查询效率并没有随worker数量线性增加；在启用3个worker进程和4个worker进程进行并行查询时，查询效率基本一致了。所以并不是开启的worker数量越多，查询效率越高，这个也是系统有成本考虑在内。

另外，如果由于CPU、共享内存、worker进程等资源是整个数据库示例共享，一个查询如果消耗了大量的资源（比如并行开启了4个worker），则其他会话能够申请到的资源会变得有限，查询性能会受到影响，这一问题在OLTP事务型应用里尤其需要注意。所以并行参数的设置一定需要在一个合理的范围以内。

