PL/SQL Practices On BULK COLLECT limit

Best practices for knowing your LIMIT and kicking %NOTFOUND

I have started using BULK COLLECT whenever I need to fetch large volumes of data. This has caused me some trouble with my DBA, however. He is complaining that although my programs might be running much faster, they are also consuming way too much memory. He refuses to approve them for a production rollout. What's a programmer to do?

The most important thing to remember when you learn about and start to take advantage of features such as BULK COLLECT is that there is no free lunch. There is almost always a trade-off to be made somewhere. The tradeoff with BULK COLLECT, like so many other performance-enhancing features, is "run faster but consume more memory."

Specifically, memory for collections is stored in the program global area (PGA), not the system global area (SGA). SGA memory is shared by all sessions connected to Oracle Database, but PGA memory is allocated for each session. Thus, if a program requires 5MB of memory to populate a collection and there are 100 simultaneous connections, that program causes the consumption of 500MB of PGA memory, in addition to the memory allocated to the SGA.

Fortunately, PL/SQL makes it easy for developers to control the amount of memory used in a BULK COLLECT operation by using the LIMIT clause.

Suppose I need to retrieve all the rows from the employees table and then perform some compensation analysis on each row. I can use BULK COLLECT as follows:

PROCEDURE process_all_rows
IS
TYPE employees_aat
IS TABLE OF employees%ROWTYPE
INDEX BY PLS_INTEGER;
l_employees employees_aat;
BEGIN
SELECT *
BULK COLLECT INTO l_employees
FROM employees;
FOR indx IN 1 .. l_employees.COUNT
LOOP
analyze_compensation
(l_employees(indx));
END LOOP;
END process_all_rows;

 

Very concise, elegant, and efficient code. If, however, my employees table contains tens of thousands of rows, each of which contains hundreds of columns, this program can cause excessive PGA memory consumption.

Consequently, you should avoid this sort of "unlimited" use of BULK COLLECT. Instead, move the SELECT statement into an explicit cursor declaration and then use a simple loop to fetch many, but not all, rows from the table with each execution of the loop body, as shown in Listing 1.

Code Listing 1: Using BULK COLLECT with LIMIT clause

PROCEDURE process_all_rows (limit_in IN PLS_INTEGER DEFAULT 100)
IS
CURSOR employees_cur
IS
SELECT * FROM employees;
TYPE employees_aat IS TABLE OF employees_cur%ROWTYPE
INDEX BY PLS_INTEGER;
l_employees employees_aat;
BEGIN
OPEN employees_cur;
LOOP
FETCH employees_cur
BULK COLLECT INTO l_employees LIMIT limit_in;
FOR indx IN 1 .. l_employees.COUNT
LOOP
analyze_compensation (l_employees(indx));
END LOOP;
EXIT WHEN l_employees.COUNT < limit_in;
END LOOP;
CLOSE employees_cur;
END process_all_rows;

 

The process_all_rows procedure in Listing 1 requests that up to the value of limit_in rows be fetched at a time. PL/SQL will reuse the same limit_in elements in the collection each time the data is fetched and thus also reuse the same memory. Even if my table grows in size, the PGA consumption will remain stable.

How do you decide what number to use in the LIMIT clause? Theoretically, you will want to figure out how much memory you can afford to consume in the PGA and then adjust the limit to be as close to that amount as possible.

From tests I (and others) have performed, however, it appears that you will see roughly the same performance no matter what value you choose for the limit, as long as it is at least 25. The test_diff_limits.sql script, included with the sample code for this column, at otn.oracle.com/oramag/oracle/08-mar/o28plsql.zip, demonstrates this behavior, using the ALL_SOURCE data dictionary view on an Oracle Database 11g instance. Here are the results I saw (in hundredths of seconds) when fetching all the rows (a total of 470,000):

Elapsed CPU time for limit of 1 = 1839
Elapsed CPU time for limit of 5 = 716
Elapsed CPU time for limit of 25 = 539
Elapsed CPU time for limit of 50 = 545
Elapsed CPU time for limit of 75 = 489
Elapsed CPU time for limit of 100 = 490
Elapsed CPU time for limit of 1000 = 501
Elapsed CPU time for limit of 10000 = 478
Elapsed CPU time for limit of 100000 = 527

 

