PostgreSQL 并行计算解说 之11 - parallel gather, gather merge

本文涉及的产品
PolarClaw,2核4GB
简介: 标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan parallel index sc

标签

PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持


背景

PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。

parallel seq scan                      
                      
parallel index scan                      
                      
parallel index only scan                      
                      
parallel bitmap scan                      
                      
parallel filter                      
                  
parallel hash agg                  
                  
parallel group agg                  
                      
parallel cte                      
                      
parallel subquery                      
                      
parallel create table                      
                      
parallel create index                      
                      
parallel select into                      
                      
parallel CREATE MATERIALIZED VIEW                      
                      
parallel 排序 : gather merge                       
                      
parallel nestloop join                      
                      
parallel hash join                      
                      
parallel merge join                      
                      
parallel 自定义并行聚合                      
                      
parallel 自定义并行UDF                      
                      
parallel append                      
                      
parallel union                      
                      
parallel fdw table scan                      
                      
parallel partition join                      
                      
parallel partition agg                      
                      
parallel gather              
      
parallel gather merge      
                      
parallel rc 并行                      
                      
parallel rr 并行                      
                      
parallel GPU 并行                      
                      
parallel unlogged table                       

接下来进行一一介绍。

关键知识请先自行了解:

1、优化器自动并行度算法 CBO

《PostgreSQL 9.6 并行计算 优化器算法浅析》

《PostgreSQL 11 并行计算算法,参数,强制并行度设置》

parallel gather , gather merge

并行计算结果合并: (gather, gather merge)

parallel gather , gather merge 是一段并行任务中的ROOT部分,gather用来收集所有计算子进程的计算结果,gather merge用来收集所有计算子进程的计算结果并进行归并排序。

数据量:10亿。

场景 数据量 关闭并行 开启并行 并行度 开启并行性能提升倍数
普通并行(gather) 10 亿 70.2 秒 2.5 秒 30 28.1 倍
归并并行(gather merge) 10 亿 78.2 秒 2.76 秒 30 28.3 倍

1、关闭并行,耗时: 70.2 秒 , 78.2 秒。

postgres=# explain select max(i) from table2;  
                                 QUERY PLAN                                   
----------------------------------------------------------------------------  
 Aggregate  (cost=16924779.00..16924779.01 rows=1 width=4)  
   ->  Seq Scan on table2  (cost=0.00..14424779.00 rows=1000000000 width=4)  
(2 rows)  
  
postgres=# explain select i from table2 order by i desc limit 10;  
                                    QUERY PLAN                                      
----------------------------------------------------------------------------------  
 Limit  (cost=10036034419.47..10036034419.50 rows=10 width=4)  
   ->  Sort  (cost=10036034419.47..10038534419.47 rows=1000000000 width=4)  
         Sort Key: i DESC  
         ->  Seq Scan on table2  (cost=0.00..14424779.00 rows=1000000000 width=4)  
(4 rows)  
  
  
postgres=# select max(i) from table2;  
 max   
-----  
  -1  
(1 row)  
  
Time: 70168.043 ms (01:10.168)  
  
postgres=# select i from table2 order by i desc limit 10;  
  i    
-----  
  -1  
  -2  
  -6  
  -7  
  -7  
 -12  
 -12  
 -14  
 -16  
 -18  
(10 rows)  
  
Time: 78217.388 ms (01:18.217)  

2、开启并行,耗时: 2.5 秒 , 2.76 秒。

postgres=# explain select max(i) from table2;  
                                          QUERY PLAN                                            
----------------------------------------------------------------------------------------------  
 Finalize Aggregate  (cost=4841445.75..4841445.76 rows=1 width=4)  
   ->  Gather  (cost=4841445.67..4841445.68 rows=30 width=4)  
         Workers Planned: 30  
         ->  Partial Aggregate  (cost=4841445.67..4841445.68 rows=1 width=4)  
               ->  Parallel Seq Scan on table2  (cost=0.00..4758112.33 rows=33333333 width=4)  
(5 rows)  
  
postgres=# explain select i from table2 order by i desc limit 10;  
                                          QUERY PLAN                                            
----------------------------------------------------------------------------------------------  
 Limit  (cost=10005478434.44..10005478434.72 rows=10 width=4)  
   ->  Gather Merge  (cost=10005478434.44..10032832749.05 rows=999999990 width=4)  
         Workers Planned: 30  
         ->  Sort  (cost=10005478433.68..10005561767.01 rows=33333333 width=4)  
               Sort Key: i DESC  
               ->  Parallel Seq Scan on table2  (cost=0.00..4758112.33 rows=33333333 width=4)  
