Converting REF CURSOR to PIPE for Performance in PHP OCI8 and PDO_OCI
原文地址:https://blogs.oracle.com/opal/entry/converting_ref_cursor_to_pipe
REF CURSORs are common in Oracle's stored procedural language PL/SQL. They let you pass around a pointer to a set of query results.
However in PHP, PDO_OCI doesn't yet allow fetching from them. And fetching from REF CURSORS in OCI8 is not as fast as doing a normal query. This is because of two architectural decisions: OCI8 doesn't use the preferred Oracle style of "array fetching". Instead it uses "prefetching", but prefetching from REF CURSORS isn't supported by Oracle for various reasons (including that array fetching is available and recommended....)
One workaround, when you can't rewrite the PL/SQL code to do a normal query, is to write a wrapper function that pipes the output.
For this example, the "fixed" procedure that you can't change is:
create or replace procedure myproc(p1 out sys_refcursor) as
begin
open p1 for select last_name from employees;
end;
The wrapper function follows the performance tip in 12-14 of Tuning PL/SQL Applications for Performance and uses a BULK COLLECT:
create or replace package myplmap as
type outtype is record ( -- structure of the ref cursor in myproc
last_name varchar2()
);
type outtype_set is table of outtype;
function maprctopl return outtype_set pipelined;
end;
/
show errors create or replace package body myplmap as
function maprctopl return outtype_set pipelined is
outrow outtype_set;
p_rc sys_refcursor;
rlim pls_integer := ; -- fetch batches of rows at a time
begin
myproc(p_rc); -- call the original procedure
loop
fetch p_rc bulk collect into outrow limit rlim;
exit when outrow.count = ;
for i in .. outrow.count loop
pipe row (outrow(i));
end loop;
end loop;
end maprctopl;
end myplmap;
The PHP OCI8 code to query the pipelined function is:
<?php
$c = oci_connect('hr', 'hrpwd', '//localhost/XE');
$s = oci_parse($c, "select * from table(myplmap.maprctopl())");
oci_execute($s);
oci_fetch_all($s, $res);
var_dump($res);
?>
You could use this query in PDO_OCI too.
I found it doubled the performance of smallish tests with a local database (from small time to even smaller time). The change is more dramatic from a distant, remote database. With a query returning a large number of rows it dropped the runtime to 35 seconds from about 24 minutes.
This tip will appear in the next edition of the Underground PHP and Oracle Manual.
Update: Prefetching from REF CURSORS is supported in Oracle 11gR2. See Oracle Database 11gR2 Enhancements for PHP
Converting REF CURSOR to PIPE for Performance in PHP OCI8 and PDO_OCI的更多相关文章
- Report_报表中Ref Cursor数据源的概念和用法(案例)
2014-06-22 Created By BaoXinjian
- REF CURSOR和CURSOR
REF CURSOR DECLARE TYPE TY_EMP_CUR IS REF CURSOR; V_Emp_Cur TY_EMP_CUR; V_Id EMP.ID%TYPE; BEGIN OPEN ...
- Entity Framework 5.0.0 Function Import 以及 ODP. NET Implicit REF CURSOR Binding使用简介
源代码 概要: 1,说明如何使用Entity Framework中的function import功能. 2,说明如何使用ODP.NET的隐式REF CURSOR绑定(implicit REF CUR ...
- Oracle ref cursor和sys_refcursor
1. 自定义 ref cursor 和 sys_refcursor; 2. sys_refcursor 做为参数传递结果集; 3. ref cursor 做为参数传递结果集; 1. 自定义 ref c ...
- oracle中REF Cursor用法
from:http://www.111cn.net/database/Oracle/42873.htm 1,什么是 REF游标 ? 动态关联结果集的临时对象.即在运行的时候动态决定执行查询. 2,RE ...
- PLSQL中显示Cursor、隐示Cursor、动态Ref Cursor差别
一.显式cursor 显式是相对与隐式cursor而言的,就是有一个明白的声明的cursor.显式游标的声明类似例如以下(具体的语法參加plsql ref doc ): cursor cursor_n ...
- ORACLE中RECORD、VARRAY、TABLE、IS REF CURSOR 的使用及实例详解
ORACLE中RECORD.VARRAY.TAB.IS REF CURSOR LE的使用及实例详解 create or replaceprocedure PRO_RECORD_ROW_TAB_EXAM ...
- oracle sys_refcursor用法和ref cursor区别
--创建过程,参数为sys_refcursor,为out型 create or replace procedure aabbsys_refcursor(o out sys_refcursor) is ...
- oracle 存储过程及REF CURSOR的使用
基本使用方法及示例 1.基本结构: CREATE OR REPLACE PROCEDURE 存储过程名字 (参数1 IN NUMBER,参数2 IN NUMBER) AS 变量1 INTEGER := ...
随机推荐
- Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式
背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前 ...
- Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节
在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...
- Javascript中变量函数申明优先级
先理解这句话: “函数会首先被提升,然后才是变量”,代码并不是你写的顺序那样执行的. F12把下面的代码粘贴到控制台执行一下: var getName = function () { console. ...
- OpenGL ES 3.0之顶点缓冲
所谓顶点缓冲就是直接将顶点数据存储在gpu的一段缓冲区,不需要从cpu拷贝到gpu.提高了程序的运行效率. 操作步骤 1.创建顶点缓冲对象 GLuint vertexBufferID; 2.分配空间 ...
- pdo_mysql安装不了或是安装后用不了的修复教程
目前发现wdOS及lanmp_wdcp的RPM包安装在部分系统下安装后,在安装pdo_mysql时无法安装或安装后无法使用的问题 如在安装时提示下如下 regenerate PHP parsers.c ...
- mysql function 中使用游标
create function p_t1(tid varchar(20))returns varchar(200)begin declare tmpName varchar(50) default ' ...
- Java并发容器——ConcurrentSkipListMap和ConcurrentHashMap
一:ConcurrentSkipListMap TreeMap使用红黑树按照key的顺序(自然顺序.自定义顺序)来使得键值对有序存储,但是只能在单线程下安全使用:多线程下想要使键值对按照key的顺序来 ...
- “Info.plist” couldn’t be removed
Showing All Messages error: failed to remove /Users/Rubert/Library/Developer/Xcode/DerivedData/Proje ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- Selenium测试Ajax应用程序中的主要问题
主要的问题可能就是页面加载的问题,有时候页面没有加载成功导致对象找不到,从而导致脚本不能运行. 主要使用的方式是,在测试的全局中设置一个页面的加载时间,如果timeout就是没有找到对象.如下参考: ...