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实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in