Kicking the %NOTFOUND Habit

I was very happy to learn that Oracle Database 10g will automatically optimize my cursor FOR loops to perform at speeds comparable to BULK COLLECT. Unfortunately, my company is still running on Oracle9i Database, so I have started converting my cursor FOR loops to BULK COLLECTs. I have run into a problem: I am using a LIMIT of 100, and my query retrieves a total of 227 rows, but my program processes only 200 of them. [The query is shown in Listing 2.] What am I doing wrong?

Code Listing 2: BULK COLLECT, %NOTFOUND, and missing rows

PROCEDURE process_all_rows
IS
CURSOR table_with_227_rows_cur
IS
SELECT * FROM table_with_227_rows;
TYPE table_with_227_rows_aat IS
TABLE OF table_with_227_rows_cur%ROWTYPE
INDEX BY PLS_INTEGER;
l_table_with_227_rows table_with_227_rows_aat;
BEGIN
OPEN table_with_227_rows_cur;
LOOP
FETCH table_with_227_rows_cur
BULK COLLECT INTO l_table_with_227_rows LIMIT 100;
EXIT WHEN table_with_227_rows_cur%NOTFOUND;     /* cause of missing rows */
FOR indx IN 1 .. l_table_with_227_rows.COUNT
LOOP
analyze_compensation (l_table_with_227_rows(indx));
END LOOP;
END LOOP;
CLOSE table_with_227_rows_cur;
END process_all_rows;

 

You came so close to a completely correct conversion from your cursor FOR loop to BULK COLLECT! Your only mistake was that you didn't give up the habit of using the %NOTFOUND cursor attribute in your EXIT WHEN clause.

The statement

EXIT WHEN
table_with_227_rows_cur%NOTFOUND;

 

makes perfect sense when you are fetching your data one row at a time. With BULK COLLECT, however, that line of code can result in incomplete data processing, precisely as you described.

Let's examine what is happening when you run your program and why those last 27 rows are left out. After opening the cursor and entering the loop, here is what occurs:

1. The fetch statement retrieves rows 1 through 100.
2. table_with_227_rows_cur%NOTFOUND evaluates to FALSE, and the rows are processed.
3. The fetch statement retrieves rows 101 through 200.
4. table_with_227_rows_cur%NOTFOUND evaluates to FALSE, and the rows are processed.
5. The fetch statement retrieves rows 201 through 227.
6. table_with_227_rows_cur%NOTFOUND evaluates to TRUE, and the loop is terminated—with 27 rows left to process!

Next Steps

 

READ more Best Practice PL/SQL

DOWNLOAD
Oracle Database 11g
sample code for this column

When you are using BULK COLLECT and collections to fetch data from your cursor, you should never rely on the cursor attributes to decide whether to terminate your loop and data processing.

So, to make sure that your query processes all 227 rows, replace this statement:

EXIT WHEN
table_with_227_rows_cur%NOTFOUND;
with
EXIT WHEN
l_table_with_227_rows.COUNT = 0;

 

Generally, you should keep all of the following in mind when working with BULK COLLECT:

  • The collection is always filled sequentially, starting from index value 1.
  • It is always safe (that is, you will never raise a NO_DATA_FOUND exception) to iterate through a collection from 1 to collection.COUNT when it has been filled with BULK COLLECT.
  • The collection is empty when no rows are fetched.
  • Always check the contents of the collection (with the COUNT method) to see if there are more rows to process.
  • Ignore the values returned by the cursor attributes, especially %NOTFOUND.
时间: 2024-10-25 18:10:01

PL/SQL Practices On BULK COLLECT limit的相关文章

浅谈PL/SQL批处理语句:BULK COLLECT与FORALL对优化做出的贡献_oracle

我们知道PL/SQL程序中运行SQL语句是存在开销的,因为SQL语句是要提交给SQL引擎处理这种在PL/SQL引擎和SQL引擎之间的控制转移叫做上下文却换,每次却换时,都有额外的开销 请看下图: 但是,FORALL和BULK COLLECT可以让PL/SQL引擎把多个上下文却换压缩成一个,这使得在PL/SQL中的要处理多行记录的SQL语句执行的花费时间骤降请再看下图: 下面详解这爷俩 ㈠ 通过BULK COLLECT 加速查询 ⑴ BULK COLLECT 的用法 采用BULK COLLECT可