(6 rows)  
  
  
postgres=# select max(i) from table2;  
 max   
-----  
  -1  
(1 row)  
  
Time: 2523.247 ms (00:02.523)  
  
postgres=# select i from table2 order by i desc limit 10;  
  i    
-----  
  -1  
  -2  
  -6  
  -7  
  -7  
 -12  
 -12  
 -14  
 -16  
 -18  
(10 rows)  
  
Time: 2762.977 ms (00:02.763)  

其他知识

1、优化器自动并行度算法 CBO

《PostgreSQL 9.6 并行计算 优化器算法浅析》

《PostgreSQL 11 并行计算算法,参数,强制并行度设置》

2、function, op 识别是否支持parallel

postgres=# select proparallel,proname from pg_proc;                      
 proparallel |                   proname                                          
-------------+----------------------------------------------                      
 s           | boolin                      
 s           | boolout                      
 s           | byteain                      
 s           | byteaout                      

3、subquery mapreduce unlogged table

对于一些情况,如果期望简化优化器对非常非常复杂的SQL并行优化的负担,可以自己将SQL拆成几段,中间结果使用unlogged table保存,类似mapreduce的思想。unlogged table同样支持parallel 计算。

4、vacuum,垃圾回收并行。

5、dblink 异步调用并行

《PostgreSQL VOPS 向量计算 + DBLINK异步并行 - 单实例 10亿 聚合计算跑进2秒》

《PostgreSQL 相似搜索分布式架构设计与实践 - dblink异步调用与多机并行(远程 游标+记录 UDF实例)》

《PostgreSQL dblink异步调用实现 并行hash分片JOIN - 含数据交、并、差 提速案例 - 含dblink VS pg 11 parallel hash join VS pg 11 智能分区JOIN》

暂时不允许并行的场景(将来PG会继续扩大支持范围):

1、修改行,锁行,除了create table as , select into, create mview这几个可以使用并行。

2、query 会被中断时,例如cursor , loop in PL/SQL ,因为涉及到中间处理,所以不建议开启并行。

3、paralle unsafe udf ,这种UDF不会并行

4、嵌套并行(udf (内部query并行)),外部调用这个UDF的SQL不会并行。(主要是防止large parallel workers )

5、SSI 隔离级别

参考

https://www.postgresql.org/docs/11/parallel-plans.html

《PostgreSQL 11 并行计算算法,参数,强制并行度设置》

《PostgreSQL 11 preview - 并行计算 增强 汇总》

《PostgreSQL 10 自定义并行计算聚合函数的原理与实践 - (含array_agg合并多个数组为单个一元数组的例子)》

《PostgreSQL 9.6 并行计算 优化器算法浅析》

 

免费领取阿里云RDS PostgreSQL实例、ECS虚拟机

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
存储 关系型数据库 MySQL
MySQL中为什么要使用索引合并(Index Merge)?
通过这些内容的详细介绍和实际案例分析,希望能帮助您深入理解索引合并及其在MySQL中的
784 10
|
分布式计算 DataWorks 关系型数据库
DataWorks产品使用合集之ODPS数据怎么Merge到MySQL数据库
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
388 1
|
SQL 存储 Oracle
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
4149 0
|
存储 SQL 关系型数据库
MySQL 优化 index merge(索引合并)引起的死锁分析(强烈推荐)
生产环境出现死锁流水,通过查看死锁日志,看到造成死锁的是两条一样的update语句(只有where条件中的值不同),如下:
|
SQL 存储 弹性计算
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
标签 PostgreSQL , 数据离散性 , 扫描性能 , 重复扫 , bitmap index scan , 排序扫描 , 扫描方法 , 顺序 背景 一个这样的问题: 为什么select x from tbl offset x limit x; 两次查询连续的OFFSET,会有重复数据呢? select ctid,* from tbl where ... offset 0 li
2341 0
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之13 - parallel OLAP : 中间结果 parallel with unlogged table
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan parallel
773 0
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之14 - parallel index scan
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan paral
1400 0
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之15 - parallel bitmap scan
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan
1567 0
|
7月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
500 158
|
7月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。

推荐镜像

更多