暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

postgresql insert into select无法使用并行查询

瀚高PG实验室 2021-04-16
1104

本文信息基于PG13.1。
从PG9.6开始支持并行查询。PG11开始支持CREATE TABLE … AS、SELECT INTO以及CREATE MATERIALIZED VIEW的并行查询。
先说结论:
换用create table as 或者select into或者导入导出。

首先跟踪如下查询语句的执行计划:
select count(*) from test t1,test1 t2 where t1.id = t2.id ;

可以看到走了两个Workers。
下边看一下insert into select:

可以看到并没有Workers的指示,没有启用并行查询。
即使开启强制并行,也无法走并行查询。

原因在官方文档有写:
The query writes any data or locks any database rows. If a query contains a data-modifying operation either at the top level or within a CTE, no parallel plans for that query will be generated. As an exception, the commands CREATE TABLE … AS, SELECT INTO, and CREATE MATERIALIZED VIEW which create a new table and populate it can use a parallel plan.

解决方案有如下三种:
1.select into

2.create table as

3.或者通过导入导出的方式,例如:

一些场景下也会比非并行快。

文章转载自瀚高PG实验室,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论