【PL/SQL】初试 bulk collect

SQL> create table yang(last_name varchar2(20),first_name varchar2(10),salary number(10));   Table created Executed in 1.388 seconds SQL> begin   2  for i in 1000..100999 loop   3  insert into yang (last_name,first_name,salary) values('qilong'||(i-10

PostgreSQL Oracle 兼容性之 - PL/SQL FORALL, BULK COLLECT

Oracle PL/SQL 开发的童鞋,一定对O家的bulk批量处理的性能很是赞赏吧.但是PostgreSQL用户请不要垂涎,作为学院派和工业界的一颗璀璨明珠.开源数据库PostgreSQL,也有对应的批量处理策略哦,而且看起来性能完全不输Oracle.下面是一组LOOP和BULK的性能测试数据 一起来耍耍吧,先看看Oracle怎么耍的. Oracle PL/SQL FORALL, BULK COLLECT 为什么Oracle的PL/SQL过程语言需要bulk处理SQL,看一张图你就明白了,因为

PL/SQL --&amp;gt; 动态SQL

--==================== -- PL/SQL --> 动态SQL --====================         使用动态SQL是在编写PL/SQL过程时经常使用的方法之一.很多情况下,比如根据业务的需要,如果输入不同查询条件,则生成不同的执行 SQL查询语句,对于这种情况需要使用动态SQL来完成.再比如,对于分页的情况,对于不同的表,必定存在不同的字段,因此使用静态SQL则只 能针对某几个特定的表来形成分页.而使用动态的SQL,则可以对不同的表,不同的字段进行

pl/sql中bulk collect的用法

bulk collect可以将查询结果一次性地加载到collections中,而不用一条一条地处理.在select into,fetch into,returning into语句使用使用bulk collect时,所有的into变量都必须是collections. create table jy ( object_id number(12), object_name varchar2(20), object_type varchar2(20) ) 在select into语句中使用bulk c

批量SQL之 BULK COLLECT 子句

    BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNING INTO子句中使用BULK COLLECT.本文将逐一描述BULK COLLECT在这几种情形下的用法.    有关FORALL语句的用法请参考:批量SQL之 FORALL 语句   一.BULK COLLECT批量绑定的示例 --下面的示例中使用了BULK COLLECT将得到的结果集绑定

游标+bulk collect into limit的不同方法查询数据

如下需求想在查询表时先根据bulk collect into方式查询表中部分数据,然后处理,在查询表中其余数据知道查询出表中所有数据 如一个表有6条数据,第一次次批量取3条,显示后再批量取3条,已显示游标方式查询 具体构建环境 create TABLE TEST.COURSE (      COURSE_NO NUMBER(38),        DESCRIPTION VARCHAR2(50),        COST NUMBER(9,2),        PREREQUISITE NUMB

oracle下巧用bulk collect实现cursor批量fetch的sql语句_oracle

在一般的情况下,使用批量fetch的几率并不是很多,但是Oracle提供了这个功能我们最好能熟悉一下,说不定什么时候会用上它.  复制代码 代码如下: declare  cursor c1 is select * from t_depart;  v_depart t_depart%rowtype ;  type v_code_type is table of t_depart.depart_code%type ;  v_code v_code_type ;  type v_name_type i

PL/SQL游标(原创)

游标的相关概念及特性定义映射在结果集中某一行数据的具体位置,类似于C语言中的指针.即通过游标方式定位到结果集中某个特定的行,然后根据业务需求对该行进行相应特定的操作.游标的分类显示游标:即用户自定义游标,专门用于处理select语句返回的多行数据隐式游标:系统自动定义的游标,记录集只有单行数据,用于处理select into 和DML语句游标使用的一般过程:显示游标:声明, 打开, 读取, 关闭隐式游标:直接使用读取,声明.打开.关闭都是系统自动进行的显示游标的过程描述a.声明游标CURSOR