PostgreSQL Oracle 兼容性之 - insert all into ... 多表写入

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , Oracle , 多表批量写入 , insert all into , CTE


背景

Oracle支持 insert all 的语法,同时往多个表插入。

https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9014.htm

https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9014.htm#i2125362

PostgreSQL可以通过CTE语法达到同样的效果。

https://www.postgresql.org/docs/11/static/queries-with.html

Oracle insert all into 例子

create table a(id int, c1 int, c2 int);  
  
create table b(id int, c1 int, c2 int);  
  
  
set autotrace on;  
insert all   
  into a (id,c1) values (id, col1)  
  into b (id,c2) values (id, col2)  
select rownum as id, trunc(dbms_random.value(0, 100)) as col1, trunc(dbms_random.value(0, 100)) as col2 from dual connect by level <=10000;  
  
20000 rows created.  
Execution Plan  
----------------------------------------------------------  
Plan hash value: 702343910  
  
-----------------------------------------------------------------------------------------  
| Id  | Operation                        | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
-----------------------------------------------------------------------------------------  
|   0 | INSERT STATEMENT                 |      |     1 |    39 |     2   (0)| 00:00:01 |  
|   1 |  MULTI-TABLE INSERT              |      |       |       |            |          |  
|   2 |   INTO                           | A    |       |       |            |          |  
|   3 |   INTO                           | B    |       |       |            |          |  
|   4 |    VIEW                          |      |     1 |    39 |     2   (0)| 00:00:01 |  
|   5 |     COUNT                        |      |       |       |            |          |  
|*  6 |      CONNECT BY WITHOUT FILTERING|      |       |       |            |          |  
|   7 |       FAST DUAL                  |      |     1 |       |     2   (0)| 00:00:01 |  
-----------------------------------------------------------------------------------------  
  
Predicate Information (identified by operation id):  
---------------------------------------------------  
  
   6 - filter(LEVEL<=10000)  
  
  
Statistics  
----------------------------------------------------------  
        531  recursive calls  
        661  db block gets  
        205  consistent gets  
          0  physical reads  
     392344  redo size  
        823  bytes sent via SQL*Net to client  
        932  bytes received via SQL*Net from client  
          3  SQL*Net roundtrips to/from client  
          6  sorts (memory)  
          0  sorts (disk)  
      20000  rows processed  

PostgreSQL CTE例子

create table a(id int, c1 int, c2 int);  
  
create table b(id int, c1 int, c2 int);  
insert all   
  into a (id,c1) values (id, col1)  
  into b (id,c2) values (id, col2)  
select id, random() col1, random() col2 from generate_series(1,10000) t(id);  
  
ERROR:  42601: syntax error at or near "all"  
LINE 1: insert all   
               ^  
LOCATION:  scanner_yyerror, scan.l:1087  
with tmp as (select id, random() col1, random() col2 from generate_series(1,10000) t(id)),  
ins1 as (insert into a (id,c1) select id,col1 from tmp)  
insert into b (id,c2) select id,col2 from tmp;  
  
INSERT 0 10000  
postgres=# select count(*) from a;  
 count   
-------  
 10000  
(1 row)  
  
postgres=# select count(*) from b;  
 count   
-------  
 10000  
(1 row)  
  
  
postgres=# select * from a limit 10;  
 id | c1 | c2   
----+----+----  
  1 |  0 |     
  2 |  1 |     
  3 |  1 |     
  4 |  0 |     
  5 |  1 |     
  6 |  0 |     
  7 |  1 |     
  8 |  0 |     
  9 |  0 |     
 10 |  0 |     
(10 rows)  
  
postgres=# select * from b limit 10;  
 id | c1 | c2   
----+----+----  
  1 |    |  0  
  2 |    |  0  
  3 |    |  1  
  4 |    |  1  
  5 |    |  0  
  6 |    |  1  
  7 |    |  1  
  8 |    |  1  
  9 |    |  0  
 10 |    |  0  
(10 rows)  
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
29天前
|
Oracle 关系型数据库 分布式数据库
PolarDB常见问题之PolarDB(Oracle兼容版) 执行命令报错如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
4月前
|
SQL Oracle 关系型数据库
Oracle之你知道哪几种多表Insert的方式?
Oracle之你知道哪几种多表Insert的方式?
33 0
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
1月前
|
SQL Oracle 关系型数据库
Oracle insert数据时字符串中有‘单引号问题
Oracle insert数据时字符串中有‘单引号问题
|
7月前
|
Oracle 关系型数据库 数据库
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
201 0
|
8月前
|
SQL Oracle 关系型数据库
物化视图(Oracle与PostgreSQL对比)
物化视图(Oracle与PostgreSQL对比)
|
4月前
|
SQL Oracle 关系型数据库
Oracle,Postgresql等数据库使用
Oracle,Postgresql等数据库简单使用
133 0
Oracle,Postgresql等数据库使用
|
7月前
|
Oracle 关系型数据库 分布式数据库
如何从Oracle迁移到PolarDB(ADAM)(二)
如何从Oracle迁移到PolarDB(ADAM)(二)
128 0
|
7月前
|
SQL Oracle 关系型数据库
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
692 0
|
9月前
|
SQL Cloud Native 关系型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
730 1

相关产品

  • 云原生数据库 PolarDB
  • 推荐镜像

    更多