SlideShare a Scribd company logo
1
Advanced PL/SQL
Optimizing for Better Performance
111
Zohar Elkayam
CTO, Brillix
Zohar@Brillix.co.il
www.realdbamagic.com
Twitter: @realmgic
22
• Zohar Elkayam, COO/CTO at Brillix-DBAces
• Oracle ACE Associate
• DBA, team leader, Oracle University instructor, public
speaker, and a senior consultant for over 18 years
• Editor of ilDBA – Israel Database Community website
• Blogger – www.realdbamagic.com
Who am I?
3
We are committed to provide the highest quality of services
delivered by our dedicated team of highly trained and
experienced industry’s top data experts. We offer:
 Complete integrated end-to-end solutions based on best-of-
breed innovations in database, security and big data
technologies
 On-site professional customized trainings led by our team of
Oracle ACEs and Oracle Certified Professionals
 Comprehensive security solutions and services for leading
database platforms and business applications, leveraging a
world-class team of security experts
About Brillix
44
• Developing PL/SQL: Composite datatypes,
advanced cursors, dynamic SQL, tracing, and
more…
• Compiling PL/SQL: dependencies, optimization
levels, and DBMS_WARNING
• Tuning PL/SQL: GTT, Result cache and Memory
handling
• Oracle 11g and 12c new useful features
• SQLcl – New replacement tool for SQL*Plus
Agenda
5
The REAL Agenda
10:30-10:45‫הפסקה‬
12:30-13:30‫משתתפ‬ ‫לכל‬ ‫צהריים‬ ‫ארוחת‬‫הכנס‬ ‫י‬
15:00-15:15‫מתוקה‬ ‫הפסקה‬‫ומשובים‬
16:30‫הביתה‬ ‫הולכים‬
Developing PL/SQL
6
77
• Composite datatypes
• Advanced Cursors and Bulk operations
• Dynamic SQL and SQL Injection
• Autonomous Transactions
• 11g mentionable features
• 12c new development features
Developing PL/SQL
Composite Datatypes
8
9
• Collections
– Nested table, varray
– Associative arrays/PLSQL tables
• Use collections methods
• Manipulate collections
• Distinguish between the different types of
collections and when to use them
Composite Datatypes
10
• A collection is a group of elements, all of the
same type.
• Collections work like arrays.
• Collections can store instances of an object type and,
conversely, can be attributes of an object type.
• Types of collections in PL/SQL:
– Associative arrays
• String-indexed collections
• INDEX BY pls_integer or BINARY_INTEGER
– Nested tables
– Varrays
Understanding Collections
11
Collection Types
Nested table Varray
Associative array
1 2 3 4 5 6 a f i o t w
Index by
PLS_INTEGER
Index by
VARCHAR2
13
Associative arrays:
– That are indexed by strings can improve performance
– Are pure memory structures that are much faster than
schema-level tables
– Provide significant additional flexibility
Using Associative Arrays
Associative arrays
1 2 3 4 5 6 a f i o t w
Index by
PLS_INTEGER
Index by
VARCHAR2
15
Associative array in PL/SQL (string-indexed):
Creating the Array
TYPE type_name IS TABLE OF element_type
INDEX BY VARCHAR2(size)
CREATE OR REPLACE PROCEDURE report_credit
(p_last_name customers.cust_last_name%TYPE,
p_credit_limit customers.credit_limit%TYPE)
IS
TYPE typ_name IS TABLE OF customers%ROWTYPE
INDEX BY customers.cust_email%TYPE;
v_by_cust_email typ_name;
i VARCHAR2(30);
PROCEDURE load_arrays IS
BEGIN
FOR rec IN (SELECT * FROM customers WHERE cust_email IS NOT NULL)
LOOP
-- Load up the array in single pass to database table.
v_by_cust_email (rec.cust_email) := rec;
END LOOP;
END;
...
Create the string-indexed
associative array type.
Create the string-indexed
associative array variable.
Populate the string-indexed
associative array variable.
16
Traversing the Array
...
BEGIN
load_arrays;
i:= v_by_cust_email.FIRST;
dbms_output.put_line ('For credit amount of: ' || p_credit_limit);
WHILE i IS NOT NULL LOOP
IF v_by_cust_email(i).cust_last_name = p_last_name
AND v_by_cust_email(i).credit_limit > p_credit_limit
THEN dbms_output.put_line ( 'Customer '||
v_by_cust_email(i).cust_last_name || ': ' ||
v_by_cust_email(i).cust_email || ' has credit limit of: ' ||
v_by_cust_email(i).credit_limit);
END IF;
i := v_by_cust_email.NEXT(i);
END LOOP;
END report_credit;
/
EXECUTE report_credit('Walken', 1200)
For credit amount of: 1200
Customer Walken: Emmet.Walken@LIMPKIN.COM has credit limit of: 3600
Customer Walken: Prem.Walken@BRANT.COM has credit limit of: 3700
18
Nested table characteristics:
– A table within a table
– Unbounded
– Available in both SQL and PL/SQL as well as the database
– Array-like access to individual rows
Using Nested Tables
Nested table:
19
pOrder nested table:
Nested tables are stored out-of-line in storage tables.
Nested Table Storage
Storage table:
ORDID SUPPLIER REQUESTER ORDERED ITEMS
500 50 5000 30-OCT-07
800 80 8000 31-OCT-07
NESTED_TABLE_ID PRODID PRICE
55 555
56 566
57 577
NESTED_TABLE_ID PRODID PRICE
88 888
20
To create a nested table in the database:
To create a nested table in PL/SQL:
Creating Nested Tables
CREATE [OR REPLACE] TYPE type_name AS TABLE OF
Element_datatype [NOT NULL];
TYPE type_name IS TABLE OF element_datatype [NOT NULL];
21
– First, define an object type:
– Second, declare a column of that collection type:
Declaring Collections: Nested Table
CREATE TYPE typ_item AS OBJECT --create object
(prodid NUMBER(5),
price NUMBER(7,2) )
/
CREATE TYPE typ_item_nst -- define nested table type
AS TABLE OF typ_item
/
CREATE TABLE pOrder ( -- create database table
ordid NUMBER(5),
supplier NUMBER(5),
requester NUMBER(4),
ordered DATE,
items typ_item_nst)
NESTED TABLE items STORE AS item_stor_tab
/
1
2
3
22
Using Nested Tables
Add data to the nested table:
INSERT INTO pOrder
VALUES (500, 50, 5000, sysdate, typ_item_nst(
typ_item(55, 555),
typ_item(56, 566),
typ_item(57, 577)));
INSERT INTO pOrder
VALUES (800, 80, 8000, sysdate,
typ_item_nst (typ_item (88, 888)));
ORDID SUPPLIER REQUESTER ORDERED ITEMS
500 50 5000 30-OCT-07
800 80 8000 31-OCT-07
PRODID PRICE
55 555
56 566
57 577
PRODID PRICE
88 888
1
2
1
2
pOrder nested table
23
• Querying the results:
• Querying the results with the TABLE function:
Using Nested Tables
SELECT * FROM porder;
ORDID SUPPLIER REQUESTER ORDERED
---------- ---------- ---------- ---------
ITEMS(PRODID, PRICE)
-----------------------------------------------------------------
500 50 5000 31-OCT-07
TYP_ITEM_NST(TYP_ITEM(55, 555), TYP_ITEM(56, 566), TYP_ITEM(57, 577))
800 80 8000 31-OCT-07
TYP_ITEM_NST(TYP_ITEM(88, 888))
SELECT p2.ordid, p1.*
FROM porder p2, TABLE(p2.items) p1;
ORDID PRODID PRICE
---------- ---------- ----------
800 88 888
500 57 577
500 55 555
500 56 566
24
Use the collection name and a subscript to reference a
collection element:
– Syntax:
– Example:
– To reference a field in a collection:
Referencing Collection Elements
collection_name(subscript)
v_with_discount(i)
p_new_items(i).prodid
25
Using Nested Tables in PL/SQL
CREATE OR REPLACE PROCEDURE add_order_items
(p_ordid NUMBER, p_new_items typ_item_nst)
IS
v_num_items NUMBER;
v_with_discount typ_item_nst;
BEGIN
v_num_items := p_new_items.COUNT;
v_with_discount := p_new_items;
IF v_num_items > 2 THEN
--ordering more than 2 items gives a 5% discount
FOR i IN 1..v_num_items LOOP
v_with_discount(i) :=
typ_item(p_new_items(i).prodid,
p_new_items(i).price*.95);
END LOOP;
END IF;
UPDATE pOrder
SET items = v_with_discount
WHERE ordid = p_ordid;
END;
26
Using Nested Tables in PL/SQL
-- caller pgm:
DECLARE
v_form_items typ_item_nst:= typ_item_nst();
BEGIN
-- let's say the form holds 4 items
v_form_items.EXTEND(4);
v_form_items(1) := typ_item(1804, 65);
v_form_items(2) := typ_item(3172, 42);
v_form_items(3) := typ_item(3337, 800);
v_form_items(4) := typ_item(2144, 14);
add_order_items(800, v_form_items);
END;
PRODID PRICE
1804 65
3172 42
3337 800
2144 14
v_form_items variable
ORDID SUPPLIER REQUESTER ORDERED ITEMS
500 50 5000 30-OCT-07
800 80 8000 31-OCT-07
Resulting data in the pOrder nested table
PRODID PRICE
1804 61.75
3172 39.9
3337 760
2144 13.3
The prices are added
after discounts.
27
– To create a varray in the database:
– To create a varray in PL/SQL:
Understanding Varrays
Varray:
CREATE [OR REPLACE] TYPE type_name AS VARRAY
(max_elements) OF element_datatype [NOT NULL];
TYPE type_name IS VARRAY (max_elements) OF
element_datatype [NOT NULL];
28
CREATE TABLE department ( -- create database table
dept_id NUMBER(2),
name VARCHAR2(25),
budget NUMBER(12,2),
projects typ_ProjectList) -- declare varray as column
/
– First, define a collection type:
– Second, declare a collection of that type:
Declaring Collections: Varray
CREATE TYPE typ_Project AS OBJECT( --create object
project_no NUMBER(4),
title VARCHAR2(35),
cost NUMBER(12,2))
/
CREATE TYPE typ_ProjectList AS VARRAY (50) OF typ_Project
-- define VARRAY type
/
1
2
3
29
Add data to the table containing a varray column:
Using Varrays
INSERT INTO department
VALUES (10, 'Exec Admn', 30000000,
typ_ProjectList(
typ_Project(1001, 'Travel Monitor', 400000),
typ_Project(1002, 'Open World', 10000000)));
INSERT INTO department
VALUES (20, 'IT', 5000000,
typ_ProjectList(
typ_Project(2001, 'DB11gR2', 900000)));
1
2
DEPT_ID NAME BUDGET PROJECTS
PROJECT_NO TITLE COSTS
10 Exec Admn 30000000 1001 Travel Monitor 400000
1002 Open World 10000000
20 IT 5000000 2001 DB11gR2 900000
1
2
DEPARTMENT table
30
– Querying the results:
– Querying the results with the TABLE function:
Using Varrays
SELECT * FROM department;
DEPT_ID NAME BUDGET
---------- ------------------------- ----------
PROJECTS(PROJECT_NO, TITLE, COST)
-----------------------------------------------------------------
10 Executive Administration 30000000
TYP_PROJECTLIST(TYP_PROJECT(1001, 'Travel Monitor', 400000),
TYP_PROJECT(1002, 'Open World', 10000000))
20 Information Technology 5000000
TYP_PROJECTLIST(TYP_PROJECT(2001, 'DB11gR2', 900000))
SELECT d2.dept_id, d2.name, d1.*
FROM department d2, TABLE(d2.projects) d1;
DEPT_ID NAME PROJECT_NO TITLE COST
------- ------------------------ ---------- -------------- --------
10 Executive Administration 1001 Travel Monitor 400000
10 Executive Administration 1002 Open World 10000000
20 Information Technology 2001 DB11gR2 900000
31
• You can declare collections as the formal parameters of procedures
and functions.
• You can specify a collection type in the RETURN clause of a
function specification.
• Collections follow the usual scoping and instantiation rules.
Working with Collections in PL/SQL
CREATE OR REPLACE PACKAGE manage_dept_proj
AS
PROCEDURE allocate_new_proj_list
(p_dept_id NUMBER, p_name VARCHAR2, p_budget NUMBER);
FUNCTION get_dept_project (p_dept_id NUMBER)
RETURN typ_projectlist;
PROCEDURE update_a_project
(p_deptno NUMBER, p_new_project typ_Project,
p_position NUMBER);
FUNCTION manipulate_project (p_dept_id NUMBER)
RETURN typ_projectlist;
FUNCTION check_costs (p_project_list typ_projectlist)
RETURN boolean;
END manage_dept_proj;
34
Three ways to initialize:
– Use a constructor.
– Fetch from the database.
– Assign another collection variable directly.
Initializing Collections
PROCEDURE allocate_new_proj_list
(p_dept_id NUMBER, p_name VARCHAR2, p_budget NUMBER)
IS
v_accounting_project typ_projectlist;
BEGIN
-- this example uses a constructor
v_accounting_project :=
typ_ProjectList
(typ_Project (1, 'Dsgn New Expense Rpt', 3250),
typ_Project (2, 'Outsource Payroll', 12350),
typ_Project (3, 'Audit Accounts Payable',1425));
INSERT INTO department
VALUES(p_dept_id, p_name, p_budget, v_accounting_project);
END allocate_new_proj_list;
35
FUNCTION get_dept_project (p_dept_id NUMBER)
RETURN typ_projectlist
IS
v_accounting_project typ_projectlist;
BEGIN -- this example uses a fetch from the database
SELECT projects INTO v_accounting_project
FROM department WHERE dept_id = p_dept_id;
RETURN v_accounting_project;
END get_dept_project;
Initializing Collections
FUNCTION manipulate_project (p_dept_id NUMBER)
RETURN typ_projectlist
IS
v_accounting_project typ_projectlist;
v_changed_list typ_projectlist;
BEGIN
SELECT projects INTO v_accounting_project
FROM department WHERE dept_id = p_dept_id;
-- this example assigns one collection to another
v_changed_list := v_accounting_project;
RETURN v_changed_list;
END manipulate_project;
1
2
36
Referencing Collection Elements
-- sample caller program to the manipulate_project function
DECLARE
v_result_list typ_projectlist;
BEGIN
v_result_list := manage_dept_proj.manipulate_project(10);
FOR i IN 1..v_result_list.COUNT LOOP
dbms_output.put_line('Project #: '
||v_result_list(i).project_no);
dbms_output.put_line('Title: '||v_result_list(i).title);
dbms_output.put_line('Cost: ' ||v_result_list(i).cost);
END LOOP;
END;
Project #: 1001
Title: Travel Monitor
Cost: 400000
Project #: 1002
Title: Open World
Cost: 10000000
37
– EXISTS
– COUNT
– LIMIT
– FIRST and LAST
– PRIOR and NEXT
– EXTEND
– TRIM
– DELETE
Using Collection Methods
collection_name.method_name [(parameters)]
39
FUNCTION check_costs (p_project_list typ_projectlist)
RETURN boolean
IS
c_max_allowed NUMBER := 10000000;
i INTEGER;
v_flag BOOLEAN := FALSE;
BEGIN
i := p_project_list.FIRST ;
WHILE i IS NOT NULL LOOP
IF p_project_list(i).cost > c_max_allowed then
v_flag := TRUE;
dbms_output.put_line (p_project_list(i).title || '
exceeded allowable budget.');
RETURN TRUE;
END IF;
i := p_project_list.NEXT(i);
END LOOP;
RETURN null;
END check_costs;
Traverse collections with the following methods:
Using Collection Methods
40
Using Collection Methods
-- sample caller program to check_costs
set serverout on
DECLARE
v_project_list typ_projectlist;
BEGIN
v_project_list := typ_ProjectList(
typ_Project (1,'Dsgn New Expense Rpt', 3250),
typ_Project (2, 'Outsource Payroll', 120000),
typ_Project (3, 'Audit Accounts Payable',14250000));
IF manage_dept_proj.check_costs(v_project_list) THEN
dbms_output.put_line('Project rejected: overbudget');
ELSE
dbms_output.put_line('Project accepted, fill out forms.');
END IF;
END;
Audit Accounts Payable exceeded allowable budget.
Project rejected: overbudget
PROJECT_NO TITLE COSTS
1 Dsgn New Expense Rpt 3250
2 Outsource Payroll 120000
3 Audit Accounts Payable 14250000
V_PROJECT_LIST variable:
41
PROCEDURE update_a_project
(p_deptno NUMBER, p_new_project typ_Project, p_position NUMBER)
IS
v_my_projects typ_ProjectList;
BEGIN
v_my_projects := get_dept_project (p_deptno);
v_my_projects.EXTEND; --make room for new project
/* Move varray elements forward */
FOR i IN REVERSE p_position..v_my_projects.LAST - 1 LOOP
v_my_projects(i + 1) := v_my_projects(i);
END LOOP;
v_my_projects(p_position) := p_new_project; -- insert new one
UPDATE department SET projects = v_my_projects
WHERE dept_id = p_deptno;
END update_a_project;
Manipulating Individual Elements
42
Manipulating Individual Elements
-- check the table prior to the update:
SELECT d2.dept_id, d2.name, d1.*
FROM department d2, TABLE(d2.projects) d1;
DEPT_ID NAME PROJECT_NO TITLE COST
------- ------------------------- ---------- ----------------------------- --
10 Executive Administration 1001 Travel Monitor 400000
10 Executive Administration 1002 Open World 10000000
20 Information Technology 2001 DB11gR2 900000
-- caller program to update_a_project
BEGIN
manage_dept_proj.update_a_project(20,
typ_Project(2002, 'AQM', 80000), 2);
END;
DEPT_ID NAME PROJECT_NO TITLE COST
------- ------------------------- ---------- ----------------------------- --
10 Executive Administration 1001 Travel Monitor 400000
10 Executive Administration 1002 Open World 10000000
20 Information Technology 2001 DB11gR2 900000
20 Information Technology 2002 AQM 80000
-- check the table after the update:
SELECT d2.dept_id, d2.name, d1.*
FROM department d2, TABLE(d2.projects) d1;
43
Listing Characteristics for Collections
PL/SQL
Nested
Tables
DB
Nested
Tables
PL/SQL
Varrays
DB
Varrays
PL/SQL
Associative
Arrays
Maximum
size
No No Yes Yes Dynamic
Sparsity Can be No Dense Dense Yes
Storage N/A Stored out-of-
line
N/A Stored inline
(if < 4,000
bytes)
N/A
Ordering Does not
retain ordering
and subscripts
Does not
retain ordering
and subscripts
Retains
ordering and
subscripts
Retains
ordering and
subscripts
Retains
ordering and
subscripts
44
• Varrays involve fewer disk accesses and are more
efficient.
• Use nested tables for storing large amounts of data.
• Use varrays to preserve the order of elements in the
collection column.
• If you do not have a requirement to delete elements in
the middle of a collection, favor varrays.
• Varrays do not allow piecewise updates.
• After deleting the elements, release the unused
memory with
DBMS_SESSION.FREE_UNUSED_USER_MEMORY
Guidelines for Using Collections Effectively
Advanced Cursors
45
46
• Fetch into a record when fetching from a cursor.
• Benefit
– No individual variables declaration is needed.
– You can automatically use the structure of the SELECT
column list.
Cursor Design: Use Records
DECLARE
CURSOR cur_cust IS
SELECT customer_id, cust_last_name, cust_email
FROM customers WHERE credit_limit = 1200;
v_cust_record cur_cust%ROWTYPE;
BEGIN
OPEN cur_cust;
LOOP
FETCH cur_cust INTO v_cust_record;
...
47
• Reference implicit cursor attributes immediately after the SQL
statement executes.
• Benefit
– Doing so ensures that you are dealing with the result of the
correct SQL statement.
Guidelines for Good Cursor Design
BEGIN
UPDATE customers
SET credit_limit = p_credit_limit
WHERE customer_id = p_cust_id;
get_avg_order(p_cust_id); -- procedure call
IF SQL%NOTFOUND THEN
...
48
• Create cursors with parameters.
• Benefit
– Parameters increase the cursor’s flexibility and reusability.
– Parameters help avoid scoping problems.
Use Cursor Parameters
CURSOR cur_cust
(p_crd_limit NUMBER, p_acct_mgr NUMBER)
IS
SELECT customer_id, cust_last_name, cust_email
FROM customers
WHERE credit_limit = p_crd_limit
AND account_mgr_id = p_acct_mgr;
BEGIN
OPEN cur_cust(p_crd_limit_in, p_acct_mgr_in);
...
CLOSE cur_cust;
...
OPEN cur_cust(v_credit_limit, 145);
...
END;
49
– Simplify coding with cursor FOR loops.
– Benefit
• Reduces the volume of code
• Automatically handles the open, fetch, and close operations, and
defines a record type that matches the cursor definition
Using the Cursor For Loop
CREATE OR REPLACE PROCEDURE cust_pack
(p_crd_limit_in NUMBER, p_acct_mgr_in NUMBER)
IS
v_credit_limit NUMBER := 1500;
CURSOR cur_cust
(p_crd_limit NUMBER, p_acct_mgr NUMBER)
IS
SELECT customer_id, cust_last_name, cust_email
FROM customers WHERE credit_limit = p_crd_limit
AND account_mgr_id = p_acct_mgr;
BEGIN
FOR cur_rec IN cur_cust (p_crd_limit_in, p_acct_mgr_in)
LOOP -- implicit open and fetch
...
END LOOP; -- implicit close
...
END;
50
CREATE OR REPLACE PROCEDURE cust_list
IS
CURSOR cur_cust IS
SELECT customer_id, cust_last_name, credit_limit*1.1
FROM customers;
cust_record cur_cust%ROWTYPE;
BEGIN
OPEN cur_cust;
LOOP
FETCH cur_cust INTO cust_record;
DBMS_OUTPUT.PUT_LINE('Customer ' ||
cust_record.cust_last_name || ' wants credit '
|| cust_record.(credit_limit * 1.1));
EXIT WHEN cur_cust%NOTFOUND;
END LOOP;
...
• Make a DBA happy: Close a cursor when it is no longer needed.
• Use column aliases in cursors for calculated columns fetched into
records declared with %ROWTYPE.
More Guidelines for Good Cursor Design
Use col. alias
5151
• A Ref Cursor is a Cursor variable
• It hold a pointer to the result set of a
previously opened cursor
• The actual SQL statement of the cursor is
dynamic and determined at execution time
• A single Ref Cursor can point to different
result sets at different times
Returning Result Sets From PL/SQL
52
Memory
1 Southlake, Texas 1400
2 San Francisco 1500
3 New Jersey 1600
4 Seattle, Washington 1700
5 Toronto 1800
Cursor Variables: Overview
REF
CURSOR
memory
locator
53
Working with Cursor Variables
Define and
declare the
cursor
variable.
Open the
cursor
variable.
Fetch rows
from the
result set.
Close the
cursor
variable.
1 2 3 4
54
• Strong REF CURSOR:
– Is restrictive
– Specifies a RETURN type
– Associates only with type-compatible queries
– Is less error prone
• Weak REF CURSOR:
– Is nonrestrictive
– Associates with any query
– Is very flexible
Strong Versus Weak REF CURSOR Variables
55
DECLARE
TYPE rt_cust IS REF CURSOR
RETURN customers%ROWTYPE;
...
Define a REF CURSOR type:
– ref_type_name is a type specified in subsequent
declarations.
– return_type represents a record type.
– RETURN keyword indicates a strong cursor.
Step 1: Defining a REF CURSOR Type
TYPE ref_type_name IS REF CURSOR
[RETURN return_type];
56
Declare a cursor variable of a cursor type:
– cursor_variable_name is the name of the cursor
variable.
– ref_type_name is the name of a REF CURSOR type.
Step 1: Declaring a Cursor Variable
DECLARE
TYPE rt_cust IS REF CURSOR
RETURN customers%ROWTYPE;
cv_cust rt_cust;
cursor_variable_name ref_type_name;
57
Options:
– Use %TYPE and %ROWTYPE.
– Specify a user-defined record in the RETURN
clause.
– Declare the cursor variable as the formal
parameter of a stored procedure or function.
Step 1: Declaring a REF CURSOR Return Type
58
• Associate a cursor variable with a multiple-row
SELECT statement.
• Execute the query.
• Identify the result set:
– cursor_variable_name is the name of the
cursor variable.
– select_statement is the SQL SELECT
statement.
Step 2: Opening a Cursor Variable
OPEN cursor_variable_name
FOR select_statement;
60
• Retrieve rows from the result set one at a
time.
• The return type of the cursor variable must be
compatible with the variables named in the
INTO clause of the FETCH statement.
Step 3: Fetching from a Cursor Variable
FETCH cursor_variable_name
INTO variable_name1
[,variable_name2,. . .]
| record_name;
61
• Disable a cursor variable.
• The result set is undefined.
• Accessing the cursor variable after it is closed
raises the INVALID_CURSOR predefined
exception.
Step 4: Closing a Cursor Variable
CLOSE cursor_variable_name;
62
You can pass query result sets among PL/SQL-
stored subprograms and various clients.
Passing Cursor Variables as Arguments
Pointer
to the
result
set
Access by a host variable
on the client side
63
Passing Cursor Variables as Arguments
65
Using the SYS_REFCURSOR Predefined Type
CREATE OR REPLACE PROCEDURE REFCUR
(p_num IN NUMBER)
IS
refcur sys_refcursor;
empno emp.empno%TYPE;
ename emp.ename%TYPE;
BEGIN
IF p_num = 1 THEN
OPEN refcur FOR SELECT empno, ename FROM emp;
DBMS_OUTPUT.PUT_LINE('Employee# Name');
DBMS_OUTPUT.PUT_LINE('----- -------');
LOOP
FETCH refcur INTO empno, ename;
EXIT WHEN refcur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(empno || ' ' || ename);
END LOOP;
ELSE
....
SYS_REFCURSOR is a built-in
REF CURSOR type that allows
any result set to be associated
with it.
67
• You cannot use cursor variables with remote
subprograms on another server.
• You cannot use comparison operators to test
cursor variables.
• You cannot assign a null value to cursor variables.
• You cannot use REF CURSOR types in CREATE
TABLE or VIEW statements.
• Cursors and cursor variables are not
interoperable.
Rules for Cursor Variables
68
Cursor variables have the following benefits:
– Are dynamic and ensure more flexibility
– Are not tied to a single SELECT statement
– Hold the value of a pointer
– Can reduce network traffic
– Give access to query work areas after a
block completes
Comparing Cursor Variables
with Static Cursors
Ref Cursor Demo
69
7070
• Use explicit locking to deny access to other
sessions for the duration of a transaction
• Lock the rows before the update or delete
• Syntax:
Using the For Update Clause
SELECT ...
FROM ...
FOR UPDATE [OF column_reference][NOWAIT | WAIT n];
7171
• Select for Update default is to wait for locked
rows
• We can set the wait time to be limited with a
time frame or fail if rows are already locked
• We can also ask the query to skip the locked
rows and return the unlocked rows:
Handling Locked Rows
SELECT ...
FROM ...
FOR UPDATE SKIP LOCKED;
7272
• When using cursor with FOR UPDATE clause,
we might want to change that same record
• We can use the WHERE CURRENT OF cursor to
get a fast ROWID access to that row
Using WHERE CURRENT OF Clause
UPDATE employees
SET salary = 12000
WHERE CURRENT OF emp_cursor;
7373
• Include the RETURNING clause with an
INSERT, UPDATE, and DELETE to return column
values.
• Benefit:
– Eliminates the need to SELECT the row after DML
– Fewer network round trips
– Less server CPU time
– Fewer cursors
– Less server memory is required
Using the RETURNING Clause
7474
Using RETURNING Clause Example
DECLARE
v_emp_sal employees.salary%type;
BEGIN
UPDATE employees e
set salary = salary * 1.2
WHERE e.employee_id = 202
RETURNING e.salary into v_emp_sal;
dbms_output.put_line('The new salary is ' ||
v_emp_sal);
END;
/
The new salary is 7200
75
Use bulk binds to reduce context switches
between the PL/SQL engine and the SQL engine.
Using Bulk Binding
SQL enginePL/SQL run-time engine
PL/SQL block
FORALL j IN 1..1000
INSERT …
(OrderId(j),
OrderDate(j), …);
SQL
statement
executor
Procedural
statement
executor
76
Bind whole arrays of values simultaneously, rather
than looping to perform fetch, insert, update, and
delete on multiple rows.
– Instead of:
– Use:
Using Bulk Binding
...
FOR i IN 1 .. 50000 LOOP
INSERT INTO bulk_bind_example_tbl
VALUES(...);
END LOOP; ...
...
FORALL i IN 1 .. 50000
INSERT INTO bulk_bind_example_tbl
VALUES(...);
...
78
Use BULK COLLECT to improve performance:
Using Bulk Binding
CREATE OR REPLACE PROCEDURE process_customers
(p_account_mgr customers.account_mgr_id%TYPE)
IS
TYPE typ_numtab IS TABLE OF
customers.customer_id%TYPE;
TYPE typ_chartab IS TABLE OF
customers.cust_last_name%TYPE;
TYPE typ_emailtab IS TABLE OF
customers.cust_email%TYPE;
v_custnos typ_numtab;
v_last_names typ_chartab;
v_emails typ_emailtab;
BEGIN
SELECT customer_id, cust_last_name, cust_email
BULK COLLECT INTO v_custnos, v_last_names, v_emails
FROM customers
WHERE account_mgr_id = p_account_mgr;
...
END process_customers;
79
Use the RETURNING clause to retrieve information
about the rows that are being modified:
Using Bulk Binding
DECLARE
TYPE typ_replist IS VARRAY(100) OF NUMBER;
TYPE typ_numlist IS TABLE OF
orders.order_total%TYPE;
repids typ_replist :=
typ_replist(153, 155, 156, 161);
totlist typ_numlist;
c_big_total CONSTANT NUMBER := 60000;
BEGIN
FORALL i IN repids.FIRST..repids.LAST
UPDATE orders
SET order_total = .95 * order_total
WHERE sales_rep_id = repids(i)
AND order_total > c_big_total
RETURNING order_total BULK COLLECT INTO Totlist;
END;
81
• You can use the SAVE EXCEPTIONS keyword in your
FORALL statements:
• Exceptions raised during execution are saved in the
%BULK_EXCEPTIONS cursor attribute.
• The attribute is a collection of records with two fields:
– Note that the values always refer to the most recently executed FORALL
statement.
Using SAVE EXCEPTIONS
FORALL index IN lower_bound..upper_bound
SAVE EXCEPTIONS
{insert_stmt | update_stmt | delete_stmt}
Field Definition
ERROR_INDEX Holds the iteration of the FORALL statement where the
exception was raised
ERROR_CODE Holds the corresponding Oracle error code
82
Handling FORALL Exceptions
DECLARE
TYPE NumList IS TABLE OF NUMBER;
num_tab NumList :=
NumList(100,0,110,300,0,199,200,0,400);
bulk_errors EXCEPTION;
PRAGMA EXCEPTION_INIT (bulk_errors, -24381 );
BEGIN
FORALL i IN num_tab.FIRST..num_tab.LAST
SAVE EXCEPTIONS
DELETE FROM orders WHERE order_total < 500000/num_tab(i);
EXCEPTION WHEN bulk_errors THEN
DBMS_OUTPUT.PUT_LINE('Number of errors is: '
|| SQL%BULK_EXCEPTIONS.COUNT);
FOR j in 1..SQL%BULK_EXCEPTIONS.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE (
TO_CHAR(SQL%BULK_EXCEPTIONS(j).error_index) ||
' / ' ||
SQLERRM(-SQL%BULK_EXCEPTIONS(j).error_code) );
END LOOP;
END;
/
Dynamic SQL and SQL Injection
83
8484
• Used for two main reasons:
– Modify commands dynamically to retrieve or filter
columns based on input
– Run DDL and DCL commands from PL/SQL
– PLEASE don’t use it for running DMLs!
• Two main ways to run dynamic commands:
– EXECUTE IMMDIATE
– DBMS_SQL
Dynamic SQL
8585
• Easy syntax:
• Accepts all command types
• Accept bind variables with the USING clause
• Allows dynamic PL/SQL creation
Advantages of Native Dynamic SQL
EXECUTE_IMMEDIATE dynamic_string
{
INTO { define_variable [, define_variable ...] | record_name }
| BULK COLLECT INTO { collection_name [, collection_name ...] |
:host_array_name }
}
[ USING [ IN | OUT | IN OUT ] bind_argument
[, [ IN | OUT | IN OUT ] bind_argument] ... ] [ returning_clause ] ;
8686
• DBMS_SQL supports statements with unknown
number of inputs or outputs
• We can use DESCRIBE_COLUMNS procedure in
the DBMS_SQL package to describe columns for
a cursor opened/parsed through DBMS_SQL
• DBMS_SQL Supports SQL statements larger than
32 KB
• DBMS_SQL Lets you reuse SQL statements
Advantages of DBMS_SQL
8787
Using Execute Immediate
CREATE OR REPLACE PROCEDURE del_rows
(p_condition varchar2, p_rows_deld out number)
IS
BEGIN
EXECUTE IMMEDIATE 'DELETE FROM employees ' || p_condition;
p_rows_deld:=sql%ROWCOUNT;
END;
/
DECLARE
cnt number;
BEGIN
del_rows(‘where employee_id = 201’, cnt);
END;
/
8888
Using DBMS_SQL
CREATE OR REPLACE
PROCEDURE del_rows
(p_condition in varchar2, p_rows_del out number)
is
cursor_id integer;
BEGIN
cursor_id := dbms_sql.open_cursor;
dbms_sql.parse (cursor_id,'DELETE FROM employees ‘ ||p_condition,
dbms_sql.native);
p_rows_del := dbms_sql.execute (cursor_id);
dbms_sql.close_cursor (cursor_id);
END;
/
8989
• Starting Oracle 11g, we can transform a
DBMS_SQL cursor into a PL/SQL REF CURSOR
and vice versa
Transforming DBMS_SQL
Cursor into a REF CURSOR
DBMS_SQL.TO_REFCURSOR (cursor_number IN INTEGER) RETURN SYS_REFCURSOR;
DBMS_SQL.TO_CURSOR_NUMBER (rc IN OUT SYS_REFCURSOR) RETURN INTEGER;
9090
• Dynamic SQL is not checked until runtime
– Syntax
– Structure validity
– Permissions
• Dynamic SQL can be used as SQL injection
entry point
Disadvantages of Dynamic SQL
91
SQL injection is a technique for maliciously
exploiting applications that use client-supplied data
in SQL statements.
– Attackers trick the SQL engine into executing
unintended commands.
– SQL injection techniques may differ, but they all
exploit a single vulnerability in the application.
– To immunize your code against SQL injection attacks,
use bind arguments or validate and sanitize all input
concatenated to dynamic SQL.
Understanding SQL Injection
SQL Injection: Example
-- First order attack
CREATE OR REPLACE PROCEDURE GET_EMAIL
(p_last_name VARCHAR2 DEFAULT NULL)
AS
TYPE cv_custtyp IS REF CURSOR;
cv cv_custtyp;
v_email customers.cust_email%TYPE;
v_stmt VARCHAR2(400);
BEGIN
v_stmt := 'SELECT cust_email FROM customers
WHERE cust_last_name = '''|| p_last_name || '''';
DBMS_OUTPUT.PUT_LINE('SQL statement: ' || v_stmt);
OPEN cv FOR v_stmt;
LOOP
FETCH cv INTO v_email;
EXIT WHEN cv%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Email: '||v_email);
END LOOP;
CLOSE cv;
EXCEPTION WHEN OTHERS THEN
dbms_output.PUT_LINE(sqlerrm);
dbms_output.PUT_LINE('SQL statement: ' || v_stmt);
END;
String literals that are incorrectly
validated or not validated are
concatenated into a dynamic SQL
statement, and interpreted as code by th
SQL engine.
Assessing Vulnerability
Code
Uses bind
arguments for all
dynamic
components
Vulnerable
Safe
Contains dynamic
SQL?
Filters input
correctly?
No
No
No
Yes
Yes
YesLateral injection
vulnerability
No
94
Use the following strategies to reduce attack
surface:
– Use invoker’s rights.
– Reduce arbitrary inputs.
– Use Bind arguments.
– The Filter pitfall.
Reducing the Attack Surface
95
• Give out privileges appropriately.
• Run code with invoker’s rights when possible.
• Ensure that the database privilege model is
upheld when using definer’s rights.
Avoiding Privilege Escalation
Invoker’s rights
Definer’s rights
96
• Using invoker’s rights:
– Helps to limit the privileges
– Helps to minimize the security exposure.
• The following example does not use invoker's rights:
Using Invoker’s Rights
CREATE OR REPLACE
PROCEDURE change_password(p_username VARCHAR2 DEFAULT NULL,
p_new_password VARCHAR2 DEFAULT NULL)
IS
v_sql_stmt VARCHAR2(500);
BEGIN
v_sql_stmt := 'ALTER USER '||p_username ||' IDENTIFIED BY '
|| p_new_password;
EXECUTE IMMEDIATE v_sql_stmt;
END change_password;
GRANT EXECUTE ON change_password to OE, HR, SH;
1
2
Note the use of dynamic SQL with
concatenated input values.
97
• OE is successful at changing the SYS password,
because, by default, CHANGE_PASSWORD executes
with SYS privileges:
• Add the AUTHID to change the privileges to the invokers:
EXECUTE sys.change_password ('SYS', 'mine')
Using Invoker’s Rights
CREATE OR REPLACE
PROCEDURE change_password(p_username VARCHAR2 DEFAULT NULL,
p_new_password VARCHAR2 DEFAULT NULL)
AUTHID CURRENT_USER
IS
v_sql_stmt VARCHAR2(500);
BEGIN
v_sql_stmt := 'ALTER USER '||p_username ||' IDENTIFIED BY '
|| p_new_password;
EXECUTE IMMEDIATE v_sql_stmt;
END change_password;
98
• Reduce the end-user interfaces to only those that are actually
needed.
– In a Web application, restrict users to accessing specified Web
pages.
– In a PL/SQL API, expose only those routines that are intended for
customer use.
• Where user input is required, make use of language features
to ensure that only data of the intended type can be
specified.
– Do not specify a VARCHAR2 parameter when it will be used as a
number.
– Do not use numbers if you need only positive integers; use natural
instead.
Reducing Arbitrary Inputs
99
DBMS_ASSERT functions:
Understanding DBMS_ASSERT
Function Description
ENQUOTE_LITERAL Encloses string literal in single quotes
SIMPLE_SQL_NAME Verifies that the string is a simple SQL name
100
Formatting Oracle Identifiers
– Example 1: The object name used as an identifier:
SELECT count(*) records FROM orders;
– Example 2: The object name used as a literal:
SELECT num_rows FROM user_tables
WHERE table_name = 'ORDERS';
– Example 3: The object name used as a quoted (normal
format) identifier:
• The "orders" table referenced in example 3 is a different
table compared to the orders table in examples 1 and 2.
• It is vulnerable to SQL injection.
SELECT count(*) records FROM "orders";
101
– For your identifiers, determine:
1.Where will the input come from: user or data dictionary?
2.What verification is required?
3.How will the result be used, as an identifier or a literal
value?
– These three factors affect:
• What preprocessing is required (if any) prior to calling
the verification functions
• Which DBMS_ASSERT verification function is required
• What post-processing is required before the identifier
can actually be used
Working with Identifiers in Dynamic SQL
102
Avoiding Injection by Using
DBMS_ASSERT.ENQUOTE_LITERAL
CREATE OR REPLACE PROCEDURE Count_Rows(w in varchar2)
authid definer as
Quote constant varchar2(1) := '''';
Quote_Quote constant varchar2(2) := Quote||Quote;
Safe_Literal varchar2(32767) :=
Quote||replace(w,Quote,Quote_Quote)||Quote;
Stmt constant varchar2(32767) :=
'SELECT count(*) FROM t WHERE a='||
DBMS_ASSERT.ENQUOTE_LITERAL(Safe_Literal);
Row_Count number;
BEGIN
EXECUTE IMMEDIATE Stmt INTO Row_Count;
DBMS_OUTPUT.PUT_LINE(Row_Count||' rows');
END;
/
Verify whether the literal
is well-formed.
105
Avoiding Injection by Using
DBMS_ASSERT.SIMPLE_SQL_NAME
CREATE OR REPLACE PROCEDURE show_col2 (p_colname varchar2,
p_tablename varchar2)
AS
type t is varray(200) of varchar2(25);
Results t;
Stmt CONSTANT VARCHAR2(4000) :=
'SELECT '||dbms_assert.simple_sql_name( p_colname ) || ' FROM
'|| dbms_assert.simple_sql_name( p_tablename ) ;
BEGIN
DBMS_Output.Put_Line ('SQL Stmt: ' || Stmt);
EXECUTE IMMEDIATE Stmt bulk collect into Results;
for j in 1..Results.Count() loop
DBMS_Output.Put_Line(Results(j));
end loop;
END show_col2;
Verify that the input string conforms to
the basic characteristics of a simple
SQL name.
107
• Most common vulnerability:
– Dynamic SQL with string concatenation
• Your code design must:
– Avoid input string concatenation in dynamic SQL
– Use bind arguments, whether automatically via static
SQL or explicitly via dynamic SQL statements
Using Bind Arguments
108
• Filter parameter:
– P_WHERE_CLAUSE is a filter.
– It is difficult to protect against SQL injection.
• Prevention methods:
– Do not specify APIs that allow arbitrary query
parameters to be exposed.
– Any existing APIs with this type of functionality must
be deprecated and replaced with safe alternatives.
Beware of Filter Parameters
stmt := 'SELECT session_id FROM sessions
WHERE' || p_where_clause;
109109
• An Autonomous Transaction is an independent
Transaction started by another transaction.
• The main transaction will hold for the AT and wait
until it is completed
• Uses PRAGMA compiler directive
• Allowed in individual routines
• Commonly used for loggers, progress bars, and
concurrent operations
Autonomous Transactions
110110
AT Example: Main Transaction
DECLARE
tmp NUMBER;
BEGIN
FOR i IN 1..10
LOOP
tmp := i;
INSERT INTO t
VALUES (TRUNC(i/2));
END LOOP;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
logger('Failed on ' || TO_CHAR(tmp));
ROLLBACK;
RAISE;
END;
/
111111
AT Example: Logger (AT)
PROCEDURE logger (message IN VARCHAR2) IS
PRAGMA AUTONOMOUS_TRANSACTION
BEGIN
INSERT INTO logger VALUES (sysdate, message);
COMMIT;
END;
/
11g Useful Features
for Developers
112
113113
• For a specified measure, LISTAGG orders data
within each group specified in the ORDER BY
clause and then concatenates the values of
the measure column
• Limited to output of 4000 chars
The LISTAGG Function
LISTAGG(measure_expr [, 'delimiter'])
WITHIN GROUP (order_by_clause) [OVER
query_partition_clause]
114114
Using the LISTAGG Function Example
SELECT department_id "Dept", hire_date "Date“,
last_name "Name", LISTAGG(last_name, ', ')
WITHIN GROUP (ORDER BY hire_date, last_name)
OVER (PARTITION BY department_id) as "Emp_list"
FROM employees
WHERE hire_date < '01-SEP-2003'
ORDER BY "Dept", "Date", "Name";
115115
• Returns the N-th values in an ordered set of
values
• Different default window: RANGE BETWEEN
UNBOUNDED PRECEDING AND CURRENT
ROW
The NTH_VALUE Analytic Function
NTH_VALUE (measure_expr, n)
[ FROM { FIRST | LAST } ][ { RESPECT | IGNORE } NULLS ]
OVER (analytic_clause)
116116
Using NTH_VALUE Analytic Function
Example
SELECT prod_id, channel_id, MIN(amount_sold),
NTH_VALUE ( MIN(amount_sold), 2) OVER (PARTITION BY
prod_id ORDER BY channel_id
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING) nv
FROM sales
WHERE prod_id BETWEEN 13 and 16
GROUP BY prod_id, channel_id;
117117
• Virtual columns are dynamic (not stored) columns of a table (no
views required)
• Virtual columns obtain their value be evaluating as expression:
– Columns from the same table
– Constraints
– Function Calls (user defined)
• Might be used for
– Eliminate views
– Control table partitioning
– Manage “Binary” XMLType data
– Index values (function based index)
Virtual Columns
118118
Virtual Columns Example
column_name [datatype] [GENERATED ALWAYS] AS (expression) [VIRTUAL]
CREATE TABLE employees (
id NUMBER,
first_name VARCHAR2(10),
last_name VARCHAR2(10),
salary NUMBER(9,2),
comm1 NUMBER(3),
comm2 NUMBER(3),
salary1 AS (ROUND(salary*(1+comm1/100),2)),
salary2 NUMBER GENERATED ALWAYS AS
(ROUND(salary*(1+comm2/100),2)) VIRTUAL,
CONSTRAINT employees_pk PRIMARY KEY (id)
);
Trigger Improvements
119
120
• A single trigger on a table that allows you to
specify actions for each of the following four
timing points:
– Before the firing statement
– Before each row that the firing statement affects
– After each row that the firing statement affects
– After the firing statement
What Is a Compound Trigger?
121
• The compound trigger body supports a
common PL/SQL state that the code for each
timing point can access.
• The compound trigger common state is:
– Established when the triggering statement starts
– Destroyed when the triggering statement
completes
• A compound trigger has a declaration section
and a section for each of its timing points.
Working with Compound Triggers
122
• You can use compound triggers to:
– Program an approach where you want the actions
you implement for the various timing points to
share common data.
– Accumulate rows destined for a second table so
that you can periodically bulk-insert them
– Avoid the mutating-table error (ORA-04091)by
allowing rows destined for a second table to
accumulate and then bulk-inserting them
The Benefits of Using a Compound Trigger
123
• A compound trigger defined on a table has
one or more of the following timing-point
sections. Timing-point sections must appear in
the order shown in the table.
Timing-Point Sections of a
Table Compound Trigger
Timing Point Compound Trigger Section
Before the triggering statement executes BEFORE statement
After the triggering statement executes AFTER statement
Before each row that the triggering statement affects BEFORE EACH ROW
After each row that the triggering statement affects AFTER EACH ROW
124
Compound Trigger Structure for Tables
CREATE OR REPLACE TRIGGER schema.trigger
FOR dml_event_clause ON schema.table
COMPOUND TRIGGER
-- Initial section
-- Declarations
-- Subprograms
-- Optional section
BEFORE STATEMENT IS ...;
-- Optional section
BEFORE EACH ROW IS ...;
-- Optional section
AFTER EACH ROW IS ...;
-- Optional section
AFTER STATEMENT IS ...;
1
2
125
• A mutating table is:
– A table that is being modified by an UPDATE, DELETE, or
INSERT statement, or
– A table that might be updated by the effects of a DELETE
CASCADE constraint
• The session that issued the triggering statement cannot
query or modify a mutating table.
• This restriction prevents a trigger from seeing an
inconsistent set of data.
• This restriction applies to all triggers that use the FOR
EACH ROW clause.
• Views being modified in the INSTEAD OF triggers are not
considered mutating.
Trigger Restrictions on Mutating Tables
126
Using a Compound Trigger to
Resolve the Mutating Table Error
CREATE OR REPLACE TRIGGER check_salary
FOR INSERT OR UPDATE OF salary, job_id
ON employees
WHEN (NEW.job_id <> 'AD_PRES')
COMPOUND TRIGGER
TYPE salaries_t IS TABLE OF employees.salary%TYPE;
min_salaries salaries_t;
max_salaries salaries_t;
TYPE department_ids_t IS TABLE OF employees.department_id%TYPE;
department_ids department_ids_t;
TYPE department_salaries_t IS TABLE OF employees.salary%TYPE
INDEX BY VARCHAR2(80);
department_min_salaries department_salaries_t;
department_max_salaries department_salaries_t;
-- example continues on next slide
127
Using a Compound Trigger to Resolve
the Mutating Table Error
. . .
BEFORE STATEMENT IS
BEGIN
SELECT MIN(salary), MAX(salary), NVL(department_id, -1)
BULK COLLECT INTO min_Salaries, max_salaries, department_ids
FROM employees
GROUP BY department_id;
FOR j IN 1..department_ids.COUNT() LOOP
department_min_salaries(department_ids(j)) := min_salaries(j);
department_max_salaries(department_ids(j)) := max_salaries(j);
END LOOP;
END BEFORE STATEMENT;
AFTER EACH ROW IS
BEGIN
IF :NEW.salary < department_min_salaries(:NEW.department_id)
OR :NEW.salary > department_max_salaries(:NEW.department_id) THEN
RAISE_APPLICATION_ERROR(-20505,'New Salary is out of acceptable
range');
END IF;
END AFTER EACH ROW;
END check_salary;
128
• A compound trigger must be a DML trigger and defined on
either a table or a view
• An exception that occurs in one section must be handled in
that section. It cannot transfer control to another section
• :OLD and :NEW cannot appear in the declaration, BEFORE
STATEMENT, or the AFTER STATEMENT sections
• Only the BEFORE EACH ROW section can change the value of
:NEW
• The firing order of compound triggers is not guaranteed
unless you use the FOLLOWS clause
Compound Trigger Restrictions
129129
• To ensure that a trigger fires after certain
other triggers on the same object, use the
FOLLOWS clause
• Lets you order the executions of multiple
triggers relative to each other
• Applies to both compound and simple triggers
• Applies only to the section of the compound
trigger with the same timing point as the
simple trigger
FOLLOWS Clause
130130
• Consider two AFTER ROW ... FOR UPDATE triggers
defined on the same table. One trigger needs to
reference the :OLD value and the other trigger
needs to change the :OLD value. In this case, you
can use the FOLLOWS clause to order the firing
sequence
FOLLOWS Clause Example
CREATE OR REPLACE TRIGGER change_product
AFTER UPDATE of product_id ON order_items
FOR EACH ROW
FOLLOWS oe1.compute_total
BEGIN
dbms_output.put_line ('Do processing here…');
END;
Pivot and Unpivot
Turning things around!
131
PIVOT and UNPIVOT Clauses
of the SELECT Statement
• You can use the PIVOT operator of the
SELECT statement to write cross-tabulation
queries that rotate the column values into new
columns, aggregating data in the process.
• You can use the UNPIVOT operator of the
SELECT statement to rotate columns into
values of a column.
PIVOT UNPIVOT
133
Pivoting on the QUARTER
Column: Conceptual Example
30,000
40,000
60,000
30,000
40,000
20,000
AMOUNT_
SOLD
2,500Q1IUSAKids Jeans
2,000Q2CJapanKids Jeans
2,000Q3SUSAShorts
I
P
C
CHANNEL
Kids Jeans
Shorts
Shorts
PRODUCT
1,000Q2Germany
1,500Q4USA
Q2
QUARTER
2,500Poland
QUANTITY_
SOLD
COUNTRY
2,000
Q3
Kids Jeans
Shorts
PRODUCT
3,500
2,000
Q2
1,5002,500
Q4Q1
134134
• Pivoting the data before 11g was a complex
query which required the use of the CASE or
DECODE functions
Pivoting Before Oracle 11g
select product,
sum(case when quarter = 'Q1' then amount_sold else null end) Q1,
sum(case when quarter = 'Q2' then amount_sold else null end) Q2,
sum(case when quarter = 'Q3' then amount_sold else null end) Q3,
sum(case when quarter = 'Q4' then amount_sold else null end) Q4
from sales
group by product;
135
Pivot Clause Syntax
table_reference PIVOT [ XML ]
( aggregate_function ( expr ) [[AS] alias ]
[, aggregate_function ( expr ) [[AS] alias ] ]...
pivot_for_clause
pivot_in_clause )
-- Specify the column(s) to pivot whose values are to
-- be pivoted into columns.
pivot_for_clause =
FOR { column |( column [, column]... ) }
-- Specify the pivot column values from the columns you
-- specified in the pivot_for_clause.
pivot_in_clause =
IN ( { { { expr | ( expr [, expr]... ) } [ [ AS] alias] }...
| subquery | { ANY | ANY [, ANY]...} } )
137
Creating a New View: Example
CREATE OR REPLACE VIEW sales_view AS
SELECT
prod_name AS product,
country_name AS country,
channel_id AS channel,
SUBSTR(calendar_quarter_desc, 6,2) AS quarter,
SUM(amount_sold) AS amount_sold,
SUM(quantity_sold) AS quantity_sold
FROM sales, times, customers, countries, products
WHERE sales.time_id = times.time_id AND
sales.prod_id = products.prod_id AND
sales.cust_id = customers.cust_id AND
customers.country_id = countries.country_id
GROUP BY prod_name, country_name, channel_id,
SUBSTR(calendar_quarter_desc, 6, 2);
139
Selecting the SALES VIEW Data
SELECT product, country, channel, quarter, quantity_sold
FROM sales_view;
PRODUCT COUNTRY CHANNEL QUARTER QUANTITY_SOLD
------------ ------------ ---------- -------- -------------
Y Box Italy 4 01 21
Y Box Italy 4 02 17
Y Box Italy 4 03 20
. . .
Y Box Japan 2 01 35
Y Box Japan 2 02 39
Y Box Japan 2 03 36
Y Box Japan 2 04 46
Y Box Japan 3 01 65
. . .
Bounce Italy 2 01 34
Bounce Italy 2 02 43
. . .
9502 rows selected.
140
Pivoting the QUARTER Column
in the SH Schema: Example
SELECT *
FROM
(SELECT product, quarter, quantity_sold
FROM sales_view) PIVOT (sum(quantity_sold)
FOR quarter IN ('01', '02', '03', '04'))
ORDER BY product DESC;
. . .
142
Unpivoting the QUARTER Column:
Conceptual Example
2,000
Q3
Kids Jeans
Shorts
PRODUCT
3,500
2,000
Q2
1,5002,500
Q4Q1
2,500Q1Kids Jeans
2,000Q2Kids Jeans
3,500Q2Shorts
1,500Q4Kids Jeans
Q3
QUARTER
2,000Shorts
SUM_OF_QUANTITYPRODUCT
143143
• Univoting the data before 11g requires
multiple queries on the table using the
UNION ALL operator
Unpivoting Before Oracle 11g
SELECT *
FROM (
SELECT product, '01' AS quarter, Q1_value FROM sales
UNION ALL
SELECT product, '02' AS quarter, Q2_value FROM sales
UNION ALL
SELECT product, '03' AS quarter, Q3_value FROM sales
UNION ALL
SELECT product, '04' AS quarter, Q4_value FROM sales
);
144
• An UNPIVOT operation does not reverse a
PIVOT operation; instead, it rotates data found
in multiple columns of a single row into multiple
rows of a single column.
• If you are working with pivoted data, UNPIVOT
cannot reverse any aggregations that have been
made by PIVOT or any other means.
Using the UNPIVOT Operator
UNPIVOT
145
• The UNPIVOT clause rotates columns from a
previously pivoted table or a regular table into
rows. You specify:
– The measure column or columns to be unpivoted
– The name or names for the columns that result from
the UNPIVOT operation
– The columns that are unpivoted back into values of
the column specified in pivot_for_clause
• You can use an alias to map the column name to
another value.
Using the UNPIVOT Clause
146
UNPIVOT Clause Syntax
table_reference UNPIVOT [{INCLUDE|EXCLUDE} NULLS]
-- specify the measure column(s) to be unpivoted.
( { column | ( column [, column]... ) }
unpivot_for_clause
unpivot_in_clause )
-- Specify one or more names for the columns that will
-- result from the unpivot operation.
unpivot_for_clause =
FOR { column | ( column [, column]... ) }
-- Specify the columns that will be unpivoted into values of
-- the column specified in the unpivot_for_clause.
unpivot_in_clause =
( { column | ( column [, column]... ) }
[ AS { constant | ( constant [, constant]... ) } ]
[, { column | ( column [, column]... ) }
[ AS { constant | ( constant [, constant]...) } ] ]...)
147
Creating a New Pivot Table: Example
. . .
CREATE TABLE pivotedtable AS
SELECT *
FROM
(SELECT product, quarter, quantity_sold
FROM sales_view) PIVOT (sum(quantity_sold)
FOR quarter IN ('01' AS Q1, '02' AS Q2,
'03' AS Q3, '04' AS Q4));
SELECT * FROM pivotedtable
ORDER BY product DESC;
148
Unpivoting the QUARTER Column
in the SH Schema: Example
SELECT *
FROM pivotedtable
UNPIVOT (quantity_sold For Quarter IN (Q1, Q2, Q3, Q4))
ORDER BY product DESC, quarter;
. . .
Oracle 12c New Features
for Developers
149
150150
• Oracle 12c allows SQL VARCHAR2 to be the same
size as PL/SQL VARCHAR2
• This is not the default behavior and needs to be
turned on:
• Create table with 32k varchar2:
32K VARCHAR2/NVARCHAR2
ALTER SYSTEM set MAX_STRING_SIZE = EXTENDED scope = SPFILE;
CREATE TABLE Applicants
(id NUMBER GENERATED AS IDENTITY,
first_name varchar2(30),
last_name varchar2(30),
application date,
CV varchar2(32767)
);
151151
• Columns might be marked “invisible”
• The invisible column will not be available
unless explicitly named
• This is very useful when doing application
migration
Invisible Columns
152152
Invisible Column Example
CREATE TABLE tab1 (
id NUMBER,
description VARCHAR2(50) INVISIBLE
);
INSERT INTO tab1 VALUES (1);
SELECT * FROM tab1;
ID
----------
1
INSERT INTO tab1 (id, description) VALUES (2, 'TWO');
COMMIT;
SELECT id, description
FROM tab1;
ID DESCRIPTION
---------- ----------------------------------------------
1
2 TWO
153
• In previous releases, there was no direct
equivalent of the AutoNumber or Identity
functionality of other database engines
• This behavior had to be implemented using a
combination of sequences and trigger
• Oracle 12c introduces the ability to define an
identity clause against a table column defined
using a numeric type
• User should have the create sequence privilege
• Identity columns are always not null
Identity Column Type
154
• ALWAYS
– Forces the use of the identity. If an insert statement
references the identity column, an error is produced
• BY DEFAULT
– Allows using the identity if the column isn't referenced
in the insert statement. If the column is referenced,
the specified value will be used in place of the identity
• BY DEFAULT ON NULL
– Allows the identity to be used if the identity column is
referenced, but a value of NULL is specified
Identity Column Type – Options
Identity.sql
155
• You can specify only one identity column per table
• When specifying identity clause, you must specify a
numeric data type for datatype in the column
definition clause
• When specifying identity clause, you cannot specify
the DEFAULT clause in the column definition clause
• When specifying identity clause,
the NOT NULL constraint is implicitly specified
• CREATE TABLE AS SELECT will not inherit the identity
property on a column
Identity Column Type – Restrictions
156
• You can specify CURRVAL and NEXTVAL as default
values for a column
• Default value is used when the column is not
referenced by the insert or when the DEFAULT
keyword is used
• Gives you the ability to auto-populate master-
detail relationships
– Only makes sense if you can guarantee the inserts into
the detail table would always immediately follow the
insert into the master table
Default Value Using a Sequence
Default_with_Sequence.sql
157
• In an insert statement, when the column is
explicitly referenced, even when using the
value NULL, the default value is not used
• Oracle database 12c allows you to modify this
behavior using the ON NULL clause in the
default definition
Default Value on Explicit Nulls
Default_with_Null.sql
158
• You can define PL/SQL functions and procedures in the
WITH clause of a subquery and then use them as you
would any other built-in or user-defined function
• The “;” does not work as a terminator to the SQL
statement when the PL/SQL declaration is included in
the WITH clause
• Functions defined in the PL/SQL declaration section of
the WITH clause take precedence over objects with the
same name defined at the schema level
• Provides better performance as compared with schema
level functions
Calling PL/SQL from SQL
PLSQL_from_SQL.sql
159
Top-N Queries
• A Top-N query is used to retrieve the top or
bottom N rows from an ordered set
• Combining two Top-N queries gives you the
ability to page through an ordered set
• Oracle 12c has introduced the row limiting
clause to simplify Top-N queries
160160
• This is ANSI syntax
• The default offset is 0
• Null values in offset, rowcount or percent
will return no rows
Top-N in 12c
[ OFFSET offset { ROW | ROWS } ]
[ FETCH { FIRST | NEXT } [ { rowcount | percent PERCENT } ]
{ ROW | ROWS } { ONLY | WITH TIES } ]
161161
Top-N Examples
SELECT last_name, salary
FROM hr.employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS ONLY;
SELECT last_name, salary
FROM hr.employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS WITH TIES;
SELECT last_name, salary
FROM hr.employees
ORDER BY salary DESC
FETCH FIRST 10 PERCENT ROWS ONLY;
162162
• Before 12c we had to use the rownum pseudo
column to filter out rows
• That will require sorting the entire rowset
Paging Before 12c
SELECT val
FROM (SELECT val, rownum AS rnum
FROM (SELECT val
FROM rownum_order_test
ORDER BY val)
WHERE rownum <= 10)
WHERE rnum >= 5;
163163
• After 12c we have a syntax improvement for
paging using the Top-N queries
• This will use ROW_NUMBER and RANK in the
background – there is no real optimization
improvements
Paging in Oracle 12c
SELECT val
FROM rownum_order_test
ORDER BY val
OFFSET 4 ROWS FETCH NEXT 5 ROWS ONLY;
Pattern Matching in
Oracle 12c
164
165165
• Identify and group rows with consecutive values
• Consecutive in this regards – row after row
• Uses regular expression like syntax to find
patterns
What is Pattern Matching
166166
• Finding sequences of events in security
applications
• Locating dropped calls in a CDR listing
• Financial price behaviors (V-shape, W-shape
U-shape, etc.)
• Fraud detection and sensor data analysis
Common Business Challenges
167167
MATCH_RECOGNIZE Syntax
SELECT
FROM [row pattern input table]
MATCH_RECOGNIZE
( [ PARTITION BY <cols> ]
[ ORDER BY <cols> ]
[ MEASURES <cols> ]
[ ONE ROW PER MATCH | ALL ROWS PER MATCH ]
[ SKIP_TO_option]
PATTERN ( <row pattern> )
DEFINE <definition list>
)
169169
• Find Simple V-Shape with 1 row output per
match
MATCH_RECOGNIZE Example
SELECT *
FROM Ticker MATCH_RECOGNIZE (
PARTITION BY symbol
ORDER BY tstamp
MEASURES STRT.tstamp AS start_tstamp,
LAST(DOWN.tstamp) AS bottom_tstamp,
LAST(UP.tstamp) AS end_tstamp
ONE ROW PER MATCH
AFTER MATCH SKIP TO LAST UP
PATTERN (STRT DOWN+ UP+)
DEFINE
DOWN AS DOWN.price < PREV(DOWN.price),
UP AS UP.price > PREV(UP.price)
) MR
ORDER BY MR.symbol, MR.start_tstamp;
170170
What Will Be Matched?
171171
• Our goal: find uninterrupted sequences in a
book
• This can be useful for detecting missing
records or sequential behavior
Pages in a Book Example
172
Building Our Query
1. Define input
2. Pattern Matching
3. Order input
4. Process pattern
5. using defined conditions
6. Output: rows per match
7. Output: columns per row
8. Where to go after match?
SELECT *
FROM book_pages
MATCH_RECOGNIZE (
ORDER BY page
PATTERN (A B*)
DEFINE B AS page = PREV(page)+1
ONE ROW PER MATCH
MEASURES
A.page firstpage,
LAST(page) lastpage,
COUNT(*) cnt
AFTER MATCH SKIP PAST LAST ROW
);
SELECT *
FROM book_pages
MATCH_RECOGNIZE (
ORDER BY page
MEASURES
A.page firstpage,
LAST(page) lastpage,
COUNT(*) cnt
ONE ROW PER MATCH
AFTER MATCH SKIP PAST LAST ROW
PATTERN (A B*)
DEFINE B AS page = PREV(page)+1
);
173173173173
And The Output…
FIRSTPAGE LASTPAGE CNT
---------- ---------- ----------
1 3 3
5 7 3
10 15 6
42 42 1
174174
• Session-specific sequence
• Truncate CASCADE command
• Temporal Validity
• Temporary Undo
• Online DML Operations
• And tons of new features for DBAs too!
More 12c Developers’ Features…
175175
• More information and examples could be
found on my Blog:
http://www.realdbamagic.com/he/pivot-a-table/
http://www.realdbamagic.com/he/12c-top-n-query/
http://www.realdbamagic.com/he/with-pl-sql-oracle-
12c/
http://www.realdbamagic.com/he/session-level-
sequence-12c/
More Examples…
Compiling PL/SQL
176
177177
• Native and Interpreted Compilation
• Fine-grain Dependency management
• Code inlining
Compiling PL/SQL
178
Two compilation methods:
– Interpreted compilation
• Default compilation method
• Interpreted at run time
– Native compilation
• Compiles into native code
• Stored in the SYSTEM tablespace
Native and Interpreted Compilation
179
• Use the interpreted mode when (typically during
development):
– You are using a debugging tool, such as SQL Developer
– You need the code compiled quickly
• Use the native mode when (typically post
development):
– Your code is heavily PL/SQL based
– You are looking for increased performance in
production
Deciding on a Compilation Method
Native
Interpreted
180
• PLSQL_CODE_TYPE: Specifies the compilation mode for the
PL/SQL library units
• PLSQL_OPTIMIZE_LEVEL: Specifies the optimization level
to be used to compile the PL/SQL library units
• In general, for fastest performance, use the following setting:
Setting the Compilation Method
PLSQL_CODE_TYPE = { INTERPRETED | NATIVE }
PLSQL_OPTIMIZE_LEVEL = { 0 | 1 | 2 | 3}
PLSQL_CODE_TYPE = NATIVE
PLSQL_OPTIMIZE_LEVEL = 2
182
Use the USER|ALL|DBA_PLSQL_OBJECT_SETTINGS data
dictionary views to display the settings for a PL/SQL object:
Viewing the Compilation Settings
DESCRIBE ALL_PLSQL_OBJECT_SETTINGS
Name Null? Type
------------------------- -------- --------------------
OWNER NOT NULL VARCHAR2(30)
NAME NOT NULL VARCHAR2(30)
TYPE VARCHAR2(12)
PLSQL_OPTIMIZE_LEVEL NUMBER
PLSQL_CODE_TYPE VARCHAR2(4000)
PLSQL_DEBUG VARCHAR2(4000)
PLSQL_WARNINGS VARCHAR2(4000)
NLS_LENGTH_SEMANTICS VARCHAR2(4000)
PLSQL_CCFLAGS VARCHAR2(4000)
PLSCOPE_SETTINGS VARCHAR2(4000)
183
Viewing the Compilation Settings
SELECT name, plsql_code_type, plsql_optimize_level
FROM user_plsql_object_settings;
NAME PLSQL_CODE_TYP PLSQL_OPTIMIZE_LEVEL
-------------------- -------------- --------------------
ACTIONS_T INTERPRETED 2
ACTION_T INTERPRETED 2
ACTION_V INTERPRETED 2
ADD_ORDER_ITEMS INTERPRETED 2
CATALOG_TYP INTERPRETED 2
CATALOG_TYP INTERPRETED 2
CATALOG_TYP INTERPRETED 2
CATEGORY_TYP INTERPRETED 2
CATEGORY_TYP INTERPRETED 2
COMPOSITE_CATEGORY_TYP INTERPRETED 2
...
184
• This requires DBA privileges.
• The PLSQL_CODE_TYPE compilation
parameter must be set to NATIVE.
• The benefits apply to all the built-in PL/SQL
packages that are used for many database
operations.
Setting Up a Database for Native Compilation
ALTER SYSTEM SET PLSQL_CODE_TYPE = NATIVE;
185
Compiling a Program Unit for Native
Compilation
SELECT name, plsql_code_type, plsql_optimize_level
FROM user_plsql_object_settings
WHERE name = 'ADD_ORDER_ITEMS';
NAME PLSQL_CODE_T PLSQL_OPTIMIZE_LEVEL
---------------------- ------------ --------------------
ADD_ORDER_ITEMS INTERPRETED 2
ALTER SESSION SET PLSQL_CODE_TYPE = 'NATIVE';
ALTER PROCEDURE add_order_items COMPILE;
SELECT name, plsql_code_type, plsql_optimize_level
FROM user_plsql_object_settings
WHERE name = 'ADD_ORDER_ITEMS';
NAME PLSQL_CODE_T PLSQL_OPTIMIZE_LEVEL
---------------------- ------------ --------------------
ADD_ORDER_ITEMS NATIVE 2
1
2
3
4
186186
• We can turn on checking for certain warning conditions
• Warning messages can be issued during compilation of
PL/SQL subprograms (not for anonymous blocks )
• Use the SQL*Plus SHOW ERRORS command or query
the USER_ERRORS data dictionary view, to see any
warnings generated during compilation
• PL/SQL warning messages use the prefix PLW
• Use PLSQL_WARNINGS initialization parameter, or the
DBMS_WARNING package
PL/SQL Compile-Time Warnings
187187
• SEVERE: Messages for conditions that might cause
unexpected behavior or wrong results, such as aliasing
problems with parameters
• PERFORMANCE: Messages for conditions that might cause
performance problems, such as passing a VARCHAR2 value
to a NUMBER column in an INSERT statement.
• INFORMATIONAL: Messages for conditions that do not
have an effect on performance or correctness, but that you
might want to change to make the code more
maintainable, such as unreachable code that can never be
executed.
• All: refer to all warning messages
PL/SQL Warning Categories
188188
• Can be set at
– System level
– Session level
– Single compilation level
PLSQL_WARNINGS Parameter
ALTER SYSTEM SET PLSQL_WARNINGS='ENABLE:PERFORMANCE';
ALTER SESSION SET PLSQL_WARNINGS='DISABLE:ALL';
ALTER SESSION SET PLSQL_WARNINGS='ENABLE:SEVERE',
'DISABLE:PERFORMANCE', 'ERROR:07204';
ALTER PROCEDURE query_emp COMPILE
PLSQL_WARNINGS='ENABLE:ALL';
189189
• This warning means that the OTHERS handler
of PL/SQL subroutine can exit without
executing some form of RAISE or a call to the
standard RAISE_APPLICATION_ERROR
procedure.
• Good programming practices suggest that the
OTHERS handler should pass an exception
upward to avoid the risk of having exceptions
go unnoticed
PLW-06009 Warning Message
Fine Grain Dependency
Management
191
192
• Procedure A is a direct dependent of View B. View B is a direct
dependent of Table C. Procedure A is an indirect dependent of
Table C.
• Direct dependents are invalidated only by changes to the
referenced object that affect them.
• Indirect dependents can be invalidated by changes to the reference
object that do not affect them.
Invalidation of Dependent Objects
View B Table CProcedure A
193
• Before 11g, adding column D to table T invalidated the dependent
objects.
• Oracle Database 11g records additional, finer-grained dependency
management:
– Adding column D to table T does not impact view V and does not
invalidate the dependent objects
More Precise Dependency Metadata
in Oracle Database 11g
Procedure P Function FView V
Columns: A,B
Table T
Columns: A,B
Add column D
194
• In Oracle Database 11g, dependencies are now
tracked at the level of element within unit.
• Element-based dependency tracking covers the
following:
– Dependency of a single-table view on its base table
– Dependency of a PL/SQL program unit (package
specification, package body, or subprogram) on the
following:
• Other PL/SQL program units
• Tables
• Views
Fine-Grained Dependency Management
195
Fine-Grained Dependency Management:
Example 1
CREATE TABLE t2 (col_a NUMBER, col_b NUMBER, col_c NUMBER);
CREATE VIEW v AS SELECT col_a, col_b FROM t2;
ALTER TABLE t2 ADD (col_d VARCHAR2(20));
SELECT ud.name, ud.type, ud.referenced_name,
ud.referenced_type, uo.status
FROM user_dependencies ud, user_objects uo
WHERE ud.name = uo.object_name AND ud.name = 'V';
SELECT ud.name, ud.type, ud.referenced_name,
ud.referenced_type, uo.status
FROM user_dependencies ud, user_objects uo
WHERE ud.name = uo.object_name AND ud.name = 'V';
196
Fine-Grained Dependency Management:
Example 1
ALTER TABLE t2 MODIFY (col_a VARCHAR2(20));
SELECT ud.name, ud.referenced_name, ud.referenced_type,
uo.status
FROM user_dependencies ud, user_objects uo
WHERE ud.name = uo.object_name AND ud.name = 'V';
197
Fine-Grained Dependency Management:
Example 2
CREATE PACKAGE pkg IS
PROCEDURE proc_1;
END pkg;
/
CREATE OR REPLACE PROCEDURE p IS
BEGIN
pkg.proc_1();
END p;
/
CREATE OR REPLACE PACKAGE pkg
IS
PROCEDURE proc_1;
PROCEDURE unheard_of;
END pkg;
/
198
• To reduce invalidation of dependent objects:
Guidelines for Reducing Invalidation
Add new items to the
end of the package
Reference each table
through a view
199
• An object that is not valid when it is referenced
must be validated before it can be used.
• Validation occurs automatically when an object
is referenced; it does not require explicit user
action.
• If an object is not valid, its status is either
COMPILED WITH ERRORS,
UNAUTHORIZED, or INVALID.
Object Revalidation
Inlining
200
201
• Definition:
– Inlining is defined as the replacement of a call to
subroutine with a copy of the body of the subroutine
that is called.
– The copied procedure generally runs faster than the
original.
– The PL/SQL compiler can automatically find the calls
that should be inlined.
• Benefits:
– Inlining can provide large performance gains when
applied judiciously by a factor of 2–10 times.
Intra Unit Inlining
202
• Influence implementing inlining via two methods:
– Oracle parameter PLSQL_OPTIMIZE_LEVEL
– PRAGMA INLINE
• Recommend that you:
– Inline small programs
– Inline programs that are frequently executed
• Use performance tools to identify hot spots
suitable for inline applications:
– plstimer
Use of Inlining
203
• Noninlined program:
Inlining Concepts
CREATE OR REPLACE PROCEDURE small_pgm
IS
a NUMBER;
b NUMBER;
PROCEDURE touch(x IN OUT NUMBER, y NUMBER)
IS
BEGIN
IF y > 0 THEN
x := x*x;
END IF;
END;
BEGIN
a := b;
FOR I IN 1..10 LOOP
touch(a, -17);
a := a*b;
END LOOP;
END small_pgm;
204
• Examine the loop after inlining:
Inlining Concepts
...
BEGIN
a := b;
FOR i IN 1..10 LOOP
IF –17 > 0 THEN
a := a*a;
END IF;
a := a*b;
END LOOP;
END small_pgm;
...
205
• The loop is transformed in several steps:
Inlining Concepts
a := b;
FOR i IN 1..10 LOOP ...
IF false THEN
a := a*a;
END IF;
a := a*b;
END LOOP;
a := b;
FOR i IN 1..10 LOOP ...
a := a*b;
END LOOP;
a := b;
a := a*b;
FOR i IN 1..10 LOOP ...
END LOOP;
a := b*b;
FOR i IN 1..10 LOOP ...
END LOOP;
206
• Set the PLSQL_OPTIMIZE_LEVEL session-level
parameter to a value of 2 or 3:
– Setting it to 2 means no automatic inlining is attempted.
– Setting it to 3 means automatic inlining is attempted and
no pragmas are necessary.
• Within a PL/SQL subroutine, use PRAGMAINLINE
– NO means no inlining occurs regardless of the level and
regardless of the YES pragmas.
– YES means inline at level 2 of a particular call and increase
the priority of inlining at level 3 for the call.
Inlining: Example
ALTER PROCEDURE small_pgm COMPILE
PLSQL_OPTIMIZE_LEVEL = 3 REUSE SETTINGS;
207
• After setting the PLSQL_OPTIMIZE_LEVEL
parameter, use a pragma:
Inlining: Example
CREATE OR REPLACE PROCEDURE small_pgm
IS
a PLS_INTEGER;
FUNCTION add_it(a PLS_INTEGER, b PLS_INTEGER)
RETURN PLS_INTEGER
IS
BEGIN
RETURN a + b;
END;
BEGIN
pragma INLINE (small_pgm, 'YES');
a := add_it(3, 4) + 6;
END small_pgm;
208
• Pragmas apply only to calls in the next statement
following the pragma.
• Programs that make use of smaller helper
subroutines are good candidates for inlining.
• Only local subroutines can be inlined.
• You cannot inline an external subroutine.
• Cursor functions should not be inlined.
• Inlining can increase the size of a unit.
• Be careful about suggesting to inline functions
that are deterministic.
Inlining: Guidelines
Tuning PL/SQL
209
210210
• Handing PL/SQL in memory
• Global Temporary Tables (GTT)
• PL/SQL result cache
• Tips and Tricks
Tuning PL/SQL
211211
• Create packages that contain logically related
program units
• Reserve space for large allocations:
– Set the SHARED_POOL_RESERVED_SIZE
initialization parameter
• Prevent large or frequently used objects from
being aged out:
– Use the DBMS_SHARED_POOL package
Packages: Memory Issues
ORA-04031: unable to allocate 4160 bytes of shared memory..
212212
• Use dbms_shared_pool package:
• Flags:
– P – Package, Procedure or Function
– T – Type
– R – Trigger
– Q – Sequence
Pinning Objects
DBMS_SHARED_POOL.KEEP(object_name, flag)
DBMS_SHARED_POOL.UNKEEP(object_name, flag)
213213
• Memory is used more efficiently for scalability
(more users consume more memory)
• Package global memory is kept in the SGA
(instead of the UGA) and is reused for different
users
• Package global memory is only used within a unit
of work (a client-server call or a server to
different server call)
• Memory can be released and reused by another
user
Reusing Package Memory
Pragma SERIALLY_REUSABLE
214214
SERIALLY_REUSABLE - Example
CREATE OR REPLACE PACKAGE maintain_state
IS
pragma serially_reusable;
num1 number:= 0;
END maintain_state;
/
CREATE OR REPLACE PACKAGE regular_state
IS
num1 number:= 0;
END regular_state;
/
215215
SERIALLY_REUSABLE - Example
BEGIN
dbms_output.put_line(chr(10) || 'THE MAINTAIN PACKAGE');
dbms_output.put_line('Original Value: ' || maintain_state.num1);
maintain_state.num1 := maintain_state.num1 + 10;
dbms_output.put_line('New Value: ' || maintain_state.num1 || chr(10));
dbms_output.put_line('THE REGULAR PACKAGE');
dbms_output.put_line('Original Value: ' || regular_state.num1);
regular_state.num1 := regular_state.num1 + 10;
dbms_output.put_line('New Value: ' || regular_state.num1 || chr(10));
dbms_output.put_line(chr(10)||chr(10));
dbms_output.put_line('THE MAINTAIN PACKAGE');
dbms_output.put_line('Original Value: ' || maintain_state.num1);
maintain_state.num1 := maintain_state.num1 + 10;
dbms_output.put_line('New Value: ' || maintain_state.num1 || chr(10));
dbms_output.put_line('THE REGULAR PACKAGE');
dbms_output.put_line('Original Value: ' || regular_state.num1);
regular_state.num1 := regular_state.num1 + 10;
dbms_output.put_line('New Value: ' || regular_state.num1 || chr(10));
END;
/
216
SERIALLY_REUSABLE - Example
First Run Second Run
216
THE MAINTAIN PACKAGE
Original Value: 0
New Value: 10
THE REGULAR PACKAGE
Original Value: 0
New Value: 10
THE MAINTAIN PACKAGE
Original Value: 10
New Value: 20
THE REGULAR PACKAGE
Original Value: 10
New Value: 20
THE MAINTAIN PACKAGE
Original Value: 0
New Value: 10
THE REGULAR PACKAGE
Original Value: 20
New Value: 30
THE MAINTAIN PACKAGE
Original Value: 10
New Value: 20
THE REGULAR PACKAGE
Original Value: 30
New Value: 40
217217217217
• Since we’re giving up state managing, we can
now avoid ORA-4068 when compiling
• For more information, visit my blog:
http://www.realdbamagic.com/he/solving-ora-04068/
SERIALLY_REUSABLE – Side Effect
218
• The flexibility built into PL/SQL enables you to
pass:
– Simple scalar variables
– Complex data structures
• You can use the NOCOPY hint to improve
performance with the IN OUT parameters.
Passing Data Between PL/SQL Programs
219219
• The hint enables the PL/SQL compiler to pass
OUT and IN OUT parameters by reference, as
opposed to passing by value
• Enhances performance by reducing overhead
when passing parameters since less memory is
being used
• The Compiler will ignore the hint if it is not
possible to reference the original structure (type
conversion, constraints, for loop variable, etc.)
NOCOPY Hint
220220
NOCOPY - Example
CREATE OR REPLACE PACKAGE show_emp_pkg
IS
TYPE EmpTabTyp IS TABLE OF emp%ROWTYPE INDEX BY BINARY_INTEGER;
PROCEDURE show_emp (p_Deptno IN NUMBER,
p_EmpTab OUT NOCOPY EmpTabTyp);
END;
/
221
• Can be used in functions as an optimization
hint
• Indicates that a function can be used in a
parallelized query or parallelized DML
statement
Using the PARALLEL_ENABLE Hint
CREATE OR REPLACE FUNCTION f2 (p_p1 NUMBER)
RETURN NUMBER PARALLEL_ENABLE IS
BEGIN
RETURN p_p1 * 2;
END f2;
222222
• Large result sets can be stored in Table Variables or Temporary
Tables
• Temporary tables can be created to hold session-private data
that exists only for the duration of a transaction or session.
• Each session sees its own separate set of rows
• DML locks are not acquired on the data
• We can create indexes, views, and triggers on temporary tables
• Using temporary tables instead of program variables for large
• record sets, can reduce memory consumption
Global Temporary Tables
CREATE GLOBAL TEMPORARY TABLE hr.employees_temp
ON COMMIT PRSERVE ROWS;
Result Cache
223
224
– The result cache allows SQL query and PL/SQL function
results to be stored in cache memory.
– Subsequent executions of the same query or function can
be served directly out of the cache, improving response
times.
– This technique can be especially effective for SQL queries
and PL/SQL functions that are executed frequently.
– Cached query results become invalid when the database
data accessed by the query is modified.
What Is Result Caching?
Data dictionary
cache
Library
cache
SGA
Result
cache
Shared pool
225
• You can increase the small, default result cache
memory size by using the
RESULT_CACHE_MAX_SIZE initialization
parameter.
Increasing Result Cache Memory Size
SGA
Default
result
cache
Shared pool
Increased
result
cache
226
• Set Result_Cache_Max_Size from the command
line or in an initialization file created by a DBA.
• The cache size is dynamic and can be changed either
permanently or until the instance is restarted.
Setting Result_Cache_Max_Size
SQL> ALTER SYSTEM SET result_cache_max_size = 2M SCOPE =
MEMORY;
System altered.
SQL> SELECT name, value
2 FROM v$parameter
3 WHERE name = 'result_cache_max_size';
NAME VALUE
---------------------------------------- ------------------
result_cache_max_size 2097152
1 row selected.
227
• Use the RESULT_CACHE_MODE initialization
parameter in the database initialization
parameter file.
• RESULT_CACHE_MODE can be set to:
– MANUAL (default): You must add the RESULT_CACHE
hint to your queries for the results to be cached.
– FORCE: Results are always stored in the result
cache memory, if possible.
Enabling Query Result Cache
228
• Definition:
– Cache the results of the current query or query
fragment in memory, and then use the cached results
in future executions of the query or query fragments.
– Cached results reside in the result cache memory
portion of the SGA.
• Benefits:
– Improved performance
SQL Query Result Cache
229
• Scenario:
– You need to find the greatest average value of credit limit grouped by
state over the whole population.
– The query returns a large number of rows being analyzed to yield a
few or one row.
– In your query, the data changes fairly slowly (say every hour) but the
query is repeated fairly often (say every second).
• Solution:
– Use the new optimizer hint /*+ result_cache */ in your query:
SQL Query Result Cache
SELECT /*+ result_cache */
AVG(cust_credit_limit), cust_state_province
FROM sh.customers
GROUP BY cust_state_province;
230
Clearing the Shared Pool and Result Cache
--- flush.sql
--- Start with a clean slate. Flush the cache and shared
pool.
--- Verify that memory was released.
SET ECHO ON
SET FEEDBACK 1
SET SERVEROUTPUT ON
execute dbms_result_cache.flush
alter system flush shared_pool
/
231
– Definition:
• Enables data that is stored in cache to be shared across
sessions
• Stores the function result cache in an SGA, making it
available to any session that runs your application
– Benefits:
• Improved performance
• Improved scalability
PL/SQL Function Result Cache
232
• Scenario:
– You need a PL/SQL function that derives a complex metric.
– The data that your function calculates changes slowly, but
the function is frequently called.
• Solution:
– Use the new RESULT_CACHE clause in your function
definition.
– You can also have the cache purged when a dependent
table experiences a DML operation, by using the
RELIES_ON clause.
Marking PL/SQL Function Results to Be Cached
233
CREATE OR REPLACE FUNCTION ORD_COUNT(cust_no number)
RETURN NUMBER
RESULT_CACHE RELIES_ON (orders)
IS
V_COUNT NUMBER;
BEGIN
SELECT COUNT(*) INTO V_COUNT
FROM orders
WHERE customer_id = cust_no;
return v_count;
end;
• Include the RESULT_CACHE option in the function definition.
• Optionally, include the RELIES_ON clause.
Creating a PL/SQL Function Using the
RESULT_CACHE Clause
Specifies that the result
should be cached
Specifies the table upon which
the function relies
(not needed in 11.2+)
234
• Specify DETERMINISTIC to indicate that the
function returns the same result value whenever
it is called with the same values for its arguments.
• This helps the optimizer avoid redundant function
calls.
• If a function was called previously with the same
arguments, the optimizer can elect to use the
previous result.
• Do not specify DETERMINISTIC for a function
whose result depends on the state of session
variables or schema objects.
Using the DETERMINISTIC
Clause with Functions
235
Calling the PL/SQL Function Inside a Query
select cust_last_name, ord_count(customer_id) no_of_orders
from customers
where cust_last_name = 'MacGraw'
236
Verifying Memory Allocation
--- Establish the cache content
set serveroutput on
execute dbms_result_cache.memory_report
237
Viewing Cache Results Created
col name format a55
select * from v$result_cache_statistics
/
238
Calling the PL/SQL Function Again
select cust_last_name, ord_count(customer_id) no_of_orders
from customers
where cust_last_name = 'MacGraw'
239
Viewing Cache Results Found
col name format a55
select * from v$result_cache_statistics
/
240
Confirming That the Cached Result Was Used
select type, namespace,status, scan_count,name
from v$result_cache_objects
/
241241
• Beware of result caching of timed actions:
DBMS_LOCK.SLEEP will also be cached
overriding the sleep
• Cannot be used with invoker's rights or in an
anonymous block
• Cannot be used with pipelined table function
• Cannot be used with OUT or IN OUT
parameters.
PL/SQL Result Cache Pitfall
Tips and Tricks
242
243
You can tune your PL/SQL code by:
– Identifying the data type and constraint issues
• Data type conversion
• The NOT NULL constraint
• PLS_INTEGER
• SIMPLE_INTEGER
– Writing smaller executable sections of code
– Comparing SQL with PL/SQL
– Rephrasing conditional statements
Tuning PL/SQL Code
244
DECLARE
n NUMBER;
BEGIN
n := n + 15; -- converted
n := n + 15.0; -- not converted
...
END;
– PL/SQL performs implicit conversions between
structurally different data types.
– Example: When assigning a PLS_INTEGER
variable to a NUMBER variable
Avoiding Implicit Data Type Conversion
strings
dates
numbers
245
Understanding the NOT NULL Constraint
PROCEDURE calc_m IS
m NUMBER; --no constraint
...
BEGIN
m := a + b;
IF m IS NULL THEN
-- raise error
END IF;
END;
PROCEDURE calc_m IS
m NUMBER NOT NULL:=0;
a NUMBER;
b NUMBER;
BEGIN
m := a + b;
END;
The value of the expression a + b is
assigned to a temporary variable,
which is then tested for nullity.
A better way to check nullity; no
performance overhead
246
Use PLS_INTEGER when dealing with integer
data.
– It is an efficient data type for integer variables.
– It requires less storage than INTEGER or
NUMBER.
– Its operations use machine arithmetic, which is
faster than library arithmetic.
Using the PLS_INTEGER Data Type for Integers
247
– Definition:
• Is a predefined subtype
• Has the range –2147483648 .. 2147483648
• Does not include a null value
• Is allowed anywhere in PL/SQL where the PLS_INTEGER
data type is allowed
– Benefits:
• Eliminates the overhead of overflow
checking
• Is estimated to be 2–10 times faster
when compared with the PLS_INTEGER
type with native PL/SQL compilation
Using the SIMPLE_INTEGER Data Type
248
Each has its own benefits:
• SQL:
– Accesses data in the database
– Treats data as sets
• PL/SQL:
– Provides procedural capabilities
– Has more flexibility built into the language
Comparing SQL with PL/SQL
249
• Some simple set processing is markedly faster than the
equivalent PL/SQL.
• Avoid using procedural code when it may be better to use
SQL.
...FOR I IN 1..5600 LOOP
counter := counter + 1;
SELECT product_id, warehouse_id
INTO v_p_id, v_wh_id
FROM big_inventories WHERE v_p_id = counter;
INSERT INTO inventories2 VALUES(v_p_id, v_wh_id);
END LOOP;...
Comparing SQL with PL/SQL
BEGIN
INSERT INTO inventories2
SELECT product_id, warehouse_id
FROM main_inventories;
END;
250
If your business logic results in one condition being
true, use the ELSIF syntax for mutually exclusive
clauses:
Rephrasing Conditional
Control Statements
IF v_acct_mgr = 145 THEN
process_acct_145;
END IF;
IF v_acct_mgr = 147 THEN
process_acct_147;
END IF;
IF v_acct_mgr = 148 THEN
process_acct_148;
END IF;
IF v_acct_mgr = 149 THEN
process_acct_149;
END IF;
IF v_acct_mgr = 145
THEN
process_acct_145;
ELSIF v_acct_mgr = 147
THEN
process_acct_147;
ELSIF v_acct_mgr = 148
THEN
process_acct_148;
ELSIF v_acct_mgr = 149
THEN
process_acct_149;
END IF;
SQLcl Introduction
The Next Generation of SQL*Plus?
251
252252
• Introduced in Oracle 5 (1985)
• Looks very simple but has tight integration with
other Oracle infrastructure and tools
• Very good for reporting, scripting, and
automation
• Replaced old CLI tool called …
UFI (“User Friendly Interface”)
SQL*Plus
253253
• Nothing really wrong with SQL*Plus – it is being
updated constantly but it is missing a lot of
functionality
• SQL*Plus forces us to use GUI tools to complete
some basic tasks
• Easy to understand, a bit hard to use
• Not easy for new users or developers
What’s Wrong With SQL*Plus?
254254
• SQLcl is a new command line interface (CLI) for SQL
users and DBAs
• It is part of the SQL Developer suite – developed by the
same team: Oracle Database Development Tools Team
• Can do most of what SQL*Plus does and much more
• Minimal installation, minimal requirements
Introducing SQLcl
255255
• It’s still in the early adopter version (current
version: 4.2.0.15.295.1605 RC, October 23,
2015)
• Uses Java, one version for Windows, Linux and
OS X
• Planned to be shipped out with Oracle
Database 12cR2 (“within the next 12 month”)
Introducing SQLcl (cont.)
256256
• Early Adopter version/RC
• QA still logging bugs from SQL*Plus regression tests
• Adding support for existing SQL*Plus commands and
syntax
• Adding new commands
• But can it do...?
– Yes
– Not yet
– No
Current Status
257257
• Download from: SQL Developer Download
page
• Unzip the file
• Run it
Installing
What Can It Do?
258
259259
• Use the tab key to complete commands
• Can be used to list tables, views or other queriable
objects
• Can be used to replace the * with actual column names
• Use the arrow keys to move around the command
• Use CTRL+W and CTRL+S to jump to the beginning/end
of commands
Object Completion and Easy Edit
260260
• 100 command history buffer
• Commands are persistent between sessions (watch out for
security!)
• Use UP and DOWN arrow keys to access old commands
• Usage:
history
history usage
History script
history full
History clear [session?]
• Load from history into command buffer:
history <number>
Command History
261261
• Describe lists the column of the tables just like SQL*Plus
• Information shows column names, default values, indexes
and constraints.
• In 12c database information shows table statistics and In
memory status
• Works for table, views, sequences, and code objects
• Info+ shows additional information regarding column
statistics and column histograms
Describe, Information and info+
262262
• Outputting query results becomes easier with the
“set sqlformat” command (also available in SQL
Developer)
• We can create a query in the “regular” way and
then switch between the different output styles:
– ANSIConsole
– Fixed column size output
– XML or JSON output
– HTML output generates a built in search field and a responsive html output for
the result only
Generating Pretty Output
263263
• We can generate loader ready output (with “|” as a
delimiter)
• We can generate insert commands
• We can easily generate CSV output
• Usage:
set sqlformat {
csv,html,xml,json,ansiconsole,insert,
loader,fixed,default}
Generating Other Useful Outputs
264264
• Loads a comma separated value (csv) file into a
table
• The first row of the file must be a header row and
the file must be encoded UTF8
• The load is processed with 50 rows per batch
• Usage:
LOAD [schema.]table_name[@db_link] file_name
Load Data From CSV File
265265
• We talked about Developing PL/SQL: composite
datatypes, cursors, dynamic SQL
• We went over Oracle 11g and Oracle 12c
important features
• We looked at the compilation process of PL/SQL
and how to make it more efficient
• We talked about tuning our PL/SQL code and
some dedicated features for that
• SQLcl – New replacement tool for SQL*Plus
Summary
266266
• There are a lot of changes in Oracle 12c. We
only looked on a few developer oriented
features
• New XML and JSON handling functions
• Edition Based Redefinition
• Pipeline Table Functions and Parallel executions
• SQL Performance and new Optimizer changes
• Exceptions and Error handling
What Did We Not Talk About?
Q&AAny Questions? Now will be the time!
267
268268
Zohar Elkayam
twitter: @realmgic
Zohar@Brillix.co.il
www.ilDBA.co.il
www.realdbamagic.com
269269269269
Ad

More Related Content

What's hot (20)

MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
jkeriaki
 
Load Data Fast!
Load Data Fast!Load Data Fast!
Load Data Fast!
Karwin Software Solutions LLC
 
Oracle apex training
Oracle apex trainingOracle apex training
Oracle apex training
Vasudha India
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Zohar Elkayam
 
Oracle Apps - Forms
Oracle Apps - FormsOracle Apps - Forms
Oracle Apps - Forms
Bhaskara Reddy Sannapureddy
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
Mauro Pagano
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
Oracle Forms: create debug triggers
Oracle Forms: create debug triggersOracle Forms: create debug triggers
Oracle Forms: create debug triggers
Sekhar Byna
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architecture
Kevin Kline
 
[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning
PgDay.Seoul
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
Jafar Nesargi
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Svetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov - Database Transactions
Svetlin Nakov - Database Transactions
Svetlin Nakov
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
Hasan Kata
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
Srinath Maharana
 
Get to know PostgreSQL!
Get to know PostgreSQL!Get to know PostgreSQL!
Get to know PostgreSQL!
Oddbjørn Steffensen
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
Jeff Smith
 
PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuning
Federico Campoli
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
jkeriaki
 
Oracle apex training
Oracle apex trainingOracle apex training
Oracle apex training
Vasudha India
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Zohar Elkayam
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
Mauro Pagano
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
Oracle Forms: create debug triggers
Oracle Forms: create debug triggersOracle Forms: create debug triggers
Oracle Forms: create debug triggers
Sekhar Byna
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architecture
Kevin Kline
 
[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2020] SQL Tuning
PgDay.Seoul
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Svetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov - Database Transactions
Svetlin Nakov - Database Transactions
Svetlin Nakov
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
Hasan Kata
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
Jeff Smith
 
PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuning
Federico Campoli
 

Viewers also liked (18)

Presentation of Oracle database products for Beginners
Presentation of Oracle database products for BeginnersPresentation of Oracle database products for Beginners
Presentation of Oracle database products for Beginners
yazidaguedal
 
Programming Motherfucker
Programming MotherfuckerProgramming Motherfucker
Programming Motherfucker
Dusan Omercevic
 
Mc amca04919 plsql programs
Mc amca04919 plsql programsMc amca04919 plsql programs
Mc amca04919 plsql programs
Ashwin Kumar
 
Plsql les04
Plsql les04Plsql les04
Plsql les04
sasa_eldoby
 
plsql les06
 plsql les06 plsql les06
plsql les06
sasa_eldoby
 
Migrating to a New DBMS
Migrating to a New DBMSMigrating to a New DBMS
Migrating to a New DBMS
Stephen Conant
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
Tanu_Manu
 
plsql les01
 plsql les01 plsql les01
plsql les01
sasa_eldoby
 
SOA for PL/SQL Developer (OPP 2010)
SOA for PL/SQL Developer (OPP 2010)SOA for PL/SQL Developer (OPP 2010)
SOA for PL/SQL Developer (OPP 2010)
Lucas Jellema
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
Nawaz Sk
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
lubna19
 
Thaj oacle erp_project lead
Thaj oacle erp_project leadThaj oacle erp_project lead
Thaj oacle erp_project lead
Vajeerdeen Thajudeen
 
4 years of exp in PLSQL
4 years of exp in PLSQL4 years of exp in PLSQL
4 years of exp in PLSQL
prapulreddy kavva
 
Sql plsql online training
Sql plsql online trainingSql plsql online training
Sql plsql online training
enrollmy training
 
My resume
My resumeMy resume
My resume
Mysaiah gollamandala
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
Scott Wesley
 
pl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgepl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledge
Masood Khan
 
Resume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQL
Resume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQLResume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQL
Resume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQL
Singari Ranga Prasad
 
Presentation of Oracle database products for Beginners
Presentation of Oracle database products for BeginnersPresentation of Oracle database products for Beginners
Presentation of Oracle database products for Beginners
yazidaguedal
 
Programming Motherfucker
Programming MotherfuckerProgramming Motherfucker
Programming Motherfucker
Dusan Omercevic
 
Mc amca04919 plsql programs
Mc amca04919 plsql programsMc amca04919 plsql programs
Mc amca04919 plsql programs
Ashwin Kumar
 
Migrating to a New DBMS
Migrating to a New DBMSMigrating to a New DBMS
Migrating to a New DBMS
Stephen Conant
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
Tanu_Manu
 
SOA for PL/SQL Developer (OPP 2010)
SOA for PL/SQL Developer (OPP 2010)SOA for PL/SQL Developer (OPP 2010)
SOA for PL/SQL Developer (OPP 2010)
Lucas Jellema
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
Nawaz Sk
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
lubna19
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
Scott Wesley
 
pl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgepl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledge
Masood Khan
 
Resume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQL
Resume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQLResume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQL
Resume_Ranga_Prasad_2.7Exp_Oracle SQL PLSQL
Singari Ranga Prasad
 
Ad

Similar to Advanced PLSQL Optimizing for Better Performance (20)

PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme Performance
Zohar Elkayam
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
Er. Nawaraj Bhandari
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
ABAP Cheat sheet
ABAP Cheat sheetABAP Cheat sheet
ABAP Cheat sheet
ssuser24b056
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
Er. Nawaraj Bhandari
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
Lori Moore
 
Graph db as metastore
Graph db as metastoreGraph db as metastore
Graph db as metastore
Haris Khan
 
Oracle PL/SQL Collections | Learn PL/SQL
Oracle PL/SQL Collections | Learn PL/SQLOracle PL/SQL Collections | Learn PL/SQL
Oracle PL/SQL Collections | Learn PL/SQL
msora1
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
Nitesh Singh
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara Hakim
 
Day-09-Temp Table, Sub Query, View & Trigger.pptx
Day-09-Temp Table, Sub Query, View & Trigger.pptxDay-09-Temp Table, Sub Query, View & Trigger.pptx
Day-09-Temp Table, Sub Query, View & Trigger.pptx
joynulabeden2
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
arjun431527
 
Database Management Systems Lab manual (KR20) CSE.pdf
Database Management Systems Lab manual (KR20) CSE.pdfDatabase Management Systems Lab manual (KR20) CSE.pdf
Database Management Systems Lab manual (KR20) CSE.pdf
Anvesh71
 
HPD SQL Training - Beginner - 20220916.pptx
HPD SQL Training - Beginner - 20220916.pptxHPD SQL Training - Beginner - 20220916.pptx
HPD SQL Training - Beginner - 20220916.pptx
PatriceRochon1
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
SiddhantBhardwaj26
 
Oracle data integrator project
Oracle data integrator projectOracle data integrator project
Oracle data integrator project
Amit Sharma
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
Buck Woolley
 
Chap 7
Chap 7Chap 7
Chap 7
Karan Patil
 
[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10
AnusAhmad
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
prakashvs7
 
PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme Performance
Zohar Elkayam
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
Lori Moore
 
Graph db as metastore
Graph db as metastoreGraph db as metastore
Graph db as metastore
Haris Khan
 
Oracle PL/SQL Collections | Learn PL/SQL
Oracle PL/SQL Collections | Learn PL/SQLOracle PL/SQL Collections | Learn PL/SQL
Oracle PL/SQL Collections | Learn PL/SQL
msora1
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
Dimara Hakim
 
Day-09-Temp Table, Sub Query, View & Trigger.pptx
Day-09-Temp Table, Sub Query, View & Trigger.pptxDay-09-Temp Table, Sub Query, View & Trigger.pptx
Day-09-Temp Table, Sub Query, View & Trigger.pptx
joynulabeden2
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
arjun431527
 
Database Management Systems Lab manual (KR20) CSE.pdf
Database Management Systems Lab manual (KR20) CSE.pdfDatabase Management Systems Lab manual (KR20) CSE.pdf
Database Management Systems Lab manual (KR20) CSE.pdf
Anvesh71
 
HPD SQL Training - Beginner - 20220916.pptx
HPD SQL Training - Beginner - 20220916.pptxHPD SQL Training - Beginner - 20220916.pptx
HPD SQL Training - Beginner - 20220916.pptx
PatriceRochon1
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
SiddhantBhardwaj26
 
Oracle data integrator project
Oracle data integrator projectOracle data integrator project
Oracle data integrator project
Amit Sharma
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
Buck Woolley
 
[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10
AnusAhmad
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
prakashvs7
 
Ad

More from Zohar Elkayam (20)

Docker Concepts for Oracle/MySQL DBAs and DevOps
Docker Concepts for Oracle/MySQL DBAs and DevOpsDocker Concepts for Oracle/MySQL DBAs and DevOps
Docker Concepts for Oracle/MySQL DBAs and DevOps
Zohar Elkayam
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniques
Zohar Elkayam
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
Zohar Elkayam
 
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Zohar Elkayam
 
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)
Zohar Elkayam
 
Oracle 12c New Features For Better Performance
Oracle 12c New Features For Better PerformanceOracle 12c New Features For Better Performance
Oracle 12c New Features For Better Performance
Zohar Elkayam
 
Introduction to Oracle Data Guard Broker
Introduction to Oracle Data Guard BrokerIntroduction to Oracle Data Guard Broker
Introduction to Oracle Data Guard Broker
Zohar Elkayam
 
Exploring Oracle Multitenant in Oracle Database 12c
Exploring Oracle Multitenant in Oracle Database 12cExploring Oracle Multitenant in Oracle Database 12c
Exploring Oracle Multitenant in Oracle Database 12c
Zohar Elkayam
 
Things Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Things Every Oracle DBA Needs To Know About The Hadoop EcosystemThings Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Things Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Zohar Elkayam
 
Rapid Cluster Computing with Apache Spark 2016
Rapid Cluster Computing with Apache Spark 2016Rapid Cluster Computing with Apache Spark 2016
Rapid Cluster Computing with Apache Spark 2016
Zohar Elkayam
 
Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)
Zohar Elkayam
 
MySQL 5.7 New Features for Developers
MySQL 5.7 New Features for DevelopersMySQL 5.7 New Features for Developers
MySQL 5.7 New Features for Developers
Zohar Elkayam
 
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic FunctionsOOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Is SQLcl the Next Generation of SQL*Plus?
Is SQLcl the Next Generation of SQL*Plus?Is SQLcl the Next Generation of SQL*Plus?
Is SQLcl the Next Generation of SQL*Plus?
Zohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Things Every Oracle DBA Needs to Know about the Hadoop Ecosystem
Things Every Oracle DBA Needs to Know about the Hadoop EcosystemThings Every Oracle DBA Needs to Know about the Hadoop Ecosystem
Things Every Oracle DBA Needs to Know about the Hadoop Ecosystem
Zohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
Zohar Elkayam
 
The Hadoop Ecosystem for Developers
The Hadoop Ecosystem for DevelopersThe Hadoop Ecosystem for Developers
The Hadoop Ecosystem for Developers
Zohar Elkayam
 
Big data for cio 2015
Big data for cio 2015Big data for cio 2015
Big data for cio 2015
Zohar Elkayam
 
Docker Concepts for Oracle/MySQL DBAs and DevOps
Docker Concepts for Oracle/MySQL DBAs and DevOpsDocker Concepts for Oracle/MySQL DBAs and DevOps
Docker Concepts for Oracle/MySQL DBAs and DevOps
Zohar Elkayam
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniques
Zohar Elkayam
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
Zohar Elkayam
 
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Zohar Elkayam
 
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem (c17lv version)
Zohar Elkayam
 
Oracle 12c New Features For Better Performance
Oracle 12c New Features For Better PerformanceOracle 12c New Features For Better Performance
Oracle 12c New Features For Better Performance
Zohar Elkayam
 
Introduction to Oracle Data Guard Broker
Introduction to Oracle Data Guard BrokerIntroduction to Oracle Data Guard Broker
Introduction to Oracle Data Guard Broker
Zohar Elkayam
 
Exploring Oracle Multitenant in Oracle Database 12c
Exploring Oracle Multitenant in Oracle Database 12cExploring Oracle Multitenant in Oracle Database 12c
Exploring Oracle Multitenant in Oracle Database 12c
Zohar Elkayam
 
Things Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Things Every Oracle DBA Needs To Know About The Hadoop EcosystemThings Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Things Every Oracle DBA Needs To Know About The Hadoop Ecosystem
Zohar Elkayam
 
Rapid Cluster Computing with Apache Spark 2016
Rapid Cluster Computing with Apache Spark 2016Rapid Cluster Computing with Apache Spark 2016
Rapid Cluster Computing with Apache Spark 2016
Zohar Elkayam
 
Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)
Zohar Elkayam
 
MySQL 5.7 New Features for Developers
MySQL 5.7 New Features for DevelopersMySQL 5.7 New Features for Developers
MySQL 5.7 New Features for Developers
Zohar Elkayam
 
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic FunctionsOOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Is SQLcl the Next Generation of SQL*Plus?
Is SQLcl the Next Generation of SQL*Plus?Is SQLcl the Next Generation of SQL*Plus?
Is SQLcl the Next Generation of SQL*Plus?
Zohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Things Every Oracle DBA Needs to Know about the Hadoop Ecosystem
Things Every Oracle DBA Needs to Know about the Hadoop EcosystemThings Every Oracle DBA Needs to Know about the Hadoop Ecosystem
Things Every Oracle DBA Needs to Know about the Hadoop Ecosystem
Zohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
Zohar Elkayam
 
The Hadoop Ecosystem for Developers
The Hadoop Ecosystem for DevelopersThe Hadoop Ecosystem for Developers
The Hadoop Ecosystem for Developers
Zohar Elkayam
 
Big data for cio 2015
Big data for cio 2015Big data for cio 2015
Big data for cio 2015
Zohar Elkayam
 

Recently uploaded (20)

Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 

Advanced PLSQL Optimizing for Better Performance

  • 1. 1 Advanced PL/SQL Optimizing for Better Performance 111 Zohar Elkayam CTO, Brillix [email protected] www.realdbamagic.com Twitter: @realmgic
  • 2. 22 • Zohar Elkayam, COO/CTO at Brillix-DBAces • Oracle ACE Associate • DBA, team leader, Oracle University instructor, public speaker, and a senior consultant for over 18 years • Editor of ilDBA – Israel Database Community website • Blogger – www.realdbamagic.com Who am I?
  • 3. 3 We are committed to provide the highest quality of services delivered by our dedicated team of highly trained and experienced industry’s top data experts. We offer:  Complete integrated end-to-end solutions based on best-of- breed innovations in database, security and big data technologies  On-site professional customized trainings led by our team of Oracle ACEs and Oracle Certified Professionals  Comprehensive security solutions and services for leading database platforms and business applications, leveraging a world-class team of security experts About Brillix
  • 4. 44 • Developing PL/SQL: Composite datatypes, advanced cursors, dynamic SQL, tracing, and more… • Compiling PL/SQL: dependencies, optimization levels, and DBMS_WARNING • Tuning PL/SQL: GTT, Result cache and Memory handling • Oracle 11g and 12c new useful features • SQLcl – New replacement tool for SQL*Plus Agenda
  • 5. 5 The REAL Agenda 10:30-10:45‫הפסקה‬ 12:30-13:30‫משתתפ‬ ‫לכל‬ ‫צהריים‬ ‫ארוחת‬‫הכנס‬ ‫י‬ 15:00-15:15‫מתוקה‬ ‫הפסקה‬‫ומשובים‬ 16:30‫הביתה‬ ‫הולכים‬
  • 7. 77 • Composite datatypes • Advanced Cursors and Bulk operations • Dynamic SQL and SQL Injection • Autonomous Transactions • 11g mentionable features • 12c new development features Developing PL/SQL
  • 9. 9 • Collections – Nested table, varray – Associative arrays/PLSQL tables • Use collections methods • Manipulate collections • Distinguish between the different types of collections and when to use them Composite Datatypes
  • 10. 10 • A collection is a group of elements, all of the same type. • Collections work like arrays. • Collections can store instances of an object type and, conversely, can be attributes of an object type. • Types of collections in PL/SQL: – Associative arrays • String-indexed collections • INDEX BY pls_integer or BINARY_INTEGER – Nested tables – Varrays Understanding Collections
  • 11. 11 Collection Types Nested table Varray Associative array 1 2 3 4 5 6 a f i o t w Index by PLS_INTEGER Index by VARCHAR2
  • 12. 13 Associative arrays: – That are indexed by strings can improve performance – Are pure memory structures that are much faster than schema-level tables – Provide significant additional flexibility Using Associative Arrays Associative arrays 1 2 3 4 5 6 a f i o t w Index by PLS_INTEGER Index by VARCHAR2
  • 13. 15 Associative array in PL/SQL (string-indexed): Creating the Array TYPE type_name IS TABLE OF element_type INDEX BY VARCHAR2(size) CREATE OR REPLACE PROCEDURE report_credit (p_last_name customers.cust_last_name%TYPE, p_credit_limit customers.credit_limit%TYPE) IS TYPE typ_name IS TABLE OF customers%ROWTYPE INDEX BY customers.cust_email%TYPE; v_by_cust_email typ_name; i VARCHAR2(30); PROCEDURE load_arrays IS BEGIN FOR rec IN (SELECT * FROM customers WHERE cust_email IS NOT NULL) LOOP -- Load up the array in single pass to database table. v_by_cust_email (rec.cust_email) := rec; END LOOP; END; ... Create the string-indexed associative array type. Create the string-indexed associative array variable. Populate the string-indexed associative array variable.
  • 14. 16 Traversing the Array ... BEGIN load_arrays; i:= v_by_cust_email.FIRST; dbms_output.put_line ('For credit amount of: ' || p_credit_limit); WHILE i IS NOT NULL LOOP IF v_by_cust_email(i).cust_last_name = p_last_name AND v_by_cust_email(i).credit_limit > p_credit_limit THEN dbms_output.put_line ( 'Customer '|| v_by_cust_email(i).cust_last_name || ': ' || v_by_cust_email(i).cust_email || ' has credit limit of: ' || v_by_cust_email(i).credit_limit); END IF; i := v_by_cust_email.NEXT(i); END LOOP; END report_credit; / EXECUTE report_credit('Walken', 1200) For credit amount of: 1200 Customer Walken: [email protected] has credit limit of: 3600 Customer Walken: [email protected] has credit limit of: 3700
  • 15. 18 Nested table characteristics: – A table within a table – Unbounded – Available in both SQL and PL/SQL as well as the database – Array-like access to individual rows Using Nested Tables Nested table:
  • 16. 19 pOrder nested table: Nested tables are stored out-of-line in storage tables. Nested Table Storage Storage table: ORDID SUPPLIER REQUESTER ORDERED ITEMS 500 50 5000 30-OCT-07 800 80 8000 31-OCT-07 NESTED_TABLE_ID PRODID PRICE 55 555 56 566 57 577 NESTED_TABLE_ID PRODID PRICE 88 888
  • 17. 20 To create a nested table in the database: To create a nested table in PL/SQL: Creating Nested Tables CREATE [OR REPLACE] TYPE type_name AS TABLE OF Element_datatype [NOT NULL]; TYPE type_name IS TABLE OF element_datatype [NOT NULL];
  • 18. 21 – First, define an object type: – Second, declare a column of that collection type: Declaring Collections: Nested Table CREATE TYPE typ_item AS OBJECT --create object (prodid NUMBER(5), price NUMBER(7,2) ) / CREATE TYPE typ_item_nst -- define nested table type AS TABLE OF typ_item / CREATE TABLE pOrder ( -- create database table ordid NUMBER(5), supplier NUMBER(5), requester NUMBER(4), ordered DATE, items typ_item_nst) NESTED TABLE items STORE AS item_stor_tab / 1 2 3
  • 19. 22 Using Nested Tables Add data to the nested table: INSERT INTO pOrder VALUES (500, 50, 5000, sysdate, typ_item_nst( typ_item(55, 555), typ_item(56, 566), typ_item(57, 577))); INSERT INTO pOrder VALUES (800, 80, 8000, sysdate, typ_item_nst (typ_item (88, 888))); ORDID SUPPLIER REQUESTER ORDERED ITEMS 500 50 5000 30-OCT-07 800 80 8000 31-OCT-07 PRODID PRICE 55 555 56 566 57 577 PRODID PRICE 88 888 1 2 1 2 pOrder nested table
  • 20. 23 • Querying the results: • Querying the results with the TABLE function: Using Nested Tables SELECT * FROM porder; ORDID SUPPLIER REQUESTER ORDERED ---------- ---------- ---------- --------- ITEMS(PRODID, PRICE) ----------------------------------------------------------------- 500 50 5000 31-OCT-07 TYP_ITEM_NST(TYP_ITEM(55, 555), TYP_ITEM(56, 566), TYP_ITEM(57, 577)) 800 80 8000 31-OCT-07 TYP_ITEM_NST(TYP_ITEM(88, 888)) SELECT p2.ordid, p1.* FROM porder p2, TABLE(p2.items) p1; ORDID PRODID PRICE ---------- ---------- ---------- 800 88 888 500 57 577 500 55 555 500 56 566
  • 21. 24 Use the collection name and a subscript to reference a collection element: – Syntax: – Example: – To reference a field in a collection: Referencing Collection Elements collection_name(subscript) v_with_discount(i) p_new_items(i).prodid
  • 22. 25 Using Nested Tables in PL/SQL CREATE OR REPLACE PROCEDURE add_order_items (p_ordid NUMBER, p_new_items typ_item_nst) IS v_num_items NUMBER; v_with_discount typ_item_nst; BEGIN v_num_items := p_new_items.COUNT; v_with_discount := p_new_items; IF v_num_items > 2 THEN --ordering more than 2 items gives a 5% discount FOR i IN 1..v_num_items LOOP v_with_discount(i) := typ_item(p_new_items(i).prodid, p_new_items(i).price*.95); END LOOP; END IF; UPDATE pOrder SET items = v_with_discount WHERE ordid = p_ordid; END;
  • 23. 26 Using Nested Tables in PL/SQL -- caller pgm: DECLARE v_form_items typ_item_nst:= typ_item_nst(); BEGIN -- let's say the form holds 4 items v_form_items.EXTEND(4); v_form_items(1) := typ_item(1804, 65); v_form_items(2) := typ_item(3172, 42); v_form_items(3) := typ_item(3337, 800); v_form_items(4) := typ_item(2144, 14); add_order_items(800, v_form_items); END; PRODID PRICE 1804 65 3172 42 3337 800 2144 14 v_form_items variable ORDID SUPPLIER REQUESTER ORDERED ITEMS 500 50 5000 30-OCT-07 800 80 8000 31-OCT-07 Resulting data in the pOrder nested table PRODID PRICE 1804 61.75 3172 39.9 3337 760 2144 13.3 The prices are added after discounts.
  • 24. 27 – To create a varray in the database: – To create a varray in PL/SQL: Understanding Varrays Varray: CREATE [OR REPLACE] TYPE type_name AS VARRAY (max_elements) OF element_datatype [NOT NULL]; TYPE type_name IS VARRAY (max_elements) OF element_datatype [NOT NULL];
  • 25. 28 CREATE TABLE department ( -- create database table dept_id NUMBER(2), name VARCHAR2(25), budget NUMBER(12,2), projects typ_ProjectList) -- declare varray as column / – First, define a collection type: – Second, declare a collection of that type: Declaring Collections: Varray CREATE TYPE typ_Project AS OBJECT( --create object project_no NUMBER(4), title VARCHAR2(35), cost NUMBER(12,2)) / CREATE TYPE typ_ProjectList AS VARRAY (50) OF typ_Project -- define VARRAY type / 1 2 3
  • 26. 29 Add data to the table containing a varray column: Using Varrays INSERT INTO department VALUES (10, 'Exec Admn', 30000000, typ_ProjectList( typ_Project(1001, 'Travel Monitor', 400000), typ_Project(1002, 'Open World', 10000000))); INSERT INTO department VALUES (20, 'IT', 5000000, typ_ProjectList( typ_Project(2001, 'DB11gR2', 900000))); 1 2 DEPT_ID NAME BUDGET PROJECTS PROJECT_NO TITLE COSTS 10 Exec Admn 30000000 1001 Travel Monitor 400000 1002 Open World 10000000 20 IT 5000000 2001 DB11gR2 900000 1 2 DEPARTMENT table
  • 27. 30 – Querying the results: – Querying the results with the TABLE function: Using Varrays SELECT * FROM department; DEPT_ID NAME BUDGET ---------- ------------------------- ---------- PROJECTS(PROJECT_NO, TITLE, COST) ----------------------------------------------------------------- 10 Executive Administration 30000000 TYP_PROJECTLIST(TYP_PROJECT(1001, 'Travel Monitor', 400000), TYP_PROJECT(1002, 'Open World', 10000000)) 20 Information Technology 5000000 TYP_PROJECTLIST(TYP_PROJECT(2001, 'DB11gR2', 900000)) SELECT d2.dept_id, d2.name, d1.* FROM department d2, TABLE(d2.projects) d1; DEPT_ID NAME PROJECT_NO TITLE COST ------- ------------------------ ---------- -------------- -------- 10 Executive Administration 1001 Travel Monitor 400000 10 Executive Administration 1002 Open World 10000000 20 Information Technology 2001 DB11gR2 900000
  • 28. 31 • You can declare collections as the formal parameters of procedures and functions. • You can specify a collection type in the RETURN clause of a function specification. • Collections follow the usual scoping and instantiation rules. Working with Collections in PL/SQL CREATE OR REPLACE PACKAGE manage_dept_proj AS PROCEDURE allocate_new_proj_list (p_dept_id NUMBER, p_name VARCHAR2, p_budget NUMBER); FUNCTION get_dept_project (p_dept_id NUMBER) RETURN typ_projectlist; PROCEDURE update_a_project (p_deptno NUMBER, p_new_project typ_Project, p_position NUMBER); FUNCTION manipulate_project (p_dept_id NUMBER) RETURN typ_projectlist; FUNCTION check_costs (p_project_list typ_projectlist) RETURN boolean; END manage_dept_proj;
  • 29. 34 Three ways to initialize: – Use a constructor. – Fetch from the database. – Assign another collection variable directly. Initializing Collections PROCEDURE allocate_new_proj_list (p_dept_id NUMBER, p_name VARCHAR2, p_budget NUMBER) IS v_accounting_project typ_projectlist; BEGIN -- this example uses a constructor v_accounting_project := typ_ProjectList (typ_Project (1, 'Dsgn New Expense Rpt', 3250), typ_Project (2, 'Outsource Payroll', 12350), typ_Project (3, 'Audit Accounts Payable',1425)); INSERT INTO department VALUES(p_dept_id, p_name, p_budget, v_accounting_project); END allocate_new_proj_list;
  • 30. 35 FUNCTION get_dept_project (p_dept_id NUMBER) RETURN typ_projectlist IS v_accounting_project typ_projectlist; BEGIN -- this example uses a fetch from the database SELECT projects INTO v_accounting_project FROM department WHERE dept_id = p_dept_id; RETURN v_accounting_project; END get_dept_project; Initializing Collections FUNCTION manipulate_project (p_dept_id NUMBER) RETURN typ_projectlist IS v_accounting_project typ_projectlist; v_changed_list typ_projectlist; BEGIN SELECT projects INTO v_accounting_project FROM department WHERE dept_id = p_dept_id; -- this example assigns one collection to another v_changed_list := v_accounting_project; RETURN v_changed_list; END manipulate_project; 1 2
  • 31. 36 Referencing Collection Elements -- sample caller program to the manipulate_project function DECLARE v_result_list typ_projectlist; BEGIN v_result_list := manage_dept_proj.manipulate_project(10); FOR i IN 1..v_result_list.COUNT LOOP dbms_output.put_line('Project #: ' ||v_result_list(i).project_no); dbms_output.put_line('Title: '||v_result_list(i).title); dbms_output.put_line('Cost: ' ||v_result_list(i).cost); END LOOP; END; Project #: 1001 Title: Travel Monitor Cost: 400000 Project #: 1002 Title: Open World Cost: 10000000
  • 32. 37 – EXISTS – COUNT – LIMIT – FIRST and LAST – PRIOR and NEXT – EXTEND – TRIM – DELETE Using Collection Methods collection_name.method_name [(parameters)]
  • 33. 39 FUNCTION check_costs (p_project_list typ_projectlist) RETURN boolean IS c_max_allowed NUMBER := 10000000; i INTEGER; v_flag BOOLEAN := FALSE; BEGIN i := p_project_list.FIRST ; WHILE i IS NOT NULL LOOP IF p_project_list(i).cost > c_max_allowed then v_flag := TRUE; dbms_output.put_line (p_project_list(i).title || ' exceeded allowable budget.'); RETURN TRUE; END IF; i := p_project_list.NEXT(i); END LOOP; RETURN null; END check_costs; Traverse collections with the following methods: Using Collection Methods
  • 34. 40 Using Collection Methods -- sample caller program to check_costs set serverout on DECLARE v_project_list typ_projectlist; BEGIN v_project_list := typ_ProjectList( typ_Project (1,'Dsgn New Expense Rpt', 3250), typ_Project (2, 'Outsource Payroll', 120000), typ_Project (3, 'Audit Accounts Payable',14250000)); IF manage_dept_proj.check_costs(v_project_list) THEN dbms_output.put_line('Project rejected: overbudget'); ELSE dbms_output.put_line('Project accepted, fill out forms.'); END IF; END; Audit Accounts Payable exceeded allowable budget. Project rejected: overbudget PROJECT_NO TITLE COSTS 1 Dsgn New Expense Rpt 3250 2 Outsource Payroll 120000 3 Audit Accounts Payable 14250000 V_PROJECT_LIST variable:
  • 35. 41 PROCEDURE update_a_project (p_deptno NUMBER, p_new_project typ_Project, p_position NUMBER) IS v_my_projects typ_ProjectList; BEGIN v_my_projects := get_dept_project (p_deptno); v_my_projects.EXTEND; --make room for new project /* Move varray elements forward */ FOR i IN REVERSE p_position..v_my_projects.LAST - 1 LOOP v_my_projects(i + 1) := v_my_projects(i); END LOOP; v_my_projects(p_position) := p_new_project; -- insert new one UPDATE department SET projects = v_my_projects WHERE dept_id = p_deptno; END update_a_project; Manipulating Individual Elements
  • 36. 42 Manipulating Individual Elements -- check the table prior to the update: SELECT d2.dept_id, d2.name, d1.* FROM department d2, TABLE(d2.projects) d1; DEPT_ID NAME PROJECT_NO TITLE COST ------- ------------------------- ---------- ----------------------------- -- 10 Executive Administration 1001 Travel Monitor 400000 10 Executive Administration 1002 Open World 10000000 20 Information Technology 2001 DB11gR2 900000 -- caller program to update_a_project BEGIN manage_dept_proj.update_a_project(20, typ_Project(2002, 'AQM', 80000), 2); END; DEPT_ID NAME PROJECT_NO TITLE COST ------- ------------------------- ---------- ----------------------------- -- 10 Executive Administration 1001 Travel Monitor 400000 10 Executive Administration 1002 Open World 10000000 20 Information Technology 2001 DB11gR2 900000 20 Information Technology 2002 AQM 80000 -- check the table after the update: SELECT d2.dept_id, d2.name, d1.* FROM department d2, TABLE(d2.projects) d1;
  • 37. 43 Listing Characteristics for Collections PL/SQL Nested Tables DB Nested Tables PL/SQL Varrays DB Varrays PL/SQL Associative Arrays Maximum size No No Yes Yes Dynamic Sparsity Can be No Dense Dense Yes Storage N/A Stored out-of- line N/A Stored inline (if < 4,000 bytes) N/A Ordering Does not retain ordering and subscripts Does not retain ordering and subscripts Retains ordering and subscripts Retains ordering and subscripts Retains ordering and subscripts
  • 38. 44 • Varrays involve fewer disk accesses and are more efficient. • Use nested tables for storing large amounts of data. • Use varrays to preserve the order of elements in the collection column. • If you do not have a requirement to delete elements in the middle of a collection, favor varrays. • Varrays do not allow piecewise updates. • After deleting the elements, release the unused memory with DBMS_SESSION.FREE_UNUSED_USER_MEMORY Guidelines for Using Collections Effectively
  • 40. 46 • Fetch into a record when fetching from a cursor. • Benefit – No individual variables declaration is needed. – You can automatically use the structure of the SELECT column list. Cursor Design: Use Records DECLARE CURSOR cur_cust IS SELECT customer_id, cust_last_name, cust_email FROM customers WHERE credit_limit = 1200; v_cust_record cur_cust%ROWTYPE; BEGIN OPEN cur_cust; LOOP FETCH cur_cust INTO v_cust_record; ...
  • 41. 47 • Reference implicit cursor attributes immediately after the SQL statement executes. • Benefit – Doing so ensures that you are dealing with the result of the correct SQL statement. Guidelines for Good Cursor Design BEGIN UPDATE customers SET credit_limit = p_credit_limit WHERE customer_id = p_cust_id; get_avg_order(p_cust_id); -- procedure call IF SQL%NOTFOUND THEN ...
  • 42. 48 • Create cursors with parameters. • Benefit – Parameters increase the cursor’s flexibility and reusability. – Parameters help avoid scoping problems. Use Cursor Parameters CURSOR cur_cust (p_crd_limit NUMBER, p_acct_mgr NUMBER) IS SELECT customer_id, cust_last_name, cust_email FROM customers WHERE credit_limit = p_crd_limit AND account_mgr_id = p_acct_mgr; BEGIN OPEN cur_cust(p_crd_limit_in, p_acct_mgr_in); ... CLOSE cur_cust; ... OPEN cur_cust(v_credit_limit, 145); ... END;
  • 43. 49 – Simplify coding with cursor FOR loops. – Benefit • Reduces the volume of code • Automatically handles the open, fetch, and close operations, and defines a record type that matches the cursor definition Using the Cursor For Loop CREATE OR REPLACE PROCEDURE cust_pack (p_crd_limit_in NUMBER, p_acct_mgr_in NUMBER) IS v_credit_limit NUMBER := 1500; CURSOR cur_cust (p_crd_limit NUMBER, p_acct_mgr NUMBER) IS SELECT customer_id, cust_last_name, cust_email FROM customers WHERE credit_limit = p_crd_limit AND account_mgr_id = p_acct_mgr; BEGIN FOR cur_rec IN cur_cust (p_crd_limit_in, p_acct_mgr_in) LOOP -- implicit open and fetch ... END LOOP; -- implicit close ... END;
  • 44. 50 CREATE OR REPLACE PROCEDURE cust_list IS CURSOR cur_cust IS SELECT customer_id, cust_last_name, credit_limit*1.1 FROM customers; cust_record cur_cust%ROWTYPE; BEGIN OPEN cur_cust; LOOP FETCH cur_cust INTO cust_record; DBMS_OUTPUT.PUT_LINE('Customer ' || cust_record.cust_last_name || ' wants credit ' || cust_record.(credit_limit * 1.1)); EXIT WHEN cur_cust%NOTFOUND; END LOOP; ... • Make a DBA happy: Close a cursor when it is no longer needed. • Use column aliases in cursors for calculated columns fetched into records declared with %ROWTYPE. More Guidelines for Good Cursor Design Use col. alias
  • 45. 5151 • A Ref Cursor is a Cursor variable • It hold a pointer to the result set of a previously opened cursor • The actual SQL statement of the cursor is dynamic and determined at execution time • A single Ref Cursor can point to different result sets at different times Returning Result Sets From PL/SQL
  • 46. 52 Memory 1 Southlake, Texas 1400 2 San Francisco 1500 3 New Jersey 1600 4 Seattle, Washington 1700 5 Toronto 1800 Cursor Variables: Overview REF CURSOR memory locator
  • 47. 53 Working with Cursor Variables Define and declare the cursor variable. Open the cursor variable. Fetch rows from the result set. Close the cursor variable. 1 2 3 4
  • 48. 54 • Strong REF CURSOR: – Is restrictive – Specifies a RETURN type – Associates only with type-compatible queries – Is less error prone • Weak REF CURSOR: – Is nonrestrictive – Associates with any query – Is very flexible Strong Versus Weak REF CURSOR Variables
  • 49. 55 DECLARE TYPE rt_cust IS REF CURSOR RETURN customers%ROWTYPE; ... Define a REF CURSOR type: – ref_type_name is a type specified in subsequent declarations. – return_type represents a record type. – RETURN keyword indicates a strong cursor. Step 1: Defining a REF CURSOR Type TYPE ref_type_name IS REF CURSOR [RETURN return_type];
  • 50. 56 Declare a cursor variable of a cursor type: – cursor_variable_name is the name of the cursor variable. – ref_type_name is the name of a REF CURSOR type. Step 1: Declaring a Cursor Variable DECLARE TYPE rt_cust IS REF CURSOR RETURN customers%ROWTYPE; cv_cust rt_cust; cursor_variable_name ref_type_name;
  • 51. 57 Options: – Use %TYPE and %ROWTYPE. – Specify a user-defined record in the RETURN clause. – Declare the cursor variable as the formal parameter of a stored procedure or function. Step 1: Declaring a REF CURSOR Return Type
  • 52. 58 • Associate a cursor variable with a multiple-row SELECT statement. • Execute the query. • Identify the result set: – cursor_variable_name is the name of the cursor variable. – select_statement is the SQL SELECT statement. Step 2: Opening a Cursor Variable OPEN cursor_variable_name FOR select_statement;
  • 53. 60 • Retrieve rows from the result set one at a time. • The return type of the cursor variable must be compatible with the variables named in the INTO clause of the FETCH statement. Step 3: Fetching from a Cursor Variable FETCH cursor_variable_name INTO variable_name1 [,variable_name2,. . .] | record_name;
  • 54. 61 • Disable a cursor variable. • The result set is undefined. • Accessing the cursor variable after it is closed raises the INVALID_CURSOR predefined exception. Step 4: Closing a Cursor Variable CLOSE cursor_variable_name;
  • 55. 62 You can pass query result sets among PL/SQL- stored subprograms and various clients. Passing Cursor Variables as Arguments Pointer to the result set Access by a host variable on the client side
  • 57. 65 Using the SYS_REFCURSOR Predefined Type CREATE OR REPLACE PROCEDURE REFCUR (p_num IN NUMBER) IS refcur sys_refcursor; empno emp.empno%TYPE; ename emp.ename%TYPE; BEGIN IF p_num = 1 THEN OPEN refcur FOR SELECT empno, ename FROM emp; DBMS_OUTPUT.PUT_LINE('Employee# Name'); DBMS_OUTPUT.PUT_LINE('----- -------'); LOOP FETCH refcur INTO empno, ename; EXIT WHEN refcur%NOTFOUND; DBMS_OUTPUT.PUT_LINE(empno || ' ' || ename); END LOOP; ELSE .... SYS_REFCURSOR is a built-in REF CURSOR type that allows any result set to be associated with it.
  • 58. 67 • You cannot use cursor variables with remote subprograms on another server. • You cannot use comparison operators to test cursor variables. • You cannot assign a null value to cursor variables. • You cannot use REF CURSOR types in CREATE TABLE or VIEW statements. • Cursors and cursor variables are not interoperable. Rules for Cursor Variables
  • 59. 68 Cursor variables have the following benefits: – Are dynamic and ensure more flexibility – Are not tied to a single SELECT statement – Hold the value of a pointer – Can reduce network traffic – Give access to query work areas after a block completes Comparing Cursor Variables with Static Cursors
  • 61. 7070 • Use explicit locking to deny access to other sessions for the duration of a transaction • Lock the rows before the update or delete • Syntax: Using the For Update Clause SELECT ... FROM ... FOR UPDATE [OF column_reference][NOWAIT | WAIT n];
  • 62. 7171 • Select for Update default is to wait for locked rows • We can set the wait time to be limited with a time frame or fail if rows are already locked • We can also ask the query to skip the locked rows and return the unlocked rows: Handling Locked Rows SELECT ... FROM ... FOR UPDATE SKIP LOCKED;
  • 63. 7272 • When using cursor with FOR UPDATE clause, we might want to change that same record • We can use the WHERE CURRENT OF cursor to get a fast ROWID access to that row Using WHERE CURRENT OF Clause UPDATE employees SET salary = 12000 WHERE CURRENT OF emp_cursor;
  • 64. 7373 • Include the RETURNING clause with an INSERT, UPDATE, and DELETE to return column values. • Benefit: – Eliminates the need to SELECT the row after DML – Fewer network round trips – Less server CPU time – Fewer cursors – Less server memory is required Using the RETURNING Clause
  • 65. 7474 Using RETURNING Clause Example DECLARE v_emp_sal employees.salary%type; BEGIN UPDATE employees e set salary = salary * 1.2 WHERE e.employee_id = 202 RETURNING e.salary into v_emp_sal; dbms_output.put_line('The new salary is ' || v_emp_sal); END; / The new salary is 7200
  • 66. 75 Use bulk binds to reduce context switches between the PL/SQL engine and the SQL engine. Using Bulk Binding SQL enginePL/SQL run-time engine PL/SQL block FORALL j IN 1..1000 INSERT … (OrderId(j), OrderDate(j), …); SQL statement executor Procedural statement executor
  • 67. 76 Bind whole arrays of values simultaneously, rather than looping to perform fetch, insert, update, and delete on multiple rows. – Instead of: – Use: Using Bulk Binding ... FOR i IN 1 .. 50000 LOOP INSERT INTO bulk_bind_example_tbl VALUES(...); END LOOP; ... ... FORALL i IN 1 .. 50000 INSERT INTO bulk_bind_example_tbl VALUES(...); ...
  • 68. 78 Use BULK COLLECT to improve performance: Using Bulk Binding CREATE OR REPLACE PROCEDURE process_customers (p_account_mgr customers.account_mgr_id%TYPE) IS TYPE typ_numtab IS TABLE OF customers.customer_id%TYPE; TYPE typ_chartab IS TABLE OF customers.cust_last_name%TYPE; TYPE typ_emailtab IS TABLE OF customers.cust_email%TYPE; v_custnos typ_numtab; v_last_names typ_chartab; v_emails typ_emailtab; BEGIN SELECT customer_id, cust_last_name, cust_email BULK COLLECT INTO v_custnos, v_last_names, v_emails FROM customers WHERE account_mgr_id = p_account_mgr; ... END process_customers;
  • 69. 79 Use the RETURNING clause to retrieve information about the rows that are being modified: Using Bulk Binding DECLARE TYPE typ_replist IS VARRAY(100) OF NUMBER; TYPE typ_numlist IS TABLE OF orders.order_total%TYPE; repids typ_replist := typ_replist(153, 155, 156, 161); totlist typ_numlist; c_big_total CONSTANT NUMBER := 60000; BEGIN FORALL i IN repids.FIRST..repids.LAST UPDATE orders SET order_total = .95 * order_total WHERE sales_rep_id = repids(i) AND order_total > c_big_total RETURNING order_total BULK COLLECT INTO Totlist; END;
  • 70. 81 • You can use the SAVE EXCEPTIONS keyword in your FORALL statements: • Exceptions raised during execution are saved in the %BULK_EXCEPTIONS cursor attribute. • The attribute is a collection of records with two fields: – Note that the values always refer to the most recently executed FORALL statement. Using SAVE EXCEPTIONS FORALL index IN lower_bound..upper_bound SAVE EXCEPTIONS {insert_stmt | update_stmt | delete_stmt} Field Definition ERROR_INDEX Holds the iteration of the FORALL statement where the exception was raised ERROR_CODE Holds the corresponding Oracle error code
  • 71. 82 Handling FORALL Exceptions DECLARE TYPE NumList IS TABLE OF NUMBER; num_tab NumList := NumList(100,0,110,300,0,199,200,0,400); bulk_errors EXCEPTION; PRAGMA EXCEPTION_INIT (bulk_errors, -24381 ); BEGIN FORALL i IN num_tab.FIRST..num_tab.LAST SAVE EXCEPTIONS DELETE FROM orders WHERE order_total < 500000/num_tab(i); EXCEPTION WHEN bulk_errors THEN DBMS_OUTPUT.PUT_LINE('Number of errors is: ' || SQL%BULK_EXCEPTIONS.COUNT); FOR j in 1..SQL%BULK_EXCEPTIONS.COUNT LOOP DBMS_OUTPUT.PUT_LINE ( TO_CHAR(SQL%BULK_EXCEPTIONS(j).error_index) || ' / ' || SQLERRM(-SQL%BULK_EXCEPTIONS(j).error_code) ); END LOOP; END; /
  • 72. Dynamic SQL and SQL Injection 83
  • 73. 8484 • Used for two main reasons: – Modify commands dynamically to retrieve or filter columns based on input – Run DDL and DCL commands from PL/SQL – PLEASE don’t use it for running DMLs! • Two main ways to run dynamic commands: – EXECUTE IMMDIATE – DBMS_SQL Dynamic SQL
  • 74. 8585 • Easy syntax: • Accepts all command types • Accept bind variables with the USING clause • Allows dynamic PL/SQL creation Advantages of Native Dynamic SQL EXECUTE_IMMEDIATE dynamic_string { INTO { define_variable [, define_variable ...] | record_name } | BULK COLLECT INTO { collection_name [, collection_name ...] | :host_array_name } } [ USING [ IN | OUT | IN OUT ] bind_argument [, [ IN | OUT | IN OUT ] bind_argument] ... ] [ returning_clause ] ;
  • 75. 8686 • DBMS_SQL supports statements with unknown number of inputs or outputs • We can use DESCRIBE_COLUMNS procedure in the DBMS_SQL package to describe columns for a cursor opened/parsed through DBMS_SQL • DBMS_SQL Supports SQL statements larger than 32 KB • DBMS_SQL Lets you reuse SQL statements Advantages of DBMS_SQL
  • 76. 8787 Using Execute Immediate CREATE OR REPLACE PROCEDURE del_rows (p_condition varchar2, p_rows_deld out number) IS BEGIN EXECUTE IMMEDIATE 'DELETE FROM employees ' || p_condition; p_rows_deld:=sql%ROWCOUNT; END; / DECLARE cnt number; BEGIN del_rows(‘where employee_id = 201’, cnt); END; /
  • 77. 8888 Using DBMS_SQL CREATE OR REPLACE PROCEDURE del_rows (p_condition in varchar2, p_rows_del out number) is cursor_id integer; BEGIN cursor_id := dbms_sql.open_cursor; dbms_sql.parse (cursor_id,'DELETE FROM employees ‘ ||p_condition, dbms_sql.native); p_rows_del := dbms_sql.execute (cursor_id); dbms_sql.close_cursor (cursor_id); END; /
  • 78. 8989 • Starting Oracle 11g, we can transform a DBMS_SQL cursor into a PL/SQL REF CURSOR and vice versa Transforming DBMS_SQL Cursor into a REF CURSOR DBMS_SQL.TO_REFCURSOR (cursor_number IN INTEGER) RETURN SYS_REFCURSOR; DBMS_SQL.TO_CURSOR_NUMBER (rc IN OUT SYS_REFCURSOR) RETURN INTEGER;
  • 79. 9090 • Dynamic SQL is not checked until runtime – Syntax – Structure validity – Permissions • Dynamic SQL can be used as SQL injection entry point Disadvantages of Dynamic SQL
  • 80. 91 SQL injection is a technique for maliciously exploiting applications that use client-supplied data in SQL statements. – Attackers trick the SQL engine into executing unintended commands. – SQL injection techniques may differ, but they all exploit a single vulnerability in the application. – To immunize your code against SQL injection attacks, use bind arguments or validate and sanitize all input concatenated to dynamic SQL. Understanding SQL Injection
  • 81. SQL Injection: Example -- First order attack CREATE OR REPLACE PROCEDURE GET_EMAIL (p_last_name VARCHAR2 DEFAULT NULL) AS TYPE cv_custtyp IS REF CURSOR; cv cv_custtyp; v_email customers.cust_email%TYPE; v_stmt VARCHAR2(400); BEGIN v_stmt := 'SELECT cust_email FROM customers WHERE cust_last_name = '''|| p_last_name || ''''; DBMS_OUTPUT.PUT_LINE('SQL statement: ' || v_stmt); OPEN cv FOR v_stmt; LOOP FETCH cv INTO v_email; EXIT WHEN cv%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Email: '||v_email); END LOOP; CLOSE cv; EXCEPTION WHEN OTHERS THEN dbms_output.PUT_LINE(sqlerrm); dbms_output.PUT_LINE('SQL statement: ' || v_stmt); END; String literals that are incorrectly validated or not validated are concatenated into a dynamic SQL statement, and interpreted as code by th SQL engine.
  • 82. Assessing Vulnerability Code Uses bind arguments for all dynamic components Vulnerable Safe Contains dynamic SQL? Filters input correctly? No No No Yes Yes YesLateral injection vulnerability No
  • 83. 94 Use the following strategies to reduce attack surface: – Use invoker’s rights. – Reduce arbitrary inputs. – Use Bind arguments. – The Filter pitfall. Reducing the Attack Surface
  • 84. 95 • Give out privileges appropriately. • Run code with invoker’s rights when possible. • Ensure that the database privilege model is upheld when using definer’s rights. Avoiding Privilege Escalation Invoker’s rights Definer’s rights
  • 85. 96 • Using invoker’s rights: – Helps to limit the privileges – Helps to minimize the security exposure. • The following example does not use invoker's rights: Using Invoker’s Rights CREATE OR REPLACE PROCEDURE change_password(p_username VARCHAR2 DEFAULT NULL, p_new_password VARCHAR2 DEFAULT NULL) IS v_sql_stmt VARCHAR2(500); BEGIN v_sql_stmt := 'ALTER USER '||p_username ||' IDENTIFIED BY ' || p_new_password; EXECUTE IMMEDIATE v_sql_stmt; END change_password; GRANT EXECUTE ON change_password to OE, HR, SH; 1 2 Note the use of dynamic SQL with concatenated input values.
  • 86. 97 • OE is successful at changing the SYS password, because, by default, CHANGE_PASSWORD executes with SYS privileges: • Add the AUTHID to change the privileges to the invokers: EXECUTE sys.change_password ('SYS', 'mine') Using Invoker’s Rights CREATE OR REPLACE PROCEDURE change_password(p_username VARCHAR2 DEFAULT NULL, p_new_password VARCHAR2 DEFAULT NULL) AUTHID CURRENT_USER IS v_sql_stmt VARCHAR2(500); BEGIN v_sql_stmt := 'ALTER USER '||p_username ||' IDENTIFIED BY ' || p_new_password; EXECUTE IMMEDIATE v_sql_stmt; END change_password;
  • 87. 98 • Reduce the end-user interfaces to only those that are actually needed. – In a Web application, restrict users to accessing specified Web pages. – In a PL/SQL API, expose only those routines that are intended for customer use. • Where user input is required, make use of language features to ensure that only data of the intended type can be specified. – Do not specify a VARCHAR2 parameter when it will be used as a number. – Do not use numbers if you need only positive integers; use natural instead. Reducing Arbitrary Inputs
  • 88. 99 DBMS_ASSERT functions: Understanding DBMS_ASSERT Function Description ENQUOTE_LITERAL Encloses string literal in single quotes SIMPLE_SQL_NAME Verifies that the string is a simple SQL name
  • 89. 100 Formatting Oracle Identifiers – Example 1: The object name used as an identifier: SELECT count(*) records FROM orders; – Example 2: The object name used as a literal: SELECT num_rows FROM user_tables WHERE table_name = 'ORDERS'; – Example 3: The object name used as a quoted (normal format) identifier: • The "orders" table referenced in example 3 is a different table compared to the orders table in examples 1 and 2. • It is vulnerable to SQL injection. SELECT count(*) records FROM "orders";
  • 90. 101 – For your identifiers, determine: 1.Where will the input come from: user or data dictionary? 2.What verification is required? 3.How will the result be used, as an identifier or a literal value? – These three factors affect: • What preprocessing is required (if any) prior to calling the verification functions • Which DBMS_ASSERT verification function is required • What post-processing is required before the identifier can actually be used Working with Identifiers in Dynamic SQL
  • 91. 102 Avoiding Injection by Using DBMS_ASSERT.ENQUOTE_LITERAL CREATE OR REPLACE PROCEDURE Count_Rows(w in varchar2) authid definer as Quote constant varchar2(1) := ''''; Quote_Quote constant varchar2(2) := Quote||Quote; Safe_Literal varchar2(32767) := Quote||replace(w,Quote,Quote_Quote)||Quote; Stmt constant varchar2(32767) := 'SELECT count(*) FROM t WHERE a='|| DBMS_ASSERT.ENQUOTE_LITERAL(Safe_Literal); Row_Count number; BEGIN EXECUTE IMMEDIATE Stmt INTO Row_Count; DBMS_OUTPUT.PUT_LINE(Row_Count||' rows'); END; / Verify whether the literal is well-formed.
  • 92. 105 Avoiding Injection by Using DBMS_ASSERT.SIMPLE_SQL_NAME CREATE OR REPLACE PROCEDURE show_col2 (p_colname varchar2, p_tablename varchar2) AS type t is varray(200) of varchar2(25); Results t; Stmt CONSTANT VARCHAR2(4000) := 'SELECT '||dbms_assert.simple_sql_name( p_colname ) || ' FROM '|| dbms_assert.simple_sql_name( p_tablename ) ; BEGIN DBMS_Output.Put_Line ('SQL Stmt: ' || Stmt); EXECUTE IMMEDIATE Stmt bulk collect into Results; for j in 1..Results.Count() loop DBMS_Output.Put_Line(Results(j)); end loop; END show_col2; Verify that the input string conforms to the basic characteristics of a simple SQL name.
  • 93. 107 • Most common vulnerability: – Dynamic SQL with string concatenation • Your code design must: – Avoid input string concatenation in dynamic SQL – Use bind arguments, whether automatically via static SQL or explicitly via dynamic SQL statements Using Bind Arguments
  • 94. 108 • Filter parameter: – P_WHERE_CLAUSE is a filter. – It is difficult to protect against SQL injection. • Prevention methods: – Do not specify APIs that allow arbitrary query parameters to be exposed. – Any existing APIs with this type of functionality must be deprecated and replaced with safe alternatives. Beware of Filter Parameters stmt := 'SELECT session_id FROM sessions WHERE' || p_where_clause;
  • 95. 109109 • An Autonomous Transaction is an independent Transaction started by another transaction. • The main transaction will hold for the AT and wait until it is completed • Uses PRAGMA compiler directive • Allowed in individual routines • Commonly used for loggers, progress bars, and concurrent operations Autonomous Transactions
  • 96. 110110 AT Example: Main Transaction DECLARE tmp NUMBER; BEGIN FOR i IN 1..10 LOOP tmp := i; INSERT INTO t VALUES (TRUNC(i/2)); END LOOP; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN logger('Failed on ' || TO_CHAR(tmp)); ROLLBACK; RAISE; END; /
  • 97. 111111 AT Example: Logger (AT) PROCEDURE logger (message IN VARCHAR2) IS PRAGMA AUTONOMOUS_TRANSACTION BEGIN INSERT INTO logger VALUES (sysdate, message); COMMIT; END; /
  • 98. 11g Useful Features for Developers 112
  • 99. 113113 • For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column • Limited to output of 4000 chars The LISTAGG Function LISTAGG(measure_expr [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER query_partition_clause]
  • 100. 114114 Using the LISTAGG Function Example SELECT department_id "Dept", hire_date "Date“, last_name "Name", LISTAGG(last_name, ', ') WITHIN GROUP (ORDER BY hire_date, last_name) OVER (PARTITION BY department_id) as "Emp_list" FROM employees WHERE hire_date < '01-SEP-2003' ORDER BY "Dept", "Date", "Name";
  • 101. 115115 • Returns the N-th values in an ordered set of values • Different default window: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW The NTH_VALUE Analytic Function NTH_VALUE (measure_expr, n) [ FROM { FIRST | LAST } ][ { RESPECT | IGNORE } NULLS ] OVER (analytic_clause)
  • 102. 116116 Using NTH_VALUE Analytic Function Example SELECT prod_id, channel_id, MIN(amount_sold), NTH_VALUE ( MIN(amount_sold), 2) OVER (PARTITION BY prod_id ORDER BY channel_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) nv FROM sales WHERE prod_id BETWEEN 13 and 16 GROUP BY prod_id, channel_id;
  • 103. 117117 • Virtual columns are dynamic (not stored) columns of a table (no views required) • Virtual columns obtain their value be evaluating as expression: – Columns from the same table – Constraints – Function Calls (user defined) • Might be used for – Eliminate views – Control table partitioning – Manage “Binary” XMLType data – Index values (function based index) Virtual Columns
  • 104. 118118 Virtual Columns Example column_name [datatype] [GENERATED ALWAYS] AS (expression) [VIRTUAL] CREATE TABLE employees ( id NUMBER, first_name VARCHAR2(10), last_name VARCHAR2(10), salary NUMBER(9,2), comm1 NUMBER(3), comm2 NUMBER(3), salary1 AS (ROUND(salary*(1+comm1/100),2)), salary2 NUMBER GENERATED ALWAYS AS (ROUND(salary*(1+comm2/100),2)) VIRTUAL, CONSTRAINT employees_pk PRIMARY KEY (id) );
  • 106. 120 • A single trigger on a table that allows you to specify actions for each of the following four timing points: – Before the firing statement – Before each row that the firing statement affects – After each row that the firing statement affects – After the firing statement What Is a Compound Trigger?
  • 107. 121 • The compound trigger body supports a common PL/SQL state that the code for each timing point can access. • The compound trigger common state is: – Established when the triggering statement starts – Destroyed when the triggering statement completes • A compound trigger has a declaration section and a section for each of its timing points. Working with Compound Triggers
  • 108. 122 • You can use compound triggers to: – Program an approach where you want the actions you implement for the various timing points to share common data. – Accumulate rows destined for a second table so that you can periodically bulk-insert them – Avoid the mutating-table error (ORA-04091)by allowing rows destined for a second table to accumulate and then bulk-inserting them The Benefits of Using a Compound Trigger
  • 109. 123 • A compound trigger defined on a table has one or more of the following timing-point sections. Timing-point sections must appear in the order shown in the table. Timing-Point Sections of a Table Compound Trigger Timing Point Compound Trigger Section Before the triggering statement executes BEFORE statement After the triggering statement executes AFTER statement Before each row that the triggering statement affects BEFORE EACH ROW After each row that the triggering statement affects AFTER EACH ROW
  • 110. 124 Compound Trigger Structure for Tables CREATE OR REPLACE TRIGGER schema.trigger FOR dml_event_clause ON schema.table COMPOUND TRIGGER -- Initial section -- Declarations -- Subprograms -- Optional section BEFORE STATEMENT IS ...; -- Optional section BEFORE EACH ROW IS ...; -- Optional section AFTER EACH ROW IS ...; -- Optional section AFTER STATEMENT IS ...; 1 2
  • 111. 125 • A mutating table is: – A table that is being modified by an UPDATE, DELETE, or INSERT statement, or – A table that might be updated by the effects of a DELETE CASCADE constraint • The session that issued the triggering statement cannot query or modify a mutating table. • This restriction prevents a trigger from seeing an inconsistent set of data. • This restriction applies to all triggers that use the FOR EACH ROW clause. • Views being modified in the INSTEAD OF triggers are not considered mutating. Trigger Restrictions on Mutating Tables
  • 112. 126 Using a Compound Trigger to Resolve the Mutating Table Error CREATE OR REPLACE TRIGGER check_salary FOR INSERT OR UPDATE OF salary, job_id ON employees WHEN (NEW.job_id <> 'AD_PRES') COMPOUND TRIGGER TYPE salaries_t IS TABLE OF employees.salary%TYPE; min_salaries salaries_t; max_salaries salaries_t; TYPE department_ids_t IS TABLE OF employees.department_id%TYPE; department_ids department_ids_t; TYPE department_salaries_t IS TABLE OF employees.salary%TYPE INDEX BY VARCHAR2(80); department_min_salaries department_salaries_t; department_max_salaries department_salaries_t; -- example continues on next slide
  • 113. 127 Using a Compound Trigger to Resolve the Mutating Table Error . . . BEFORE STATEMENT IS BEGIN SELECT MIN(salary), MAX(salary), NVL(department_id, -1) BULK COLLECT INTO min_Salaries, max_salaries, department_ids FROM employees GROUP BY department_id; FOR j IN 1..department_ids.COUNT() LOOP department_min_salaries(department_ids(j)) := min_salaries(j); department_max_salaries(department_ids(j)) := max_salaries(j); END LOOP; END BEFORE STATEMENT; AFTER EACH ROW IS BEGIN IF :NEW.salary < department_min_salaries(:NEW.department_id) OR :NEW.salary > department_max_salaries(:NEW.department_id) THEN RAISE_APPLICATION_ERROR(-20505,'New Salary is out of acceptable range'); END IF; END AFTER EACH ROW; END check_salary;
  • 114. 128 • A compound trigger must be a DML trigger and defined on either a table or a view • An exception that occurs in one section must be handled in that section. It cannot transfer control to another section • :OLD and :NEW cannot appear in the declaration, BEFORE STATEMENT, or the AFTER STATEMENT sections • Only the BEFORE EACH ROW section can change the value of :NEW • The firing order of compound triggers is not guaranteed unless you use the FOLLOWS clause Compound Trigger Restrictions
  • 115. 129129 • To ensure that a trigger fires after certain other triggers on the same object, use the FOLLOWS clause • Lets you order the executions of multiple triggers relative to each other • Applies to both compound and simple triggers • Applies only to the section of the compound trigger with the same timing point as the simple trigger FOLLOWS Clause
  • 116. 130130 • Consider two AFTER ROW ... FOR UPDATE triggers defined on the same table. One trigger needs to reference the :OLD value and the other trigger needs to change the :OLD value. In this case, you can use the FOLLOWS clause to order the firing sequence FOLLOWS Clause Example CREATE OR REPLACE TRIGGER change_product AFTER UPDATE of product_id ON order_items FOR EACH ROW FOLLOWS oe1.compute_total BEGIN dbms_output.put_line ('Do processing here…'); END;
  • 117. Pivot and Unpivot Turning things around! 131
  • 118. PIVOT and UNPIVOT Clauses of the SELECT Statement • You can use the PIVOT operator of the SELECT statement to write cross-tabulation queries that rotate the column values into new columns, aggregating data in the process. • You can use the UNPIVOT operator of the SELECT statement to rotate columns into values of a column. PIVOT UNPIVOT
  • 119. 133 Pivoting on the QUARTER Column: Conceptual Example 30,000 40,000 60,000 30,000 40,000 20,000 AMOUNT_ SOLD 2,500Q1IUSAKids Jeans 2,000Q2CJapanKids Jeans 2,000Q3SUSAShorts I P C CHANNEL Kids Jeans Shorts Shorts PRODUCT 1,000Q2Germany 1,500Q4USA Q2 QUARTER 2,500Poland QUANTITY_ SOLD COUNTRY 2,000 Q3 Kids Jeans Shorts PRODUCT 3,500 2,000 Q2 1,5002,500 Q4Q1
  • 120. 134134 • Pivoting the data before 11g was a complex query which required the use of the CASE or DECODE functions Pivoting Before Oracle 11g select product, sum(case when quarter = 'Q1' then amount_sold else null end) Q1, sum(case when quarter = 'Q2' then amount_sold else null end) Q2, sum(case when quarter = 'Q3' then amount_sold else null end) Q3, sum(case when quarter = 'Q4' then amount_sold else null end) Q4 from sales group by product;
  • 121. 135 Pivot Clause Syntax table_reference PIVOT [ XML ] ( aggregate_function ( expr ) [[AS] alias ] [, aggregate_function ( expr ) [[AS] alias ] ]... pivot_for_clause pivot_in_clause ) -- Specify the column(s) to pivot whose values are to -- be pivoted into columns. pivot_for_clause = FOR { column |( column [, column]... ) } -- Specify the pivot column values from the columns you -- specified in the pivot_for_clause. pivot_in_clause = IN ( { { { expr | ( expr [, expr]... ) } [ [ AS] alias] }... | subquery | { ANY | ANY [, ANY]...} } )
  • 122. 137 Creating a New View: Example CREATE OR REPLACE VIEW sales_view AS SELECT prod_name AS product, country_name AS country, channel_id AS channel, SUBSTR(calendar_quarter_desc, 6,2) AS quarter, SUM(amount_sold) AS amount_sold, SUM(quantity_sold) AS quantity_sold FROM sales, times, customers, countries, products WHERE sales.time_id = times.time_id AND sales.prod_id = products.prod_id AND sales.cust_id = customers.cust_id AND customers.country_id = countries.country_id GROUP BY prod_name, country_name, channel_id, SUBSTR(calendar_quarter_desc, 6, 2);
  • 123. 139 Selecting the SALES VIEW Data SELECT product, country, channel, quarter, quantity_sold FROM sales_view; PRODUCT COUNTRY CHANNEL QUARTER QUANTITY_SOLD ------------ ------------ ---------- -------- ------------- Y Box Italy 4 01 21 Y Box Italy 4 02 17 Y Box Italy 4 03 20 . . . Y Box Japan 2 01 35 Y Box Japan 2 02 39 Y Box Japan 2 03 36 Y Box Japan 2 04 46 Y Box Japan 3 01 65 . . . Bounce Italy 2 01 34 Bounce Italy 2 02 43 . . . 9502 rows selected.
  • 124. 140 Pivoting the QUARTER Column in the SH Schema: Example SELECT * FROM (SELECT product, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter IN ('01', '02', '03', '04')) ORDER BY product DESC; . . .
  • 125. 142 Unpivoting the QUARTER Column: Conceptual Example 2,000 Q3 Kids Jeans Shorts PRODUCT 3,500 2,000 Q2 1,5002,500 Q4Q1 2,500Q1Kids Jeans 2,000Q2Kids Jeans 3,500Q2Shorts 1,500Q4Kids Jeans Q3 QUARTER 2,000Shorts SUM_OF_QUANTITYPRODUCT
  • 126. 143143 • Univoting the data before 11g requires multiple queries on the table using the UNION ALL operator Unpivoting Before Oracle 11g SELECT * FROM ( SELECT product, '01' AS quarter, Q1_value FROM sales UNION ALL SELECT product, '02' AS quarter, Q2_value FROM sales UNION ALL SELECT product, '03' AS quarter, Q3_value FROM sales UNION ALL SELECT product, '04' AS quarter, Q4_value FROM sales );
  • 127. 144 • An UNPIVOT operation does not reverse a PIVOT operation; instead, it rotates data found in multiple columns of a single row into multiple rows of a single column. • If you are working with pivoted data, UNPIVOT cannot reverse any aggregations that have been made by PIVOT or any other means. Using the UNPIVOT Operator UNPIVOT
  • 128. 145 • The UNPIVOT clause rotates columns from a previously pivoted table or a regular table into rows. You specify: – The measure column or columns to be unpivoted – The name or names for the columns that result from the UNPIVOT operation – The columns that are unpivoted back into values of the column specified in pivot_for_clause • You can use an alias to map the column name to another value. Using the UNPIVOT Clause
  • 129. 146 UNPIVOT Clause Syntax table_reference UNPIVOT [{INCLUDE|EXCLUDE} NULLS] -- specify the measure column(s) to be unpivoted. ( { column | ( column [, column]... ) } unpivot_for_clause unpivot_in_clause ) -- Specify one or more names for the columns that will -- result from the unpivot operation. unpivot_for_clause = FOR { column | ( column [, column]... ) } -- Specify the columns that will be unpivoted into values of -- the column specified in the unpivot_for_clause. unpivot_in_clause = ( { column | ( column [, column]... ) } [ AS { constant | ( constant [, constant]... ) } ] [, { column | ( column [, column]... ) } [ AS { constant | ( constant [, constant]...) } ] ]...)
  • 130. 147 Creating a New Pivot Table: Example . . . CREATE TABLE pivotedtable AS SELECT * FROM (SELECT product, quarter, quantity_sold FROM sales_view) PIVOT (sum(quantity_sold) FOR quarter IN ('01' AS Q1, '02' AS Q2, '03' AS Q3, '04' AS Q4)); SELECT * FROM pivotedtable ORDER BY product DESC;
  • 131. 148 Unpivoting the QUARTER Column in the SH Schema: Example SELECT * FROM pivotedtable UNPIVOT (quantity_sold For Quarter IN (Q1, Q2, Q3, Q4)) ORDER BY product DESC, quarter; . . .
  • 132. Oracle 12c New Features for Developers 149
  • 133. 150150 • Oracle 12c allows SQL VARCHAR2 to be the same size as PL/SQL VARCHAR2 • This is not the default behavior and needs to be turned on: • Create table with 32k varchar2: 32K VARCHAR2/NVARCHAR2 ALTER SYSTEM set MAX_STRING_SIZE = EXTENDED scope = SPFILE; CREATE TABLE Applicants (id NUMBER GENERATED AS IDENTITY, first_name varchar2(30), last_name varchar2(30), application date, CV varchar2(32767) );
  • 134. 151151 • Columns might be marked “invisible” • The invisible column will not be available unless explicitly named • This is very useful when doing application migration Invisible Columns
  • 135. 152152 Invisible Column Example CREATE TABLE tab1 ( id NUMBER, description VARCHAR2(50) INVISIBLE ); INSERT INTO tab1 VALUES (1); SELECT * FROM tab1; ID ---------- 1 INSERT INTO tab1 (id, description) VALUES (2, 'TWO'); COMMIT; SELECT id, description FROM tab1; ID DESCRIPTION ---------- ---------------------------------------------- 1 2 TWO
  • 136. 153 • In previous releases, there was no direct equivalent of the AutoNumber or Identity functionality of other database engines • This behavior had to be implemented using a combination of sequences and trigger • Oracle 12c introduces the ability to define an identity clause against a table column defined using a numeric type • User should have the create sequence privilege • Identity columns are always not null Identity Column Type
  • 137. 154 • ALWAYS – Forces the use of the identity. If an insert statement references the identity column, an error is produced • BY DEFAULT – Allows using the identity if the column isn't referenced in the insert statement. If the column is referenced, the specified value will be used in place of the identity • BY DEFAULT ON NULL – Allows the identity to be used if the identity column is referenced, but a value of NULL is specified Identity Column Type – Options Identity.sql
  • 138. 155 • You can specify only one identity column per table • When specifying identity clause, you must specify a numeric data type for datatype in the column definition clause • When specifying identity clause, you cannot specify the DEFAULT clause in the column definition clause • When specifying identity clause, the NOT NULL constraint is implicitly specified • CREATE TABLE AS SELECT will not inherit the identity property on a column Identity Column Type – Restrictions
  • 139. 156 • You can specify CURRVAL and NEXTVAL as default values for a column • Default value is used when the column is not referenced by the insert or when the DEFAULT keyword is used • Gives you the ability to auto-populate master- detail relationships – Only makes sense if you can guarantee the inserts into the detail table would always immediately follow the insert into the master table Default Value Using a Sequence Default_with_Sequence.sql
  • 140. 157 • In an insert statement, when the column is explicitly referenced, even when using the value NULL, the default value is not used • Oracle database 12c allows you to modify this behavior using the ON NULL clause in the default definition Default Value on Explicit Nulls Default_with_Null.sql
  • 141. 158 • You can define PL/SQL functions and procedures in the WITH clause of a subquery and then use them as you would any other built-in or user-defined function • The “;” does not work as a terminator to the SQL statement when the PL/SQL declaration is included in the WITH clause • Functions defined in the PL/SQL declaration section of the WITH clause take precedence over objects with the same name defined at the schema level • Provides better performance as compared with schema level functions Calling PL/SQL from SQL PLSQL_from_SQL.sql
  • 142. 159 Top-N Queries • A Top-N query is used to retrieve the top or bottom N rows from an ordered set • Combining two Top-N queries gives you the ability to page through an ordered set • Oracle 12c has introduced the row limiting clause to simplify Top-N queries
  • 143. 160160 • This is ANSI syntax • The default offset is 0 • Null values in offset, rowcount or percent will return no rows Top-N in 12c [ OFFSET offset { ROW | ROWS } ] [ FETCH { FIRST | NEXT } [ { rowcount | percent PERCENT } ] { ROW | ROWS } { ONLY | WITH TIES } ]
  • 144. 161161 Top-N Examples SELECT last_name, salary FROM hr.employees ORDER BY salary DESC FETCH FIRST 5 ROWS ONLY; SELECT last_name, salary FROM hr.employees ORDER BY salary DESC FETCH FIRST 5 ROWS WITH TIES; SELECT last_name, salary FROM hr.employees ORDER BY salary DESC FETCH FIRST 10 PERCENT ROWS ONLY;
  • 145. 162162 • Before 12c we had to use the rownum pseudo column to filter out rows • That will require sorting the entire rowset Paging Before 12c SELECT val FROM (SELECT val, rownum AS rnum FROM (SELECT val FROM rownum_order_test ORDER BY val) WHERE rownum <= 10) WHERE rnum >= 5;
  • 146. 163163 • After 12c we have a syntax improvement for paging using the Top-N queries • This will use ROW_NUMBER and RANK in the background – there is no real optimization improvements Paging in Oracle 12c SELECT val FROM rownum_order_test ORDER BY val OFFSET 4 ROWS FETCH NEXT 5 ROWS ONLY;
  • 148. 165165 • Identify and group rows with consecutive values • Consecutive in this regards – row after row • Uses regular expression like syntax to find patterns What is Pattern Matching
  • 149. 166166 • Finding sequences of events in security applications • Locating dropped calls in a CDR listing • Financial price behaviors (V-shape, W-shape U-shape, etc.) • Fraud detection and sensor data analysis Common Business Challenges
  • 150. 167167 MATCH_RECOGNIZE Syntax SELECT FROM [row pattern input table] MATCH_RECOGNIZE ( [ PARTITION BY <cols> ] [ ORDER BY <cols> ] [ MEASURES <cols> ] [ ONE ROW PER MATCH | ALL ROWS PER MATCH ] [ SKIP_TO_option] PATTERN ( <row pattern> ) DEFINE <definition list> )
  • 151. 169169 • Find Simple V-Shape with 1 row output per match MATCH_RECOGNIZE Example SELECT * FROM Ticker MATCH_RECOGNIZE ( PARTITION BY symbol ORDER BY tstamp MEASURES STRT.tstamp AS start_tstamp, LAST(DOWN.tstamp) AS bottom_tstamp, LAST(UP.tstamp) AS end_tstamp ONE ROW PER MATCH AFTER MATCH SKIP TO LAST UP PATTERN (STRT DOWN+ UP+) DEFINE DOWN AS DOWN.price < PREV(DOWN.price), UP AS UP.price > PREV(UP.price) ) MR ORDER BY MR.symbol, MR.start_tstamp;
  • 152. 170170 What Will Be Matched?
  • 153. 171171 • Our goal: find uninterrupted sequences in a book • This can be useful for detecting missing records or sequential behavior Pages in a Book Example
  • 154. 172 Building Our Query 1. Define input 2. Pattern Matching 3. Order input 4. Process pattern 5. using defined conditions 6. Output: rows per match 7. Output: columns per row 8. Where to go after match? SELECT * FROM book_pages MATCH_RECOGNIZE ( ORDER BY page PATTERN (A B*) DEFINE B AS page = PREV(page)+1 ONE ROW PER MATCH MEASURES A.page firstpage, LAST(page) lastpage, COUNT(*) cnt AFTER MATCH SKIP PAST LAST ROW ); SELECT * FROM book_pages MATCH_RECOGNIZE ( ORDER BY page MEASURES A.page firstpage, LAST(page) lastpage, COUNT(*) cnt ONE ROW PER MATCH AFTER MATCH SKIP PAST LAST ROW PATTERN (A B*) DEFINE B AS page = PREV(page)+1 );
  • 155. 173173173173 And The Output… FIRSTPAGE LASTPAGE CNT ---------- ---------- ---------- 1 3 3 5 7 3 10 15 6 42 42 1
  • 156. 174174 • Session-specific sequence • Truncate CASCADE command • Temporal Validity • Temporary Undo • Online DML Operations • And tons of new features for DBAs too! More 12c Developers’ Features…
  • 157. 175175 • More information and examples could be found on my Blog: http://www.realdbamagic.com/he/pivot-a-table/ http://www.realdbamagic.com/he/12c-top-n-query/ http://www.realdbamagic.com/he/with-pl-sql-oracle- 12c/ http://www.realdbamagic.com/he/session-level- sequence-12c/ More Examples…
  • 159. 177177 • Native and Interpreted Compilation • Fine-grain Dependency management • Code inlining Compiling PL/SQL
  • 160. 178 Two compilation methods: – Interpreted compilation • Default compilation method • Interpreted at run time – Native compilation • Compiles into native code • Stored in the SYSTEM tablespace Native and Interpreted Compilation
  • 161. 179 • Use the interpreted mode when (typically during development): – You are using a debugging tool, such as SQL Developer – You need the code compiled quickly • Use the native mode when (typically post development): – Your code is heavily PL/SQL based – You are looking for increased performance in production Deciding on a Compilation Method Native Interpreted
  • 162. 180 • PLSQL_CODE_TYPE: Specifies the compilation mode for the PL/SQL library units • PLSQL_OPTIMIZE_LEVEL: Specifies the optimization level to be used to compile the PL/SQL library units • In general, for fastest performance, use the following setting: Setting the Compilation Method PLSQL_CODE_TYPE = { INTERPRETED | NATIVE } PLSQL_OPTIMIZE_LEVEL = { 0 | 1 | 2 | 3} PLSQL_CODE_TYPE = NATIVE PLSQL_OPTIMIZE_LEVEL = 2
  • 163. 182 Use the USER|ALL|DBA_PLSQL_OBJECT_SETTINGS data dictionary views to display the settings for a PL/SQL object: Viewing the Compilation Settings DESCRIBE ALL_PLSQL_OBJECT_SETTINGS Name Null? Type ------------------------- -------- -------------------- OWNER NOT NULL VARCHAR2(30) NAME NOT NULL VARCHAR2(30) TYPE VARCHAR2(12) PLSQL_OPTIMIZE_LEVEL NUMBER PLSQL_CODE_TYPE VARCHAR2(4000) PLSQL_DEBUG VARCHAR2(4000) PLSQL_WARNINGS VARCHAR2(4000) NLS_LENGTH_SEMANTICS VARCHAR2(4000) PLSQL_CCFLAGS VARCHAR2(4000) PLSCOPE_SETTINGS VARCHAR2(4000)
  • 164. 183 Viewing the Compilation Settings SELECT name, plsql_code_type, plsql_optimize_level FROM user_plsql_object_settings; NAME PLSQL_CODE_TYP PLSQL_OPTIMIZE_LEVEL -------------------- -------------- -------------------- ACTIONS_T INTERPRETED 2 ACTION_T INTERPRETED 2 ACTION_V INTERPRETED 2 ADD_ORDER_ITEMS INTERPRETED 2 CATALOG_TYP INTERPRETED 2 CATALOG_TYP INTERPRETED 2 CATALOG_TYP INTERPRETED 2 CATEGORY_TYP INTERPRETED 2 CATEGORY_TYP INTERPRETED 2 COMPOSITE_CATEGORY_TYP INTERPRETED 2 ...
  • 165. 184 • This requires DBA privileges. • The PLSQL_CODE_TYPE compilation parameter must be set to NATIVE. • The benefits apply to all the built-in PL/SQL packages that are used for many database operations. Setting Up a Database for Native Compilation ALTER SYSTEM SET PLSQL_CODE_TYPE = NATIVE;
  • 166. 185 Compiling a Program Unit for Native Compilation SELECT name, plsql_code_type, plsql_optimize_level FROM user_plsql_object_settings WHERE name = 'ADD_ORDER_ITEMS'; NAME PLSQL_CODE_T PLSQL_OPTIMIZE_LEVEL ---------------------- ------------ -------------------- ADD_ORDER_ITEMS INTERPRETED 2 ALTER SESSION SET PLSQL_CODE_TYPE = 'NATIVE'; ALTER PROCEDURE add_order_items COMPILE; SELECT name, plsql_code_type, plsql_optimize_level FROM user_plsql_object_settings WHERE name = 'ADD_ORDER_ITEMS'; NAME PLSQL_CODE_T PLSQL_OPTIMIZE_LEVEL ---------------------- ------------ -------------------- ADD_ORDER_ITEMS NATIVE 2 1 2 3 4
  • 167. 186186 • We can turn on checking for certain warning conditions • Warning messages can be issued during compilation of PL/SQL subprograms (not for anonymous blocks ) • Use the SQL*Plus SHOW ERRORS command or query the USER_ERRORS data dictionary view, to see any warnings generated during compilation • PL/SQL warning messages use the prefix PLW • Use PLSQL_WARNINGS initialization parameter, or the DBMS_WARNING package PL/SQL Compile-Time Warnings
  • 168. 187187 • SEVERE: Messages for conditions that might cause unexpected behavior or wrong results, such as aliasing problems with parameters • PERFORMANCE: Messages for conditions that might cause performance problems, such as passing a VARCHAR2 value to a NUMBER column in an INSERT statement. • INFORMATIONAL: Messages for conditions that do not have an effect on performance or correctness, but that you might want to change to make the code more maintainable, such as unreachable code that can never be executed. • All: refer to all warning messages PL/SQL Warning Categories
  • 169. 188188 • Can be set at – System level – Session level – Single compilation level PLSQL_WARNINGS Parameter ALTER SYSTEM SET PLSQL_WARNINGS='ENABLE:PERFORMANCE'; ALTER SESSION SET PLSQL_WARNINGS='DISABLE:ALL'; ALTER SESSION SET PLSQL_WARNINGS='ENABLE:SEVERE', 'DISABLE:PERFORMANCE', 'ERROR:07204'; ALTER PROCEDURE query_emp COMPILE PLSQL_WARNINGS='ENABLE:ALL';
  • 170. 189189 • This warning means that the OTHERS handler of PL/SQL subroutine can exit without executing some form of RAISE or a call to the standard RAISE_APPLICATION_ERROR procedure. • Good programming practices suggest that the OTHERS handler should pass an exception upward to avoid the risk of having exceptions go unnoticed PLW-06009 Warning Message
  • 172. 192 • Procedure A is a direct dependent of View B. View B is a direct dependent of Table C. Procedure A is an indirect dependent of Table C. • Direct dependents are invalidated only by changes to the referenced object that affect them. • Indirect dependents can be invalidated by changes to the reference object that do not affect them. Invalidation of Dependent Objects View B Table CProcedure A
  • 173. 193 • Before 11g, adding column D to table T invalidated the dependent objects. • Oracle Database 11g records additional, finer-grained dependency management: – Adding column D to table T does not impact view V and does not invalidate the dependent objects More Precise Dependency Metadata in Oracle Database 11g Procedure P Function FView V Columns: A,B Table T Columns: A,B Add column D
  • 174. 194 • In Oracle Database 11g, dependencies are now tracked at the level of element within unit. • Element-based dependency tracking covers the following: – Dependency of a single-table view on its base table – Dependency of a PL/SQL program unit (package specification, package body, or subprogram) on the following: • Other PL/SQL program units • Tables • Views Fine-Grained Dependency Management
  • 175. 195 Fine-Grained Dependency Management: Example 1 CREATE TABLE t2 (col_a NUMBER, col_b NUMBER, col_c NUMBER); CREATE VIEW v AS SELECT col_a, col_b FROM t2; ALTER TABLE t2 ADD (col_d VARCHAR2(20)); SELECT ud.name, ud.type, ud.referenced_name, ud.referenced_type, uo.status FROM user_dependencies ud, user_objects uo WHERE ud.name = uo.object_name AND ud.name = 'V'; SELECT ud.name, ud.type, ud.referenced_name, ud.referenced_type, uo.status FROM user_dependencies ud, user_objects uo WHERE ud.name = uo.object_name AND ud.name = 'V';
  • 176. 196 Fine-Grained Dependency Management: Example 1 ALTER TABLE t2 MODIFY (col_a VARCHAR2(20)); SELECT ud.name, ud.referenced_name, ud.referenced_type, uo.status FROM user_dependencies ud, user_objects uo WHERE ud.name = uo.object_name AND ud.name = 'V';
  • 177. 197 Fine-Grained Dependency Management: Example 2 CREATE PACKAGE pkg IS PROCEDURE proc_1; END pkg; / CREATE OR REPLACE PROCEDURE p IS BEGIN pkg.proc_1(); END p; / CREATE OR REPLACE PACKAGE pkg IS PROCEDURE proc_1; PROCEDURE unheard_of; END pkg; /
  • 178. 198 • To reduce invalidation of dependent objects: Guidelines for Reducing Invalidation Add new items to the end of the package Reference each table through a view
  • 179. 199 • An object that is not valid when it is referenced must be validated before it can be used. • Validation occurs automatically when an object is referenced; it does not require explicit user action. • If an object is not valid, its status is either COMPILED WITH ERRORS, UNAUTHORIZED, or INVALID. Object Revalidation
  • 181. 201 • Definition: – Inlining is defined as the replacement of a call to subroutine with a copy of the body of the subroutine that is called. – The copied procedure generally runs faster than the original. – The PL/SQL compiler can automatically find the calls that should be inlined. • Benefits: – Inlining can provide large performance gains when applied judiciously by a factor of 2–10 times. Intra Unit Inlining
  • 182. 202 • Influence implementing inlining via two methods: – Oracle parameter PLSQL_OPTIMIZE_LEVEL – PRAGMA INLINE • Recommend that you: – Inline small programs – Inline programs that are frequently executed • Use performance tools to identify hot spots suitable for inline applications: – plstimer Use of Inlining
  • 183. 203 • Noninlined program: Inlining Concepts CREATE OR REPLACE PROCEDURE small_pgm IS a NUMBER; b NUMBER; PROCEDURE touch(x IN OUT NUMBER, y NUMBER) IS BEGIN IF y > 0 THEN x := x*x; END IF; END; BEGIN a := b; FOR I IN 1..10 LOOP touch(a, -17); a := a*b; END LOOP; END small_pgm;
  • 184. 204 • Examine the loop after inlining: Inlining Concepts ... BEGIN a := b; FOR i IN 1..10 LOOP IF –17 > 0 THEN a := a*a; END IF; a := a*b; END LOOP; END small_pgm; ...
  • 185. 205 • The loop is transformed in several steps: Inlining Concepts a := b; FOR i IN 1..10 LOOP ... IF false THEN a := a*a; END IF; a := a*b; END LOOP; a := b; FOR i IN 1..10 LOOP ... a := a*b; END LOOP; a := b; a := a*b; FOR i IN 1..10 LOOP ... END LOOP; a := b*b; FOR i IN 1..10 LOOP ... END LOOP;
  • 186. 206 • Set the PLSQL_OPTIMIZE_LEVEL session-level parameter to a value of 2 or 3: – Setting it to 2 means no automatic inlining is attempted. – Setting it to 3 means automatic inlining is attempted and no pragmas are necessary. • Within a PL/SQL subroutine, use PRAGMAINLINE – NO means no inlining occurs regardless of the level and regardless of the YES pragmas. – YES means inline at level 2 of a particular call and increase the priority of inlining at level 3 for the call. Inlining: Example ALTER PROCEDURE small_pgm COMPILE PLSQL_OPTIMIZE_LEVEL = 3 REUSE SETTINGS;
  • 187. 207 • After setting the PLSQL_OPTIMIZE_LEVEL parameter, use a pragma: Inlining: Example CREATE OR REPLACE PROCEDURE small_pgm IS a PLS_INTEGER; FUNCTION add_it(a PLS_INTEGER, b PLS_INTEGER) RETURN PLS_INTEGER IS BEGIN RETURN a + b; END; BEGIN pragma INLINE (small_pgm, 'YES'); a := add_it(3, 4) + 6; END small_pgm;
  • 188. 208 • Pragmas apply only to calls in the next statement following the pragma. • Programs that make use of smaller helper subroutines are good candidates for inlining. • Only local subroutines can be inlined. • You cannot inline an external subroutine. • Cursor functions should not be inlined. • Inlining can increase the size of a unit. • Be careful about suggesting to inline functions that are deterministic. Inlining: Guidelines
  • 190. 210210 • Handing PL/SQL in memory • Global Temporary Tables (GTT) • PL/SQL result cache • Tips and Tricks Tuning PL/SQL
  • 191. 211211 • Create packages that contain logically related program units • Reserve space for large allocations: – Set the SHARED_POOL_RESERVED_SIZE initialization parameter • Prevent large or frequently used objects from being aged out: – Use the DBMS_SHARED_POOL package Packages: Memory Issues ORA-04031: unable to allocate 4160 bytes of shared memory..
  • 192. 212212 • Use dbms_shared_pool package: • Flags: – P – Package, Procedure or Function – T – Type – R – Trigger – Q – Sequence Pinning Objects DBMS_SHARED_POOL.KEEP(object_name, flag) DBMS_SHARED_POOL.UNKEEP(object_name, flag)
  • 193. 213213 • Memory is used more efficiently for scalability (more users consume more memory) • Package global memory is kept in the SGA (instead of the UGA) and is reused for different users • Package global memory is only used within a unit of work (a client-server call or a server to different server call) • Memory can be released and reused by another user Reusing Package Memory Pragma SERIALLY_REUSABLE
  • 194. 214214 SERIALLY_REUSABLE - Example CREATE OR REPLACE PACKAGE maintain_state IS pragma serially_reusable; num1 number:= 0; END maintain_state; / CREATE OR REPLACE PACKAGE regular_state IS num1 number:= 0; END regular_state; /
  • 195. 215215 SERIALLY_REUSABLE - Example BEGIN dbms_output.put_line(chr(10) || 'THE MAINTAIN PACKAGE'); dbms_output.put_line('Original Value: ' || maintain_state.num1); maintain_state.num1 := maintain_state.num1 + 10; dbms_output.put_line('New Value: ' || maintain_state.num1 || chr(10)); dbms_output.put_line('THE REGULAR PACKAGE'); dbms_output.put_line('Original Value: ' || regular_state.num1); regular_state.num1 := regular_state.num1 + 10; dbms_output.put_line('New Value: ' || regular_state.num1 || chr(10)); dbms_output.put_line(chr(10)||chr(10)); dbms_output.put_line('THE MAINTAIN PACKAGE'); dbms_output.put_line('Original Value: ' || maintain_state.num1); maintain_state.num1 := maintain_state.num1 + 10; dbms_output.put_line('New Value: ' || maintain_state.num1 || chr(10)); dbms_output.put_line('THE REGULAR PACKAGE'); dbms_output.put_line('Original Value: ' || regular_state.num1); regular_state.num1 := regular_state.num1 + 10; dbms_output.put_line('New Value: ' || regular_state.num1 || chr(10)); END; /
  • 196. 216 SERIALLY_REUSABLE - Example First Run Second Run 216 THE MAINTAIN PACKAGE Original Value: 0 New Value: 10 THE REGULAR PACKAGE Original Value: 0 New Value: 10 THE MAINTAIN PACKAGE Original Value: 10 New Value: 20 THE REGULAR PACKAGE Original Value: 10 New Value: 20 THE MAINTAIN PACKAGE Original Value: 0 New Value: 10 THE REGULAR PACKAGE Original Value: 20 New Value: 30 THE MAINTAIN PACKAGE Original Value: 10 New Value: 20 THE REGULAR PACKAGE Original Value: 30 New Value: 40
  • 197. 217217217217 • Since we’re giving up state managing, we can now avoid ORA-4068 when compiling • For more information, visit my blog: http://www.realdbamagic.com/he/solving-ora-04068/ SERIALLY_REUSABLE – Side Effect
  • 198. 218 • The flexibility built into PL/SQL enables you to pass: – Simple scalar variables – Complex data structures • You can use the NOCOPY hint to improve performance with the IN OUT parameters. Passing Data Between PL/SQL Programs
  • 199. 219219 • The hint enables the PL/SQL compiler to pass OUT and IN OUT parameters by reference, as opposed to passing by value • Enhances performance by reducing overhead when passing parameters since less memory is being used • The Compiler will ignore the hint if it is not possible to reference the original structure (type conversion, constraints, for loop variable, etc.) NOCOPY Hint
  • 200. 220220 NOCOPY - Example CREATE OR REPLACE PACKAGE show_emp_pkg IS TYPE EmpTabTyp IS TABLE OF emp%ROWTYPE INDEX BY BINARY_INTEGER; PROCEDURE show_emp (p_Deptno IN NUMBER, p_EmpTab OUT NOCOPY EmpTabTyp); END; /
  • 201. 221 • Can be used in functions as an optimization hint • Indicates that a function can be used in a parallelized query or parallelized DML statement Using the PARALLEL_ENABLE Hint CREATE OR REPLACE FUNCTION f2 (p_p1 NUMBER) RETURN NUMBER PARALLEL_ENABLE IS BEGIN RETURN p_p1 * 2; END f2;
  • 202. 222222 • Large result sets can be stored in Table Variables or Temporary Tables • Temporary tables can be created to hold session-private data that exists only for the duration of a transaction or session. • Each session sees its own separate set of rows • DML locks are not acquired on the data • We can create indexes, views, and triggers on temporary tables • Using temporary tables instead of program variables for large • record sets, can reduce memory consumption Global Temporary Tables CREATE GLOBAL TEMPORARY TABLE hr.employees_temp ON COMMIT PRSERVE ROWS;
  • 204. 224 – The result cache allows SQL query and PL/SQL function results to be stored in cache memory. – Subsequent executions of the same query or function can be served directly out of the cache, improving response times. – This technique can be especially effective for SQL queries and PL/SQL functions that are executed frequently. – Cached query results become invalid when the database data accessed by the query is modified. What Is Result Caching? Data dictionary cache Library cache SGA Result cache Shared pool
  • 205. 225 • You can increase the small, default result cache memory size by using the RESULT_CACHE_MAX_SIZE initialization parameter. Increasing Result Cache Memory Size SGA Default result cache Shared pool Increased result cache
  • 206. 226 • Set Result_Cache_Max_Size from the command line or in an initialization file created by a DBA. • The cache size is dynamic and can be changed either permanently or until the instance is restarted. Setting Result_Cache_Max_Size SQL> ALTER SYSTEM SET result_cache_max_size = 2M SCOPE = MEMORY; System altered. SQL> SELECT name, value 2 FROM v$parameter 3 WHERE name = 'result_cache_max_size'; NAME VALUE ---------------------------------------- ------------------ result_cache_max_size 2097152 1 row selected.
  • 207. 227 • Use the RESULT_CACHE_MODE initialization parameter in the database initialization parameter file. • RESULT_CACHE_MODE can be set to: – MANUAL (default): You must add the RESULT_CACHE hint to your queries for the results to be cached. – FORCE: Results are always stored in the result cache memory, if possible. Enabling Query Result Cache
  • 208. 228 • Definition: – Cache the results of the current query or query fragment in memory, and then use the cached results in future executions of the query or query fragments. – Cached results reside in the result cache memory portion of the SGA. • Benefits: – Improved performance SQL Query Result Cache
  • 209. 229 • Scenario: – You need to find the greatest average value of credit limit grouped by state over the whole population. – The query returns a large number of rows being analyzed to yield a few or one row. – In your query, the data changes fairly slowly (say every hour) but the query is repeated fairly often (say every second). • Solution: – Use the new optimizer hint /*+ result_cache */ in your query: SQL Query Result Cache SELECT /*+ result_cache */ AVG(cust_credit_limit), cust_state_province FROM sh.customers GROUP BY cust_state_province;
  • 210. 230 Clearing the Shared Pool and Result Cache --- flush.sql --- Start with a clean slate. Flush the cache and shared pool. --- Verify that memory was released. SET ECHO ON SET FEEDBACK 1 SET SERVEROUTPUT ON execute dbms_result_cache.flush alter system flush shared_pool /
  • 211. 231 – Definition: • Enables data that is stored in cache to be shared across sessions • Stores the function result cache in an SGA, making it available to any session that runs your application – Benefits: • Improved performance • Improved scalability PL/SQL Function Result Cache
  • 212. 232 • Scenario: – You need a PL/SQL function that derives a complex metric. – The data that your function calculates changes slowly, but the function is frequently called. • Solution: – Use the new RESULT_CACHE clause in your function definition. – You can also have the cache purged when a dependent table experiences a DML operation, by using the RELIES_ON clause. Marking PL/SQL Function Results to Be Cached
  • 213. 233 CREATE OR REPLACE FUNCTION ORD_COUNT(cust_no number) RETURN NUMBER RESULT_CACHE RELIES_ON (orders) IS V_COUNT NUMBER; BEGIN SELECT COUNT(*) INTO V_COUNT FROM orders WHERE customer_id = cust_no; return v_count; end; • Include the RESULT_CACHE option in the function definition. • Optionally, include the RELIES_ON clause. Creating a PL/SQL Function Using the RESULT_CACHE Clause Specifies that the result should be cached Specifies the table upon which the function relies (not needed in 11.2+)
  • 214. 234 • Specify DETERMINISTIC to indicate that the function returns the same result value whenever it is called with the same values for its arguments. • This helps the optimizer avoid redundant function calls. • If a function was called previously with the same arguments, the optimizer can elect to use the previous result. • Do not specify DETERMINISTIC for a function whose result depends on the state of session variables or schema objects. Using the DETERMINISTIC Clause with Functions
  • 215. 235 Calling the PL/SQL Function Inside a Query select cust_last_name, ord_count(customer_id) no_of_orders from customers where cust_last_name = 'MacGraw'
  • 216. 236 Verifying Memory Allocation --- Establish the cache content set serveroutput on execute dbms_result_cache.memory_report
  • 217. 237 Viewing Cache Results Created col name format a55 select * from v$result_cache_statistics /
  • 218. 238 Calling the PL/SQL Function Again select cust_last_name, ord_count(customer_id) no_of_orders from customers where cust_last_name = 'MacGraw'
  • 219. 239 Viewing Cache Results Found col name format a55 select * from v$result_cache_statistics /
  • 220. 240 Confirming That the Cached Result Was Used select type, namespace,status, scan_count,name from v$result_cache_objects /
  • 221. 241241 • Beware of result caching of timed actions: DBMS_LOCK.SLEEP will also be cached overriding the sleep • Cannot be used with invoker's rights or in an anonymous block • Cannot be used with pipelined table function • Cannot be used with OUT or IN OUT parameters. PL/SQL Result Cache Pitfall
  • 223. 243 You can tune your PL/SQL code by: – Identifying the data type and constraint issues • Data type conversion • The NOT NULL constraint • PLS_INTEGER • SIMPLE_INTEGER – Writing smaller executable sections of code – Comparing SQL with PL/SQL – Rephrasing conditional statements Tuning PL/SQL Code
  • 224. 244 DECLARE n NUMBER; BEGIN n := n + 15; -- converted n := n + 15.0; -- not converted ... END; – PL/SQL performs implicit conversions between structurally different data types. – Example: When assigning a PLS_INTEGER variable to a NUMBER variable Avoiding Implicit Data Type Conversion strings dates numbers
  • 225. 245 Understanding the NOT NULL Constraint PROCEDURE calc_m IS m NUMBER; --no constraint ... BEGIN m := a + b; IF m IS NULL THEN -- raise error END IF; END; PROCEDURE calc_m IS m NUMBER NOT NULL:=0; a NUMBER; b NUMBER; BEGIN m := a + b; END; The value of the expression a + b is assigned to a temporary variable, which is then tested for nullity. A better way to check nullity; no performance overhead
  • 226. 246 Use PLS_INTEGER when dealing with integer data. – It is an efficient data type for integer variables. – It requires less storage than INTEGER or NUMBER. – Its operations use machine arithmetic, which is faster than library arithmetic. Using the PLS_INTEGER Data Type for Integers
  • 227. 247 – Definition: • Is a predefined subtype • Has the range –2147483648 .. 2147483648 • Does not include a null value • Is allowed anywhere in PL/SQL where the PLS_INTEGER data type is allowed – Benefits: • Eliminates the overhead of overflow checking • Is estimated to be 2–10 times faster when compared with the PLS_INTEGER type with native PL/SQL compilation Using the SIMPLE_INTEGER Data Type
  • 228. 248 Each has its own benefits: • SQL: – Accesses data in the database – Treats data as sets • PL/SQL: – Provides procedural capabilities – Has more flexibility built into the language Comparing SQL with PL/SQL
  • 229. 249 • Some simple set processing is markedly faster than the equivalent PL/SQL. • Avoid using procedural code when it may be better to use SQL. ...FOR I IN 1..5600 LOOP counter := counter + 1; SELECT product_id, warehouse_id INTO v_p_id, v_wh_id FROM big_inventories WHERE v_p_id = counter; INSERT INTO inventories2 VALUES(v_p_id, v_wh_id); END LOOP;... Comparing SQL with PL/SQL BEGIN INSERT INTO inventories2 SELECT product_id, warehouse_id FROM main_inventories; END;
  • 230. 250 If your business logic results in one condition being true, use the ELSIF syntax for mutually exclusive clauses: Rephrasing Conditional Control Statements IF v_acct_mgr = 145 THEN process_acct_145; END IF; IF v_acct_mgr = 147 THEN process_acct_147; END IF; IF v_acct_mgr = 148 THEN process_acct_148; END IF; IF v_acct_mgr = 149 THEN process_acct_149; END IF; IF v_acct_mgr = 145 THEN process_acct_145; ELSIF v_acct_mgr = 147 THEN process_acct_147; ELSIF v_acct_mgr = 148 THEN process_acct_148; ELSIF v_acct_mgr = 149 THEN process_acct_149; END IF;
  • 231. SQLcl Introduction The Next Generation of SQL*Plus? 251
  • 232. 252252 • Introduced in Oracle 5 (1985) • Looks very simple but has tight integration with other Oracle infrastructure and tools • Very good for reporting, scripting, and automation • Replaced old CLI tool called … UFI (“User Friendly Interface”) SQL*Plus
  • 233. 253253 • Nothing really wrong with SQL*Plus – it is being updated constantly but it is missing a lot of functionality • SQL*Plus forces us to use GUI tools to complete some basic tasks • Easy to understand, a bit hard to use • Not easy for new users or developers What’s Wrong With SQL*Plus?
  • 234. 254254 • SQLcl is a new command line interface (CLI) for SQL users and DBAs • It is part of the SQL Developer suite – developed by the same team: Oracle Database Development Tools Team • Can do most of what SQL*Plus does and much more • Minimal installation, minimal requirements Introducing SQLcl
  • 235. 255255 • It’s still in the early adopter version (current version: 4.2.0.15.295.1605 RC, October 23, 2015) • Uses Java, one version for Windows, Linux and OS X • Planned to be shipped out with Oracle Database 12cR2 (“within the next 12 month”) Introducing SQLcl (cont.)
  • 236. 256256 • Early Adopter version/RC • QA still logging bugs from SQL*Plus regression tests • Adding support for existing SQL*Plus commands and syntax • Adding new commands • But can it do...? – Yes – Not yet – No Current Status
  • 237. 257257 • Download from: SQL Developer Download page • Unzip the file • Run it Installing
  • 238. What Can It Do? 258
  • 239. 259259 • Use the tab key to complete commands • Can be used to list tables, views or other queriable objects • Can be used to replace the * with actual column names • Use the arrow keys to move around the command • Use CTRL+W and CTRL+S to jump to the beginning/end of commands Object Completion and Easy Edit
  • 240. 260260 • 100 command history buffer • Commands are persistent between sessions (watch out for security!) • Use UP and DOWN arrow keys to access old commands • Usage: history history usage History script history full History clear [session?] • Load from history into command buffer: history <number> Command History
  • 241. 261261 • Describe lists the column of the tables just like SQL*Plus • Information shows column names, default values, indexes and constraints. • In 12c database information shows table statistics and In memory status • Works for table, views, sequences, and code objects • Info+ shows additional information regarding column statistics and column histograms Describe, Information and info+
  • 242. 262262 • Outputting query results becomes easier with the “set sqlformat” command (also available in SQL Developer) • We can create a query in the “regular” way and then switch between the different output styles: – ANSIConsole – Fixed column size output – XML or JSON output – HTML output generates a built in search field and a responsive html output for the result only Generating Pretty Output
  • 243. 263263 • We can generate loader ready output (with “|” as a delimiter) • We can generate insert commands • We can easily generate CSV output • Usage: set sqlformat { csv,html,xml,json,ansiconsole,insert, loader,fixed,default} Generating Other Useful Outputs
  • 244. 264264 • Loads a comma separated value (csv) file into a table • The first row of the file must be a header row and the file must be encoded UTF8 • The load is processed with 50 rows per batch • Usage: LOAD [schema.]table_name[@db_link] file_name Load Data From CSV File
  • 245. 265265 • We talked about Developing PL/SQL: composite datatypes, cursors, dynamic SQL • We went over Oracle 11g and Oracle 12c important features • We looked at the compilation process of PL/SQL and how to make it more efficient • We talked about tuning our PL/SQL code and some dedicated features for that • SQLcl – New replacement tool for SQL*Plus Summary
  • 246. 266266 • There are a lot of changes in Oracle 12c. We only looked on a few developer oriented features • New XML and JSON handling functions • Edition Based Redefinition • Pipeline Table Functions and Parallel executions • SQL Performance and new Optimizer changes • Exceptions and Error handling What Did We Not Talk About?
  • 247. Q&AAny Questions? Now will be the time! 267