Use BULK COLLECT and FORALL

New to Oracle8i—and improved in Oracle9i—are the BULK COLLECT and FORALL statements. Both share a common potential to dramatically reduce (by an order of magnitude or more) the time it takes to execute SQL operations that affect multiple rows of data. If you are currently executing SQL statements that operate on more than ten rows at a time, you should test the impact of FORALL and BULK COLLECT.

FORALL looks like a FOR statement, but it is not any kind of loop. Instead, it collects together a number of individual DML operations and sends them over to the SQL engine in a single pass for processing. Here is an example:

CREATE OR REPLACE PROCEDURE update_tragedies ( warcrim_ids IN name_varray, num_victims IN number_varray )ISBEGIN FORALL indx IN warcrim_ids.FIRST .. warcrim_ids.LAST UPDATE war_criminal SET victim_count = num_victims (indx) WHERE war_criminal_id = warcrim_ids (indx);END;

BULK COLLECT can be used inside both implicit and explicit fetch operations to retrieve more than one row in a single pass to the database. Here is an example:

DECLARE names name_varray; mileages number_varray;BEGIN SELECT name, mileage BULK COLLECT INTO names, mileages FROM transportation WHERE TYPE = 'AUTOMOBILE' AND mileage < 20; -- Now work with data in the collections ...END;

And, finally, here is an example of combing FORALL and BULK COLLECT in a single statement (with the latter used in the RETURNING clause):

CREATE OR REPLACE FUNCTION remove_emps_by_dept (deptlist dlist_t) RETURN enolist_tIS enolist enolist_t;BEGIN FORALL aDept IN deptlist.FIRST..deptlist.LAST DELETE FROM emp WHERE deptno IN deptlist(aDept) RETURNING empno BULK COLLECT INTO enolist; RETURN enolist;END;

As you can see, you need to be comfortable working with collections in order to take advantage of FORALL and BULK COLLECTION; Chapter 11 will give you all the information you need about these array-like data structures. See also Chapter 14 for more information on BULK COLLECT, and Chapter 13 for additional information on FORALL.

 

 

Part VI: Advanced PL/SQL Topics

A language as mature and rich as PL/SQL is packed full of features that you may not use on a day-to-day basis, but that may make the crucial difference between success and failure. This part of the book contains an exploration into the PL/SQL runtime architecture (Chapter 20), including PL/SQL's use of memory and differences between server-side and client-side PL/SQL. Chapter 21 offers an in-depth guide to using the object-oriented features of Oracle (object types and object views). Chapter 22 and Chapter 23 show you how to invoke Java and C code from your PL/SQL applications.

Chapter 20

Chapter 21

Chapter 22

Chapter 23

Chapter 20. PL/SQL's Runtime Architecture

As we discussed in Chapter 2, there are many different programmatic environments from which you can call a PL/SQL program, but there are only two places that you can find a PL/SQL runtime engine:

· Inside the Oracle database server

· On a client machine running Oracle's Developer tools such as Oracle Forms Builder or Oracle Reports Builder

An application developer has a number of packaging and design decisions to make, such as:

· Should I put code on the server, on the client, or both?

· What's better: an anonymous block, a top-level procedure, or a package?

· Should server-side code use conventional compilation or native compilation?

· Which client-side programs would be better stored in PL/SQL libraries rather than inside an Oracle Forms program?

You must also decide whether a program should run with the privileges of its owner (the definer rights model) or of the user who is running it (the invoker rights model).

Even to an experienced programmer, the "correct" answers to these questions are not always obvious. However, understanding something about the inner workings of the PL/SQL environment will help you make informed decisions. This chapter takes a closer look at PL/SQL's internal runtime architecture. Our goal is to take you beyond mere packaging decisions, and assist you in improving the design and performance of your applications in many ways.

While the primary emphasis of this chapter is on those aspects of PL/SQL that are of interest to the application developer, there are also many aspects of the runtime environment that are generally the purview of the database administrator. For example, the way the server assigns computer memory to the running program depends on whether you're running Oracle in shared or dedicated server mode. Because these differences can have a major impact on application tuning and performance, this chapter also highlights those areas that are of critical importance.