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

本文涉及的产品
RDS PostgreSQL Serverless,0.5-4RCU 50GB 3个月
推荐场景:
对影评进行热评分析
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

标签

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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
10月前
|
SQL Oracle 关系型数据库
Oracle之你知道哪几种多表Insert的方式?
Oracle之你知道哪几种多表Insert的方式?
109 0
|
10月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
2月前
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
我们经常发现,部分国产数据库声称与 Oracle兼容性高达90%,但在实际迁移过程中,仍需要频繁地修改业务应用的代码。为何实现与Oracle高兼容度的数据库产品如此困难?其中一个重要原因是Oracle兼容性不仅是模仿,而是一个非常复杂和工程量庞大的逆向工程。其技术实现的复杂性以及多如牛毛的细节,足以让多数“年轻”的数据库团队望洋兴叹。YashanDB作为一款从核心理论到关键系统均为原创的数据库产品,从构建初期就具备了技术优势,在Oracle兼容性实现上,敢于亮剑并充分发挥工匠精神,不断打磨,努力构筑一个真正形神兼备的数据库产品。以下将从YashanDB SQL引擎技术、Oracle兼容性的开发
|
2月前
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
Oracle兼容性是目前国产数据库的关键任务之一,其直接影响到商业迁移的成本和竞争力。
40 8
|
6月前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
1172 2
|
10月前
|
人工智能 Oracle 关系型数据库
一篇文章弄懂Oracle和PostgreSQL的Database Link
一篇文章弄懂Oracle和PostgreSQL的Database Link
|
10月前
|
SQL Oracle 关系型数据库
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
|
10月前
|
SQL Oracle 关系型数据库
Oracle insert数据时字符串中有‘单引号问题
Oracle insert数据时字符串中有‘单引号问题
|
10月前
|
存储 Oracle 关系型数据库
PolarDB 开源版通过orafce支持Oracle兼容性
背景PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.本文将介绍PolarDB开源版通过orafce支持Oracle兼容性 .测试环境为m...
173 0
|
关系型数据库 分布式数据库 PolarDB
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
402 0

相关产品

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

    更多