Oracle 利用管道函数(pipelined)实现高性能大数据处理

2024-05-29 21:38

本文主要是介绍Oracle 利用管道函数(pipelined)实现高性能大数据处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

引言

在plsql开发中,会涉及到一些大数据量表的数据处理,如将某记录数超亿的表的记录经过处理转换插入到另外一张或几张表。
常规的操作方法固然可以实现,但时间、磁盘IO、redo日志等等都非常大。Oracle 提供了一种高级函数,可以将这种数据处理的性能提升到极限。这种函数称为管道函数。在实际项目中,管道函数会和表函数、数据流函数(即表函数和CURSOR结合)、数据集合、并行度一起使用,达到大数据处理的性能顶峰。转自:(http://mikixiyou.iteye.com/blog/1673672)

一、普通方法处理大数据

下面是一个例子,将表t_ss_normal的记录插入到表t_target中,插入过程中有部分转换操作。我分成四个方法来实现这个数据处理操作。

这是源表和目标表的表结构。现在源表有200W条,其数据来自dba_objects视图。

create table T_SS_NORMAL
(owner          VARCHAR2(30),object_name    VARCHAR2(128),subobject_name VARCHAR2(30),object_id      NUMBER,data_object_id NUMBER,object_type    VARCHAR2(19),created        DATE,last_ddl_time  DATE,timestamp      VARCHAR2(19),status         VARCHAR2(7),temporary      VARCHAR2(1),generated      VARCHAR2(1),secondary      VARCHAR2(1)
);
/create table T_TARGET
(owner       VARCHAR2(30),object_name VARCHAR2(128),comm        VARCHAR2(10)
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

1、一个insert into select语句搞定这个数据处理,简单。

create or replace package pkg_test isprocedure load_target_normal;
end pkg_test;create or replace package body pkg_test isprocedure load_target_normal isbegin  insert into t_target (owner, object_name, comm)select owner, object_name, 'xxx' from t_ss_normal;  commit;  end;
beginnull;
end pkg_test; 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2、采用管道函数实现这个数据处理。

create type obj_target as object(
owner VARCHAR2(30), object_name VARCHAR2(128), comm varchar2(10)
);
/
create or replace type typ_array_target as table of obj_target;
/create or replace package pkg_test is  function pipe_target(p_source_data in sys_refcursor) return typ_array_targetpipelined;procedure load_target;
end pkg_test;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

首先创建两个自定义的类型。obj_target的定义和t_target的表结构一致,用于存储每一条目标表记录。typ_array_target用于管道函数的返回值。
接着定义一个管道函数。
普通函数的结尾加一个pipelined关键字,就是管道函数。这个函数的返回参数类型为集合,这是为了使其能作为表函数使用。表函数就是在from子句中以table(v_resultset)调用的,v_resultset就是一个集合类型的参数。
最后定义一个调用存储过程。

在包体中定义该管道函数和调用存储过程。管道函数pipe_target的传入参数一个sys_refcursor类型。这是一个游标,可以理解为使用select * from table才能得到的结果集。
你也可以不用这个传入的游标,取而代之,在函数中定义一个游标,也一样使用。

  function pipe_target(p_source_data in sys_refcursor) return typ_array_targetpipelined isr_target_data obj_target := obj_target(null, null, null);r_source_data t_ss%rowtype; beginloopfetch p_source_datainto r_source_data;exit when p_source_data%notfound;    r_target_data.owner       := r_source_data.owner;r_target_data.object_name := r_source_data.object_name;r_target_data.comm        := 'xxx';    pipe row(r_target_data);end loop;close p_source_data;return;end;procedure load_target isbegin  insert into t_target(owner, object_name, comm)select owner, object_name, commfrom table(pipe_target(cursor(select * from t_ss_normal)));  commit;  end;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

关键字 pipe row 的作用是将obj_target插入到typ_array_target类型的数组中,管道函数自动返回这些数据。因为源表的数据量会非常大,所以在fetch取值时会使用bulk collect ,实现批量取值。这样做可以减少plsql引擎和sql引擎的控制转换次数。这种转换称为上下文切换。

  function pipe_target_array(p_source_data in sys_refcursor,p_limit_size  in pls_integer default c_default_limit)return typ_array_targetpipelined is  r_target_data obj_target := obj_target(null, null, null); type typ_source_data is table of t_ss%rowtype index by pls_integer;aa_source_data typ_source_data;beginloopfetch p_source_data bulk collectinto aa_source_data;exit when aa_source_data.count = 0;for i in 1 .. aa_source_data.count loopr_target_data.owner       := aa_source_data(i).owner;r_target_data.object_name := aa_source_data(i).object_name;r_target_data.comm        := 'xxx';pipe row(r_target_data);end loop;end loop;close p_source_data;return;end;procedure load_target_array isbegininsert into t_target(owner, object_name, comm)select owner, object_name, commfrom table(pipe_target_array(cursor (select * from t_ss_normal),100));  commit;  end;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

还可以使用并行度,使得管道函数可以多进程同时执行。并行度还有一个好处,就是将数据插入方式从常规路径转换为直接路径。直接路径可以大量减少redo日志的生成量。

  function pipe_target_parallel(p_source_data in sys_refcursor,p_limit_size  in pls_integer default c_default_limit)return typ_array_targetpipelinedparallel_enable(partition p_source_data by any) isr_target_data obj_target := obj_target(null, null, null);type typ_source_data is table of t_ss%rowtype index by pls_integer;  aa_source_data typ_source_data;begin  loopfetch p_source_data bulk collectinto aa_source_data;exit when aa_source_data.count = 0;    for i in 1 .. aa_source_data.count loop      r_target_data.owner       := aa_source_data(i).owner;r_target_data.object_name := aa_source_data(i).object_name;r_target_data.comm        := 'xxx';      pipe row(r_target_data);      end loop;    end loop;  close p_source_data;return;end;procedure load_target_parallel isbeginexecute immediate 'alter session enable parallel dml';  insert /*+parallel(t,4)*/into t_target t(owner, object_name, comm)select owner, object_name, commfrom table(pipe_target_array(cursor (select /*+parallel(s,4)*/*from t_ss_normal s),100));  commit;end;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

还可以使用并行度,使得管道函数可以多进程同时执行。并行度还有一个好处,就是将数据插入方式从常规路径转换为直接路径。直接路径可以大量减少redo日志的生成量。

  function pipe_target_parallel(p_source_data in sys_refcursor,p_limit_size  in pls_integer default c_default_limit)return typ_array_targetpipelinedparallel_enable(partition p_source_data by any) isr_target_data obj_target := obj_target(null, null, null);type typ_source_data is table of t_ss%rowtype index by pls_integer;  aa_source_data typ_source_data;begin  loopfetch p_source_data bulk collectinto aa_source_data;exit when aa_source_data.count = 0;    for i in 1 .. aa_source_data.count loop      r_target_data.owner       := aa_source_data(i).owner;r_target_data.object_name := aa_source_data(i).object_name;r_target_data.comm        := 'xxx';      pipe row(r_target_data);      end loop;    end loop;  close p_source_data;return;end;procedure load_target_parallel isbeginexecute immediate 'alter session enable parallel dml';  insert /*+parallel(t,4)*/into t_target t(owner, object_name, comm)select owner, object_name, commfrom table(pipe_target_array(cursor (select /*+parallel(s,4)*/*from t_ss_normal s),100));  commit;end;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

在测试过程中,我测试200W记录的操作,时间从24秒降到到8秒,重做日志也降低更多。

这篇关于Oracle 利用管道函数(pipelined)实现高性能大数据处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1014709

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

oracle 11g导入\导出(expdp impdp)之导入过程

《oracle11g导入导出(expdpimpdp)之导入过程》导出需使用SEC.DMP格式,无分号;建立expdir目录(E:/exp)并确保存在;导入在cmd下执行,需sys用户权限;若需修... 目录准备文件导入(impdp)1、建立directory2、导入语句 3、更改密码总结上一个环节,我们讲了