Programming With Collections PDF
Programming With Collections PDF
Collect Yourself:
Optimize PL/SQL Code with
Collections
Steven Feuerstein
PL/SQL Obsession
http://www.ToadWorld.com/SF
What is a collection?
1
abc
def
sf
...
22
23
rrr
swq
Serve up complex datasets of information to nonPL/SQL host environments using table functions.
Dramatically improve multi-row querying, inserting,
updating and deleting the contents of tables.
Combined with BULK COLLECT and FORALL....
Sparse
Data does not have to be stored in consecutive rows, as is
required in traditional 3GL arrays and VARRAYs.
collection_of_records.sql
nested_table_example.sql
About Varrays
Has a maximum size, associated with its type.
Can adjust the size at runtime in Oracle10g R2.
varray_example.sql
Library cache
Shared SQL
Reserved Pool
Pre-parsed
Large Pool
Session 1
emp_rec emp%rowtype;
tot_tab tottabtype;
Session 1 memory
(PGA/UGA)
Copyright 2000-2006 Steven Feuerstein - Page 13
Select *
from emp
calc_totals
Update emp
Set sal=...
show_emps
upd_salaries
emp_rec emp%rowtype;
tot_tab tottabtype;
Session 2 memory
(PGA/UGA)
Session 2
plsql_memory*.*
array_t1
array_t2
array_t3
array_t4
array_t5
array_t6
array_t7
IS
IS
IS
IS
IS
IS
IS
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
INDEX BY BINARY_INTEGER;
INDEX BY PLS_INTEGER;
INDEX BY POSITIVE;
INDEX BY NATURAL;
INDEX BY VARCHAR2(64);
INDEX BY VARCHAR2(32767);
INDEX BY
employee.last_name%TYPE;
TYPE array_t8 IS TABLE OF NUMBER INDEX BY
types_pkg.subtype_t;
OF
OF
OF
OF
OF
OF
OF
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
assoc_array_perf.tst
int_to_string_indexing.sql
genaa.sql
add_variable_declaration;
mark_varname_as_used;
END IF;
END LOOP;
Copyright 2000-2006 Steven Feuerstein - Page 18
string_tracker1.*
Copyright 2000-2006 Steven Feuerstein - Page 19
Oracle9i
Multi-level Collections
string_tracker3.*
Copyright 2000-2006 Steven Feuerstein - Page 22
Oracle10g
Oracle10g
10g_compare.sql
10g_compare2.sql
10g_compare_old.sql
Oracle10g
our_favorites :=
my_favorites MULTISET UNION DISTINCT dad_favorites;
our_favorites :=
my_favorites MULTISET INTERSECT dad_favorites;
our_favorites :=
dad_favorites MULTISET EXCEPT my_favorites;
END;
SQL: UNION
SQL: INTERSECT
SQL: MINUS
10g_setops.sql
10g_string_nt.sql
10g_favorites.sql
10g*union*.sql
Oracle10g
10g_set.sql
10g_favorites.pkg
Applying Collections
Data caching using packaged data
Turbo-charged SQL with BULK COLLECT
and FORALL
Table functions
I offer light coverage of these topics, simply
to ensure that you know what is possible.
Data retrieved
from cache
Pass Data
to Cache
Data returned
to application
Application
Database
Not in cache;
Request data
from database
PGA
Application
Requests Data
Subsequent accesses
Database
Function
Data retrieved
from cache
Data found in
cache. Database
is not needed.
Data returned
to application
Application
PGA
Function
Application
Requests Data
emplu.pkg
emplu.tst
11g_emplu*.*
bulk_rowcount.sql
bulkexc.sql
Declare a
collection of
records to hold
the queried data.
Fetch all rows into
collection
sequentially, starting
with 1.
DECLARE
TYPE employees_aat IS TABLE OF employees%ROWTYPE
INDEX BY BINARY_INTEGER;
l_employees employees_aat;
BEGIN
SELECT *
BULK COLLECT INTO l_employees
FROM employees;
FOR indx IN 1 .. l_employees.COUNT
LOOP
process_employee (l_employees(indx));
END LOOP;
END;
bulkcoll.sql
WARNING!
BULK COLLECT will not raise
NO_DATA_FOUND if no rows
are found.
bulklimit.sql
SELECT column_value
FROM TABLE (
lotsa_names ('Steven'
, 100)) names;
COLUMN_VALUE
-----------Steven 1
...
Steven 100
tabfunc_scalar.sql
tabfunc_streaming.sql
)
/
CREATE TABLE tickertable (
ticker VARCHAR2(20),
pricedate DATE,
pricetype VARCHAR2(1),
price NUMBER)
/
BEGIN
INSERT INTO tickertable
SELECT *
FROM TABLE (stockpivot (CURSOR (SELECT *
FROM stocktable)));
END;
/
Copyright 2000-2006 Steven Feuerstein - Page 41
tabfunc_streaming.sql
RETURN...nothing at
all!
tabfunc_setup.sql
tabfunc_pipelined.sql