Oracle * ANSI Joins

advertisement
Oracle 12c – Lesser Spotted New features
Carl Dudley
Tradba Ltd
Oracle ACE Director
carl.dudley@wlv.ac.uk
1
Introduction
Working with Oracle since 1986
Oracle DBA - OCP Oracle7, 8, 9, 10
Oracle DBA of the Year – 2002
Oracle ACE Director
Beta tester – Oracle8, 9, 10, 11, 12
Regular Presenter at Oracle Conferences
Consultant and Trainer
Technical Editor for a number of Oracle texts
UK Oracle User Group Official
Member of IOUC
Fellow of British Computer Society
2
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
3
Approximate Count Distinct (12.1.0.2)
 Faster way of finding counts of distinct values
― sales table with 3,675,372 rows
SELECT COUNT(DISTINCT sales_id) FROM sales;
 3675372
-----------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes |TempSpc| Cost (%CPU)|
-----------------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
1 |
13 |
| 17447
(1)|
|
1 | SORT AGGREGATE
|
|
1 |
13 |
|
|
|
2 |
VIEW
| VW_DAG_0 | 3675K|
45M|
| 17447
(1)|
|
3 |
HASH GROUP BY
|
| 3675K|
21M|
42M| 17447
(1)|
|
4 |
TABLE ACCESS FULL| SALES
| 3675K|
21M|
| 5552
(1)|
------------------------------------------------------------------------------
SELECT APPROX_COUNT_DISTINCT(<<column_name>>) FROM sales;
SELECT APPROX_COUNT_DISTINCT(sales_id) FROM sales;
 3608058
-------------------------------------------------------------------| Id | Operation
| Name | Rows | Bytes | Cost (%CPU)|
-------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
1 |
6 | 5552
(1)|
|
1 | SORT AGGREGATE APPROX|
|
1 |
6 |
|
|
2 |
TABLE ACCESS FULL
| SALES | 3675K|
21M| 5552
(1)|
-------------------------------------------------------------------1- 4
Approximate Count Distinct (12.1.0.2)
 Some initial findings
― sales table with 3,675,372 rows
Column name
Function
Value
Timing
channel_id
Accurate
4
0.21
channel_id
Approximate
4
0.21
prod_id
Accurate
72
0.18
prod_id
Approximate
72
0.21
cust_id
Accurate
7059
0.42
cust_id
Approximate
7014
0.24
sales_id
Accurate
3675372
1.20
sales_id
Approximate
3608058
0.26
amount_sold
Accurate
3586
0.23
amount_sold
Approximate
3616
0.22
% deviation
0
0
-0.64
-1.83
+0.83
1- 5
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
6
The emp and dept Tables
dept
emp
DEPTNO
-----10
20
30
40
EMPNO
----7934
7782
7839
7369
7876
7566
7902
7788
7900
7521
7654
7844
7499
7698
DNAME
-------------ACCOUNTING
RESEARCH
SALES
OPERATIONS
ENAME
---------MILLER
CLARK
KING
SMITH
ADAMS
JONES
FORD
SCOTT
JAMES
WARD
MARTIN
TURNER
ALLEN
BLAKE
LOC
-------NEW YORK
DALLAS
CHICAGO
BOSTON
JOB
MGR HIREDATE
SAL COMM DEPTNO
--------- ----- ----------- ----- ----- -----CLERK
7782 23-JAN-1982 1300
10
MANAGER
7839 09-JUN-1981 2450
10
PRESIDENT
17-NOV-1981 5000
10
CLERK
7902 17-DEC-1980
800
20
CLERK
7788 12-JAN-1983 1100
20
MANAGER
7839 02-APR-1981 2975
20
ANALYST
7566 03-DEC-1981 3000
20
ANALYST
7566 09-DEC-1982 3000
20
CLERK
7698 03-DEC-1981
950
30
SALESMAN
7698 22-FEB-1981 1250
500
30
SALESMAN
7698 28-SEP-1981 1250 1400
30
SALESMAN
7698 08-SEP-1981 1500
0
30
SALESMAN
7698 20-FEB-1981 1600
300
30
MANAGER
7839 01-MAY-1981 2850
30
7
Top-N Queries
 Finding the 'top' set of records is now easier
― Find the employees on the two highest salaries
SELECT empno,ename,sal FROM emp ORDER BY sal DESC
FETCH FIRST 2 ROWS ONLY;
EMPNO
----7839
7788
ENAME
SAL
---------- ---------KING
5000
SCOTT
3000
— Ties are not handled
• FORD has a salary of 3000 and is missing
— Can be fixed using WITH TIES syntax
SELECT empno,ename,sal FROM emp ORDER BY sal DESC
FETCH FIRST 2 ROWS WITH TIES;
EMPNO ENAME
SAL
----- ---------- ---------7839 KING
5000
7788 SCOTT
3000
7902 FORD
3000
8
Top-N queries with Offsets
 Can 'page down' the data using OFFSET
SELECT empno,ename,sal FROM emp ORDER BY sal DESC
OFFSET 6 ROWS FETCH NEXT 4 ROWS WITH TIES;
EMPNO
----7499
7844
7934
7521
7654
ENAME
SAL
---------- ---------ALLEN
1600
TURNER
1500
MILLER
1300
WARD
1250
MARTIN
1250
 But there are some issues
SELECT empno,ename,sal
FROM emp ORDER BY sal DESC
OFFSET 1 ROW
FETCH NEXT 1 ROW WITH TIES;
EMPNO
----7788
7902
ENAME
SAL
---------- ---------SCOTT
3000
FORD
3000
SELECT empno,ename,sal
FROM emp ORDER BY sal DESC
OFFSET 2 ROWS
FETCH NEXT 1 ROW WITH TIES;
EMPNO ENAME
SAL
----- ---------- ---------7902 FORD
3000
The selection of FORD rather
than SCOTT is purely random
9
Use of Subqueries in Top-N queries
 When join sets are not known in advance
 Example purely for illustration :
Number of emp rows to select must be based on highest salary
― Note use of subquery in FETCH clause
• Can use any appropriate variable or expression
SELECT empno,ename,sal
3.333  3 rows
FROM emp
ORDER BY sal DESC
FETCH FIRST (SELECT MAX(sal)/1500 FROM emp) ROWS ONLY;
EMPNO ENAME
----- --------7839 KING
7902 FORD
7788 SCOTT
SAL
---5000
3000
3000
MAX(sal) = 5000
10
Use of Subqueries in the OFFSET Clause
 Shows flexibility of the new feature
SELECT empno,ename,sal
4.0  4 rows
FROM emp
ORDER BY sal DESC
OFFSET (SELECT MIN(sal)/200 FROM emp) ROWS
FETCH NEXT (SELECT MAX(sal)/1000 FROM emp) ROWS ONLY;
EMPNO
----7698
7782
7499
7844
7934
ENAME
---------BLAKE
CLARK
ALLEN
TURNER
MILLER
SAL
---2850
2450
1600
1500
1300
5.0  5 rows
MIN(sal) = 800
MAX(sal) = 5000
11
Top-N Query – 11g
ENAME
SAL
SELECT ename
------ -----,sal
KING
5000
FROM (SELECT ROW_NUMBER() OVER(ORDER BY sal DESC) rank SCOTT
3000
,sal
FORD
3000
,ename
JONES
2975
FROM emp)
BLAKE
2850
WHERE rank <= 5;
|-------------------------------------------------------------------------------|
| Id | Operation
| Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
14 |
462 |
4 (25)| 00:00:01 |
|* 1 | VIEW
|
|
14 |
462 |
4 (25)| 00:00:01 |
|* 2 |
WINDOW SORT PUSHED RANK|
|
14 |
140 |
4 (25)| 00:00:01 |
|
3 |
TABLE ACCESS FULL
| EMP |
14 |
140 |
3
(0)| 00:00:01 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
--------------------------------------------------1 - filter("RANK"<=5)
2 - filter(ROW_NUMBER() OVER ( ORDER BY INTERNAL_FUNCTION("SAL") DESC
)<=5)
12
Top-N Query – 12c
SELECT ename
,sal
FROM emp
ORDER BY sal DESC
FETCH FIRST 5 ROWS ONLY;
ENAME
SAL
------ -----KING
5000
SCOTT
3000
FORD
3000
JONES
2975
BLAKE
2850
--------------------------------------------------------------------------------| Id | Operation
| Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
5 |
230 |
4 (25)| 00:00:01 |
|* 1 | VIEW
|
|
5 |
230 |
4 (25)| 00:00:01 |
|* 2 |
WINDOW SORT PUSHED RANK|
|
14 |
140 |
4 (25)| 00:00:01 |
|
3 |
TABLE ACCESS FULL
| EMP |
14 |
140 |
3
(0)| 00:00:01 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
--------------------------------------------------1 - filter("from$_subquery$_002"."rowlimit_$$_rownumber"<=5)
2 - filter(ROW_NUMBER() OVER ( ORDER BY INTERNAL_FUNCTION("SAL") DESC
)<=5)
13
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
14
Two-way Outer Joins
 Project table linked to a cut down version of emp, called empb
― Project number 3 (DESIGN) has no employees
empb
EMPNO
----7782
7788
7844
7499
ENAME
-----CLARK
SCOTT
TURNER
ALLEN
JOB
-------MANAGER
ANALYST
SALESMAN
SAlESMAN
MGR
---7839
7566
7698
7698
HIREDATE
----------09-JUN-1981
19-APR-1987
08-SEP-1981
20-FEB-1981
dept
proj
PROJ_ID
------1
2
3
SAL COMM DEPTNO PROJ_ID
---- ---- ------ ------2450
10
1
3000
20
2
1500
0
30
1
1600 300
30
1
PNAME
--------BPR
MIGRATION
DESIGN
START_DATE
----------01-JUL-2002
12-OCT-2002
01-NOV-2002
DEPTNO
------10
20
30
40
DNAME
---------ACCOUNTING
RESEARCH
SALES
OPERATIONS
LOC
-------NEW YORK
DALLAS
CHICAGO
BOSTON
15
Two-way Outer Joins – Oracle Style
 In 11g, Oracle syntax cannot outer join a table to more than one other table
SELECT ename,hiredate,d.deptno,dname,p.proj_id,pname
FROM dept d, empb e, proj p
WHERE e.deptno(+) = d.deptno
AND e.proj_id(+) = p.proj_id;
ORA-01417: a table may be outer joined to at most one other table
16
Two-way Outer Joins – Oracle Style (continued)
 Can
beOracle12c,
imitated using
ANSI syntax
But in
this would
be the output
SELECT
ENAME ename,hiredate,d.deptno,dname,p.proj_id,pname
HIREDATE
DEPTNO DNAME
PROJ_ID
-------------------------------------- ------FROM
dept d CROSS
JOIN proj
p
SCOTTOUTER JOIN
19-APR-1987
20 RESEARCH
2
LEFT
empb
e
ALLEN
20-FEB-1981
30 SALES
1
ON
e.deptno = d.deptno
AND e.proj_id
= p.proj_id
TURNER
08-SEP-1981
30 SALES
1
CLARK
09-JUN-1981
10 ACCOUNTING
1
40 OPERATIONS
1
SELECT STATEMENT
|
40 OPERATIONS
2
HASH JOIN OUTER
|
40 OPERATIONS
3
MERGE JOIN CARTESIAN|
SALES
2
TABLE ACCESS FULL | PROJ 30
30 SALES
3
BUFFER SORT
|
1
TABLE ACCESS FULL | DEPT 20 RESEARCH
3
TABLE ACCESS FULL
| EMPB 20 RESEARCH
10 ACCOUNTING
2
10 ACCOUNTING
3
PNAME
---------MIGRATION
BPR
BPR
BPR
BPR
MIGRATION
DESIGN
MIGRATION
DESIGN
BPR
DESIGN
MIGRATION
DESIGN
17
Oracle Outer Joins with Subqueries
 Show each department with its longest serving employee
— Subqueries are not allowed in combination with an outer join condition
SELECT ename, hiredate,d.deptno,dname
FROM dept d LEFT OUTER JOIN emp e
ON
e.deptno
= d.deptno
SELECT
ename,
hiredate,d.deptno,dname
AND
= (SELECT
MIN(e.hiredate)
FROMe.hiredate
dept d, empa
e
FROM
emp e
WHERE e.deptno(+) =
d.deptno
WHERE
e.deptno
= d.deptno);
AND e.hiredate(+) =
(SELECT
MIN(e.hiredate)
FROM empa e
e.deptno
= d.deptno);
ENAME
HIREDATE WHERE
DEPTNO
DNAME
---------- --------- ---------- ---------CLARK
09-JUN-81
10 ACCOUNTING
SMITH
17-DEC-80
20 RESEARCH
ORA-01799: 20-FEB-81
a column may not be
to a subquery
ALLEN
30outer-joined
SALES
40 OPERATIONS
 What about ANSI joins?
18
Lateral Joins
 Inline views cannot reference columns outside of the view
― Considered to be out of scope
SELECT dname
,ename
FROM dept d
,(SELECT ename
FROM emp e
WHERE e.deptno = d.deptno);
WHERE e.deptno = d.deptno)
*
ERROR : ORA-00904: "D"."DEPTNO": invalid identifier
 Lateral views allow such references
― Used internally
 Similar to correlated subqueries
19
Lateral Joins in Oracle12c
SELECT dname
,ename
FROM dept d
,LATERAL(SELECT ename
FROM emp e
WHERE e.deptno = d.deptno);
DNAME
-------------RESEARCH
SALES
SALES
:
 Identical execution plan to
equivalent equi-join
ENAME
---------SMITH
ALLEN
WARD
:
 Need to set an event on Oracle11g
― LATERAL keyword can then be used to allow correlation
ALTER SESSION SET EVENTS '22829 TRACE NAME CONTEXT FOREVER';
20
Lateral Outer Join Syntax – Oracle12c
SELECT dname
,ename
,job
,loc
,sal
FROM dept
,LATERAL(SELECT ename,job,sal
FROM emp
WHERE emp.deptno = dept.deptno
AND (sal < 1000 OR job = 'ANALYST'))(+);
DNAME
-------------ACCOUNTING
RESEARCH
RESEARCH
RESEARCH
SALES
OPERATIONS
ENAME
JOB
LOC
SAL
---------- --------- ------------- ---------NEW YORK
SMITH
CLERK
DALLAS
800
SCOTT
ANALYST
DALLAS
3000
FORD
ANALYST
DALLAS
3000
JAMES
CLERK
CHICAGO
950
BOSTON
21
CROSS APPLY and OUTER APPLY
 Attempts to solve compatibility issues with SQL Server migration tools
SELECT dname,ename,job,loc,sal
FROM dept
CROSS APPLY (SELECT ename,job,sal
FROM emp
WHERE emp.deptno = dept.deptno
AND (sal < 1000 OR job = 'ANALYST'))
SELECT dname,ename,job,loc,sal
FROM dept
OUTER APPLY (SELECT ename,job,sal
FROM emp
WHERE emp.deptno = dept.deptno
AND (sal < 1000 OR job = 'ANALYST'))
DNAME
-------------RESEARCH
ACCOUNTING
RESEARCH
RESEARCH
SALES
RESEARCH
SALES
OPERATIONS
ENAME
---------SMITH
SCOTT
SMITH
FORD
SCOTT
JAMES
FORD
JAMES
JOB
--------CLERK
ANALYST
CLERK
ANALYST
CLERK
ANALYST
CLERK
LOC
SAL
------------- ---------DALLAS
NEW YORK
800
DALLAS
3000
800
DALLAS
3000
CHICAGO
DALLAS
3000
950
CHICAGO
950
BOSTON
22
Equivalent Outer Join Plans
SELECT dname,ename,job,loc,sal
FROM dept
OUTER APPLY (SELECT ename,job,sal
FROM emp
WHERE emp.deptno = dept.deptno
AND (sal < 1000 OR job = 'ANALYST'));
SELECT dname,ename,job,loc,sal
FROM dept, emp
WHERE emp.deptno(+) = dept.deptno
AND (sal(+) < 1000
OR job(+) = 'ANALYST');
SELECT dname,ename,job,loc,sal
FROM dept
,LATERAL(SELECT ename,job,sal
FROM emp
WHERE emp.deptno = dept.deptno
AND (sal < 1000
OR job = 'ANALYST'))(+);
------------------------------------------| Id | Operation
| Name | Rows |
------------------------------------------|
0 | SELECT STATEMENT
|
|
5 |
|* 1 | HASH JOIN OUTER
|
|
5 |
|
2 |
TABLE ACCESS FULL| DEPT |
4 |
|* 3 |
TABLE ACCESS FULL| EMP |
4 |
------------------------------------------23
Use of CROSS/OUTER APPLY
 When join sets are not known in advance
 Example purely for illustration :
Number of emp rows to join is governed by highest salary in that department
― Note use of subquery in FETCH clause
• Can use any appropriate variable or expression
SELECT deptno
DEPTNO DNAME
EMPNO ENAME
,dname
------- ---------- ----- -----,empno
10 ACCOUNTING 7839 KING
,ename
10 ACCOUNTING 7782 CLARK
,sal
10 ACCOUNTING 7934 MILLER
20 RESEARCH
7788 SCOTT
FROM dept OUTER APPLY
20 RESEARCH
7902 FORD
(SELECT empno
30 SALES
7698 BLAKE
,ename
40 OPERATIONS
,sal
FROM emp
WHERE emp.deptno = dept.deptno ORDER BY sal DESC
FETCH FIRST (SELECT MAX(sal)/1500
FROM emp
WHERE emp.deptno = dept.deptno) ROWS
ORDER BY dept.deptno;
SAL
---5000
2450
1300
3000
3000
2850
ONLY)
24
Use of CROSS/OUTER APPLY
 Previous query does not seem possible using ‘basic’ SQL
― However, there is an alternative using analytic functions
― Maybe CROSS/OUTER APPLY are redundant
OUTER APPLY
LEFT
JOIN
SELECT
dept.deptno
,dname
-------------------------------------------------------------------------------Operation
| Name
Operation,ename
| Name
-------------------------------------------------------------------------------,sal
SELECT STATEMENT
|
SELECT STATEMENT
|
FROM
dept
LEFT
JOIN
(SELECT
ROW_NUMBER()
OVER(
NESTED LOOPS OUTER
|
NESTED LOOPS OUTER
|
TABLE
ACCESS
FULL
|DEPT
PARTITION BY deptno
TABLE ACCESS FULL
|DEPT
VIEW ORDER BY sal DESC)
|VW_LAT_2880E7C5
VIEW
|VW_LAT_9C927C5A
rank
VIEW
|VW_LAT_A18161FF
FILTER
|
,sal
FILTER
|
VIEW PUSHED PREDICATE|
,ename
VIEW
|
WINDOW SORT
|
WINDOW SORT
|
TABLE ACCESS FULL |EMP
,deptno
TABLE
ACCESS
FULL|EMP
SORT AGGREGATE
| FROM emp) emp1
SORT AGGREGATE
|
TABLE ACCESS FULL
|EMP
ON emp1.deptno = dept.deptno
TABLE ACCESS FULL |EMP
AND rank <= (SELECT MAX(sal)/1500
FROM emp emp2
WHERE emp2.deptno = dept.deptno);
• But the plans are different
CROSS APPLY and OUTER APPLY with
Table Level Functions
CREATE TYPE emp_row_type as OBJECT (
empno VARCHAR2(8),
ename VARCHAR2(20),
deptno VARCHAR2(20));
CREATE TYPE emp_table_type as TABLE OF emp_row_type;
CREATE OR REPLACE FUNCTION get_all_emps(pi_deptno in number)
RETURN emp_table_type PIPELINED AS
BEGIN
FOR cur IN (SELECT * FROM emp where deptno = pi_deptno) LOOP
PIPE ROW(emp_row_type(cur.empno,cur.ename,cur.deptno));
END LOOP;
RETURN;
END;
26
Table Function Joins
SELECT * FROM dept,
TABLE(get_all_emps(dept.deptno));
SELECT * FROM dept CROSS APPLY
TABLE(get_all_emps(dept.deptno));
SELECT * FROM dept JOIN
TABLE(get_all_emps(dept.deptno))
ON 1 = 1;
SELECT * FROM dept CROSS JOIN
TABLE(get_all_emps(dept.deptno));
DEPTNO
-----10
:
20
:
30
:
DNAME
-------------ACCOUNTING
:
RESEARCH
:
SALES
:
LOC
------------NEW YORK
:
DALLAS
:
CHICAGO
:
EMPNO
-------7782
:
7369
:
7900
:
SELECT * FROM dept,
TABLE(get_all_emps(dept.deptno))(+);
SELECT * FROM dept LEFT OUTER JOIN
TABLE(get_all_emps(dept.deptno))
ON NULL IS NULL;
DEPTNO
-----10
:
20
:
30
:
40
DNAME
-------------ACCOUNTING
:
RESEARCH
:
SALES
:
OPERATIONS
LOC
------------NEW YORK
:
DALLAS
:
CHICAGO
:
BOSTON
EMPNO
-------7782
:
7369
:
7900
:
ENAME
-------------------CLARK
:
SMITH
:
JAMES
:
DEPTNO
-----10
:
20
:
30
:
SELECT * FROM dept OUTER APPLY
TABLE(get_all_emps(dept.deptno));
ENAME
-------------------CLARK
:
SMITH
:
JAMES
:
DEPTNO
-----10
:
20
:
30
:
27
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
28
Adaptive Joins - Oracle12c
 Oracle can switch execution plans during actual execution
― Can occur in joins (e.g. emp with dept)
 Generates subplans and uses a statistics collector to buffer rows
― Detects if number of rows coming out of a table are more than a
threshold value
― If so, it switches to an alternative subplan
 The new plan becomes the final plan
― Subsequent executions will ALWAYS use the final plan
• Statistics collector is bypassed
― AUTOTRACE/EXPLAIN will show the initial default plan
― dbms_xplan.display_cursor will show the final plan
 Designed to optimize when an unexpected number of rows are received
― Maybe because of inaccurate statistics
29
Adaptive Joins - Oracle12c
 Common switch is from Nested Loops to hash join
― Does not work with Sort-Merge
threshold exceeded
Nested Loops
Hash Join
Statistics Collector
Table Scan
dept
Index Scan
emp_dept_fki
Nested Loops
Hash Join
Statistics Collector
Table Scan
emp
Default plan : Nested Loops
Table Scan
dept
Index Scan
emp_dept_fki
Table Scan
emp
Final plan : Hash Join
30
Adaptive Joins - Tipsaj
aj
 Must have
― OPTIMIZER_ADAPTIVE_FEATURES = 12.1.0.1 (or higher)
― OPTIMIZER_ADAPTIVE_REPORTING_ONLY = FALSE (default)
• If set to TRUE, the optimizer can only report adaptive plans and not use them
 New column is_resolved_adaptive_plan in v$sql with values of
'Y' – plan is the final adaptive plan
'N' – plan is adaptive, but final plan has yet to be decided
NULL – plan is not adaptive
 dbms_xplan.display_cursor has new arguments, e.g. format => 'adaptive'
― Shows full plan of query including unused steps
• Unused steps marked with ‘-’
 A hard parse causes the original plan to be used
― Flushing the shared pool
― Analyzing tables with NO_INVALIDATE set to FALSE
31
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
32
PLSQL in the WITH clause
with
 PLSQL procedures and functions now in WITH clause as part of a SQL statement
WITH FUNCTION fw (inempno NUMBER) RETURN NUMBER IS
x NUMBER;
BEGIN
x := inempno;
RETURN x;
END;
SELECT fw(empno) FROM emptest;
/
 Can run much faster than calling standalone PLSQL from SQL
 BUT, functions can be designed with PRAGMA UDF
― Almost as fast as functions in the WITH clause
― Efficient as it mitigates effect of context switches due to internal optimisation
CREATE OR REPLACE FUNCTION f12 (inempno NUMBER) RETURN NUMBER IS
x NUMBER;
PRAGMA UDF;
BEGIN
x:= inempno;
RETURN x;
END;
/
33
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
34
Moving Datafiles Online
 Datafiles can be moved when online
― Transactions can be active in the tablespace when the file is moved
• Genuine online operation
• Datafile number (file_id) can be used for the source file instead of
the filename
ALTER DATABASE MOVE DATAFILE
'c:\oracle\ora12\orac\orac_user1_f1.dbf'
TO 'd:\oranew\orac_user1_f1.dbf';
• If ‘TO’ clause is omitted, the file is moved to DB_CREATE_FILE_DEST
 The move is designed to delete the old file from the operating system
• Has options
– KEEP (keep existing old file)
– REUSE (overwrite existing file of same name)
 Files in the system tablespace can be moved in this way
—But not tempfiles and redo log files
35
Moving Tables Online
 Much simplified technique using dbms_redefinition
CREATE TABLE moveit (c1 NUMBER) TABLESPACE example;
INSERT INTO moveit VALUES (1);
COMMIT;
SELECT tablespace_name FROM user_tables WHERE table_name = 'MOVEIT';
TABLESPACE_NAME
-----------------------------EXAMPLE
BEGIN
dbms_redefinition.redef_table (
uname=>'SCOTT',
tname=>'MOVEIT',
table_part_tablespace=>'USERS');
END;
SELECT tablespace_name FROM user_tables WHERE table_name = 'MOVEIT';
TABLESPACE_NAME
-----------------------------USERS
36
Online DDL Operations
 A number of DDL statements do not need to take blocking locks
 DML continues as usual
― DROP INDEX ind1 ONLINE
― ALTER INDEX ind1 UNUSABLE ONLINE
― ALTER TABLE emp SET UNUSED COLUMNS ONLINE
― ALTER TABLE emp DROP COLUMN job ONLINE
― ALTER TABLE emp MOVE PARTITION p1 ONLINE
• And others
 Could consider creating a normal table as a single partition
― Allows easy online move
37
Data Pump
dp
 New TRANSFORM argument can reduce generation of redo logs on import
TRANSFORM = DISABLE_ARCHIVE_LOGGING:Y
― The value Y disables logging of changes to tables and/or indexes on import
 New VIEWS_AS_TABLES argument can export views as tables
expdp scott/tiger dumpfile = v.dmp directory = dp_dir views_as_tables = emp_v
― Structure of emp_v and data retrieved by it can be imported as a table
38
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
39
View Expansion
view-ex
 Often a need to show the "full" text of a SQL query that uses views
― Use the SQL text expansion facility
 New expand_sql_text procedure in dbms_utility
― Allows full text of queries involving views to be shown
VARIABLE full_text1 CLOB
BEGIN
dbms_utility.expand_sql_text
(input_sql_text => 'SELECT ...
FROM myquery_using_view',
output_sql_text => :full_text1 );
END;
/
PRINT full_text1
40
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
41
Identity Columns
 Used to generate values from a sequence
― Must also specify primary key to ensure only values unique to the column are
allowed
 Three flavours
1. Values always created automatically in sequence
2. Values generated in sequence if not specified (overridden) by user
— Error if NULL is specified by user
3. Values of NULL (specified explicitly by user) result in a generated sequence value
 Automatic generation uses a sequence
― The generating sequence is unaware of any values ‘manually’ inserted
― Identity syntax allows same options as for sequence creation statement
42
Restrictions
 Only one identity column per table
― Identity column must be numeric
 Column Default values clause cannot be specified with identity clause
 A column cannot be ALTERed to become an identity column
 CREATE TABLE AS SELECT operations do not preserve the identity
property of a column in the source table
43
Auto-Generated Identity Columns
CREATE TABLE auto_always(
id NUMBER GENERATED AS IDENTITY
,vdesc VARCHAR2(25));
INSERT INTO auto_always VALUES (1,‘y');
ORA-32795: cannot insert into a generated always identity column
INSERT INTO auto_always VALUES (null,‘y');
ORA-32795: cannot insert into a generated always identity column
INSERT INTO auto_always VALUES (‘y');
ORA-00947: not enough values
INSERT INTO auto_always(vdesc) VALUES ('x');
1 row created.
SELECT * FROM auto_always;
ID VDESC
----- --------------------1 x
44
Auto-Generation That can Be Overridden
CREATE TABLE auto_def_nopk(
id NUMBER GENERATED BY DEFAULT AS IDENTITY
,vdesc VARCHAR2(25));
INSERT INTO auto_def_nopk(vdesc) VALUES('first');
INSERT INTO auto_def_nopk(id,vdesc) VALUES(3,'my id');
INSERT INTO auto_def_nopk(vdesc) VALUES('next');
INSERT INTO auto_def_nopk(vdesc) VALUES('next2');
INSERT INTO auto_def_nopk VALUES(null,'next2');
ORA-01400: cannot insert NULL into ("SCOTT"."AUTO_DEF_NOPK"."ID")
SELECT * FROM auto_def_nopk;
ID
---------1
3
2
3
VDESC
------------------------first
my id
next
next2
45
Handling Null Values with Identity Columns
CREATE TABLE auto_null_nopk(
id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY
,vdesc VARCHAR2(25));
INSERT
INSERT
INSERT
INSERT
INSERT
INTO
INTO
INTO
INTO
INTO
auto_null_nopk(vdesc) VALUES('first');
auto_null_nopk(id,vdesc) VALUES(3,'my id');
auto_null_nopk(vdesc) VALUES('next');
auto_null_nopk(vdesc) VALUES('next2');
auto_null_nopk VALUES(null,'next2');
SELECT * FROM auto_null_nopk;
ID
---------1
3
2
3
4
VDESC
------------------------first
my id
next
next2
next2
46
Using Identity with Primary Keys
CREATE TABLE auto_always(
id NUMBER GENERATED AS IDENTITY PRIMARY KEY
,vdesc VARCHAR2(25);
— Builds a sequence and a primary key constraint
— Without ‘PRIMARY KEY’ builds a NOT NULL constraint that can not be removed
SELECT table_name
,column_name
,generation_type
,identity_options
FROM all_tab_identity_cols;
TABLE_NAME COLUMN_NAME GENERATION_TYPE IDENTITY_OPTIONS
----------- ----------- --------------- ------------------------------------------AUTO_ALWAYS ID
ALWAYS
START WITH: 1, INCREMENT BY: 1, MAX_VALUE:
9999999999999999999999999999, MIN_VALUE: 1,
CYCLE_FLAG: N, CACHE_SIZE: 20, ORDER_FLAG:N
x
INSERT INTO auto_always VALUES (2,'x');
INSERT INTO auto_always(id,vdesc) VALUES (2,'x');
INSERT INTO auto_always(vdesc) VALUES ('x');
INSERT INTO auto_always VALUES ('x');
x

x
Must explicitly list
non-key column names
47
Primary Key Values Auto-Generated by Default
CREATE TABLE auto_def(
id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
,vdesc VARCHAR2(25);
INSERT INTO auto_def(vdesc) VALUES('first');
INSERT INTO auto_def(id,vdesc) VALUES(3,'my id');
INSERT INTO auto_def(vdesc) VALUES('next');
SELECT * FROM auto_def;
ID
---------1
3
2
VDESC
-----------------------------first
my id
next
INSERT INTO auto_def(vdesc) VALUES('next2');
ORA-00001: unique constraint (SCOTT.SYS_C0013084) violated
48
Generating Default Values Directly From a Sequence
 NEXTVAL attribute of a sequence can be used to create a default column value
― MUCH faster than trigger-based methods
CREATE SEQUENCE s1;
CREATE TABLE t1 (c1 NUMBER DEFAULT s1.NEXTVAL PRIMARY KEY
,c2 VARCHAR2(20));
INSERT INTO t1 (c1,c2) VALUES (DEFAULT, 'aaaaa');
INSERT INTO t1 (c2) VALUES ('zzzzz');
SELECT * FROM t1;
C1
--1
2
C2
----aaaaa
zzzzz
49
Identity Columns - Performance
 Ten times faster than using triggers
― Similar to explicit reference to standalone sequence in insert statement
CREATE SEQUENCE s;
CREATE TABLE emp (empno NUMBER DEFAULT s.NEXTVAL PRIMARY KEY
,ename VARCHAR2(30));
50
Removing Column Identity
 Identity can be dropped from columns
ALTER TABLE auto_always MODIFY id DROP IDENTITY;
SELECT table_name
,has_identity
FROM dba_tables
WHERE table_name = 'AUTO_ALWAYS';
TABLE_NAME HAS_IDENTITY
----------- -----------AUTO_ALWAYS NO
51
Adding Columns to Tables
 Adding columns with default values can take a long time
ALTER TABLE t1 ADD newcol CHAR(1000) DEFAULT 'x';
 In 11g a NOT NULL constraint was necessary for a fast operation and to
prevent any lengthening of the rows
 In 12c the NOT NULL constraint is not required
― Reduces time, DDL locking and space usage
― Can also specify that a default value should be used when an attempt to insert
a NULL is made
ALTER TABLE t1 ADD newcol CHAR(1000) DEFAULT ON NULL 'x';
52
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
53
12c
Valid Time Support—Temporal Validity
 Allows tables to have one or more valid time dimensions
― Data is visible (“valid”) depending on its time-based validity
― Determined by start and end dates or timestamps of a valid time period
 Examples include:
― Hiring and finishing dates of an employee
― Valid period for an insurance policy
― Date of change of address for a customer or client
 Temporal Validity is typically activated with:
1. AS OF and VERSIONS BETWEEN
2. DBMS_FLASHBACK_ARCHIVE.enable_at_valid_time
 Can be set to show all table data (the default) or valid data as of a specified time
― Flash forward to see future database state?
54
Valid Time Dimensions—Additional Columns
 Two date-time columns result from CREATE TABLE or ALTER TABLE
― Can be added explicitly or created automatically from PERIOD specification
 One NUMBER column is also created with same name as specified PERIOD
CREATE TABLE emp_vt (
empno NUMBER(6) NOT NULL
,ename VARCHAR2(20),
PERIOD FOR emp_vt_time);
SELECT column_name
,data_type
,column_id AS col_id
,segment_column_id AS seg_col_id
,internal_column_id AS int_col_id
,hidden_column
,virtual_column
FROM user_tab_cols WHERE table_name = 'EMP_VT';
COLUMN_NAME
----------------EMP_VT_TIME_START
EMP_VT_TIME_END
EMP_VT_TIME
EMPNO
ENAME
user_tab_columns does
not show hidden columns
DATA_TYPE
COL_ID SEG_COL_ID INT_COL_ID HID VIR
--------------------------- ------ ---------- ---------- --- --TIMESTAMP(6) WITH TIME ZONE
1
1 YES NO
TIMESTAMP(6) WITH TIME ZONE
2
2 YES NO
NUMBER
3 YES YES
NUMBER
1
3
4 NO NO
VARCHAR2
2
4
5 NO NO
55
Using Valid Times—SQL Level Control
INSERT INTO emp_vt(empno,ename,emp_vt_time_start,emp_vt_time_end)
VALUES (1023
,'COX'
Valid from April 1 to 5
,'01-APR-2013 12.00.01 PM GMT'
,'05-APR-2013 12.00.01 PM GMT');
INSERT INTO emp_vt(empno,ename,emp_vt_time_start,emp_vt_time_end)
VALUES (1024
,'ADAMS'
,'06-APR-2013 12.00.01 PM GMT'
,'05-APR-2014 12.00.01 PM GMT');
SELECT * FROM emp_vt;
EMPNO ENAME
----- ------------1023 COX
1024 ADAMS
SELECT * FROM emp_vt AS OF PERIOD FOR
emp_vt_time '03-APR-2013 12.00.00 PM';
EMPNO ENAME
----- ----------------1023 COX
SELECT * FROM emp_vt
WHERE emp_vt_time_start > '03-APR-2013 12.00.00 PM';
EMPNO ENAME
----- --------------------1024 ADAMS
56
Displaying Valid Times
 Must explicitly select hidden columns
― Cannot be combined with '*'
SELECT empno
,ename
,emp_vt_time_start
,emp_vt_time_end
,emp_vt_time
FROM emp_vt;
EMPNO
----1023
1024
ENAME
----COX
ADAMS
EMP_VT_TIME_START
-------------------------------01-APR-13 12.00.01.000000 PM GMT
06-APR-13 12.00.01.000000 PM GMT
EMP_VT_TIME_END
EMP_VT_TIME
-------------------------------- ----------05-APR-13 12.00.01.000000 PM GMT
7339307
05-APR-14 12.00.01.000000 PM GMT
7339307
• Additional periods each require three extra columns
 No real support for primary keys
― What if employees have numerous valid periods
• Cannot have more than one row for each employee
57
Session-Level Control of Visibility
 Visibility only of data valid at a point in time
― Row must be valid for all PERIODs in use
EXEC dbms_flashback_archive.enable_at_valid_time ('ASOF', '01-APR-13 12.00.01 PM')
 Visibility only of currently valid data
EXEC dbms_flashback_archive.enable_at_valid_time('CURRENT')
 Full data visibility (default)
EXEC dbms_flashback_archive.enable_at_valid_time('ALL')
Must be uppercase
58
Constraints for Valid Times
OWNER
CONSTRAINT_NAME
CONSTRAINT_TYPE
TABLE_NAME
SEARCH_CONDITION
SEARCH_CONDITION_VC
R_OWNER
R_CONSTRAINT_NAME
DELETE_RULE
STATUS
DEFERRABLE
DEFERRED
VALIDATED
GENERATED
BAD
RELY
LAST_CHANGE
INDEX_OWNER
INDEX_NAME
INVALID
VIEW_RELATED
ORIGIN_CON_ID
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
FRED
EMP_VT_TIMEA67E83
C
EMP_VT
EMP_VT_TIME_START < EMP_VT_TIME_END
EMP_VT_TIME_START < EMP_VT_TIME_END
ENABLED
NOT DEFERRABLE
IMMEDIATE
VALIDATED
USER NAME
Data obtained from
user_constraints
16-sep-2013 23:18:34
A null value appears to
get past the constraint
0
Filters in Execution Plans Based on Valid Times
filter(("T"."VT_A_START" IS NULL OR
 Two valid time periods placed on table data
SYS_EXTRACT_UTC("T"."VT_A_START")<=SYS_EXTRACT_UTC(TIMESTAMP'
― Execution
plan shows combinedAND
constraint
2010-01-01
00:00:00.000000000'))
("T"."VT_A_END" IS NULL OR
SYS_EXTRACT_UTC("T"."VT_A_END")>SYS_EXTRACT_UTC(TIMESTAMP' 2010-01-01
00:00:00.000000000')) AND ("T"."VT_B_START" IS NULL OR
SYS_EXTRACT_UTC("T"."VT_B_START")<=SYS_EXTRACT_UTC(TIMESTAMP'
2010-01-01 00:00:00.000000000')) AND ("T"."VT_B_END" IS NULL OR
SYS_EXTRACT_UTC("T"."VT_B_END")>SYS_EXTRACT_UTC(TIMESTAMP' 2010-01-01
00:00:00.000000000')))
60
Table With User-Generated Start and End Columns
CREATE TABLE
(empno
,ename
,stime
,etime
emp_mine
NUMBER
VARCHAR2(12)
DATE
DATE);
SELECT * FROM
EMPNO
----11
12
ENAME
----COX
ALLEN
EMP_MINE;
STIME
--------20-JAN-13
20-JAN-13
ETIME
--------21-FEB-13
21-FEB-13
ALTER TABLE emp_mine ADD(PERIOD FOR pd(stime,etime));
SELECT column_name,data_type,column_id AS col_id
,segment_column_id AS seg_col_id
,internal_column_id AS int_col_id,hidden_column,virtual_column
FROM user_tab_cols WHERE table_name = 'EMP_MINE';
COLUMN_NAME
--------------PD
ETIME
STIME
ENAME
EMPNO
DATA_TYPE
COL_ID SEG_COL_ID INT_COL_ID HID VIR
--------------- ------ ---------- ---------- --- --NUMBER
5 YES YES
DATE
4
4
4 NO NO
DATE
3
3
3 NO NO
VARCHAR2
2
2
2 NO NO
NUMBER
1
1
1 NO NO
User-generated columns can be of type DATE and are not hidden
Temporal History Optimization
 New name for Flashback Data Archive
 Prior to Oracle 12c, the history tables are compressed and deduplicated
• By default, in Oracle 12c they are not compressed nor deduplicated
 When a table is created with the OPTIMIZE DATA clause, table and LOB
compression and LOB deduplication is automatically turned on
CREATE FLASHBACK ARCHIVE two_year_archive TABLESPACE tbs1
OPTIMIZE DATA RETENTION 5 YEAR;
― OPTIMIZE DATA requires the Advanced Compression Option
― NO OPTIMIZE DATA is the default
 FDA history tables already compressed and deduplicated in 11g or before
upgrade to 12c will continue to get compression and deduplication
 To stop optimization on FDA history tables, execute the following statement
ALTER FLASHBACK ARCHIVE three_year_archive NO OPTIMIZE DATA;
62
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
63
Row Archival
ra
 Allows rows of data to be archived but still remain online within the table
― Decision on whether to archive out to other media could be difficult
• Data may be needed occasionally in the future
 New column (ora_archive_state) is added initialized to 0 for all rows
― Value can be changed to non-zero based on some criteria
• The non-zero rows will be omitted from queries when row archival is active
• Session must have row archiving in active state (default)
ALTER SESSION SET ROW ARCHIVAL VISIBILITY = ACTIVE;
― DML will affect only the rows that are active
 Table could be partitioned based on ora_archive_state
― Non-zero (archived) rows could be placed in compressed, read only partition
64
Row Archival – Dictionary Information
ALTER TABLE emparch ROW ARCHIVAL;
SELECT column_name
,data_type
,hidden_column
FROM user_tab_cols
WHERE table_name = 'EMPARCH';
COLUMN_NAME
-------------------EMPNO
ENAME
JOB
MGR
HIREDATE
SAL
COMM
DEPTNO
SYS_NC00009$
ORA_ARCHIVE_STATE
DATA_TYPE
--------NUMBER
VARCHAR2
VARCHAR2
NUMBER
DATE
NUMBER
NUMBER
NUMBER
RAW
VARCHAR2
HID
--NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
 To optimise performance
― Could append ora_archive_state to primary and unique key indexes
• Not to the constraint as it could lead to rows with duplicate ‘key’ values and
different ora_archive_state values
65
Online Archiving
 Set some rows to be archived
― There are a total of fourteen rows in the table
UPDATE emparch
SET ORA_ARCHIVE_STATE = DBMS_ILM.ARCHIVESTATENAME(1)
WHERE hiredate >'01-SEP-81';
SELECT COUNT(*) FROM emparch;
COUNT(*)
--------6
Only 6 rows visible
Could simply use a number
(Can be reset to zero to remove archiving)
 Put session out of row archival mode
ALTER SESSION SET ROW ARCHIVAL VISIBILITY = ALL;
SELECT COUNT(*) FROM emparch;
COUNT(*)
--------14
All rows now visible
66
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
67
Privilege Capture Views—Full List
View
Description
DBA_PRIV_CAPTURES
Existing privilege analysis policies
DBA_USED_PRIVS
Privileges used for reported privilege analysis policies
DBA_UNUSED_PRIVS
Privileges not used for reported privilege analysis policies
DBA_USED_OBJPRIVS
Object privileges used for reported privilege analysis policies
DBA_UNUSED_OBJPRIVS
Object privileges not used for reported privilege analysis policies
DBA_USED_OBJPRIVS_PATH
Object privileges used for reported privilege analysis policies, with grant paths
DBA_UNUSED_OBJPRIVS_PATH
Object privileges not used for reported privilege analysis policies, with grant paths
DBA_USED_SYSPRIVS
System privileges used for reported privilege analysis policies
DBA_UNUSED_SYSPRIVS
System privileges not used for reported privilege analysis policies
DBA_USED_SYSPRIVS_PATH
System privileges used for reported privilege analysis policies, with grant paths
DBA_UNUSED_SYSPRIVS_PATH
System privileges not used for reported privilege analysis policies, wiith grant paths
DBA_USED_PUBPRIVS
All privileges for the PUBLIC role used for reported privilege analysis policies
DBA_USED_USERPRIVS
User privileges used for reported privilege analysis policies
DBA_UNUSED_USERPRIVS
User privileges not used for reported privilege analysis policies
DBA_USED_USERPRIVS_PATH
User privileges used for reported privilege analysis policies, with grant paths
DBA_UNUSED_USERPRIVS_PATH
Privileges not used for reported privilege analysis policies, with grant paths
68
Benefits of Privilege Analysis
 Finding over-privileged users
― Too many system privileges
― Gain evidence to convince the application developer to reduce privileges
 Finding unnecessarily granted privileges of application database users
― Privileges unnecessarily granted to PUBLIC
― SELECT ANY TABLE rather than specific object privileges
 Can perform
― Role analysis
• Provide a list of roles to analyze
― Context analysis
• Specify a boolean expression with the SYS_CONTEXT function
― Role and context analysis
• Provide list of roles and a SYS_CONTEXT boolean expression for the condition
for the context analysis
― Database analysis
• All privileges are analyzed except those used by sys
69
General Steps for Managing Privilege Analysis
1. Define the privilege analysis policy with
DBMS_PRIVILEGE_CAPTURE.CREATE_CAPTURE
2. Enable the privilege analysis policy with the
DBMS_PRIVILEGE_CAPTURE.ENABLE_CAPTURE
― Allows to start analysis at a specific time
3. Let the application or users go about their operations
4. Disable the privilege analysis with
DBMS_PRIVILEGE_CAPTURE.DISABLE_CAPTURE
― Enables the production of an analysis of privilege usage over a period of time
5. Generate privilege analysis results with
DBMS_PRIVILEGE_CAPTURE.GENERATE_RESULT
― Writes the results to the data dictionary views described earlier
 May also drop a privilege capture—removes all records from the dictionary
― DBMS_PRIVILEGE_CAPTURE.DROP_CAPTURE
70
Privilege Capture—Setup
 Capture the privileges used by FRED
― Note the use of sys_context to limit capture to sessions owned by FRED
BEGIN
dbms_privilege_capture.create_capture(
name =>'CAPFRED',
type => dbms_privilege_capture.g_context,
condition => 'sys_context(''USERENV'',''SESSION_USER'') = ''FRED''' );
dbms_privilege_capture.enable_capture('CAPFRED');
END;
 Any privileges used by FRED will now be captured
 Fred has (and happens to use in his session under analysis)
 Direct SELECT access on amy’s table called d
• SELECT access on amy’s table called b via the role rf
71
Completing the Privilege Capture
 The capture must first be ended (disabled)
 The result can then be generated
― Rows will be placed in the privilege capture views
BEGIN
dbms_privilege_capture.disable_capture('CAPFRED');
dbms_privilege_capture.generate_result('CAPFRED');
END;
CAPTURE
OS_USER
MODULE
OBJ_PRIV
OBJECT_NAME
USED_ROLE
PATH
----------------CAPTURE
OS_USER
MODULE
OBJ_PRIV
OBJECT_NAME
USED_ROLE
PATH
:
:
:
:
:
:
:
:
:
:
:
:
:
:
CAPFRED
LTREE1\Administrator
SQL*Plus
SELECT
B
RF
FRED,RF
CAPFRED
LTREE1\Administrator
SQL*Plus
SELECT
D
FRED
FRED
“Verticalized” data from
dba_used_objprivs_path
Fred has used the rf role
to access amy’s b table
Fred has used the SELECT
privilege to access amy’s d table
72
Topics
Approximate SQL Functions
Top-N Queries
New Join Techniques
Adaptive Joins
PLSQL in the WITH Clause
Handling Data – Online and Data Pump
View Expansion
Identity Columns
Temporal Validity
Online Archiving
Privilege Analysis
Bits and Pieces
73
Cascading Truncate
 Truncating a parent table with referencing child tables gives
ORA-02266: unique/primary keys in table referenced
by enabled foreign keys
 New syntax in 12c
TRUNCATE TABLE dept CASCADE;
― The referencing foreign keys must be defined with ON DELETE CASCADE
• If not, an error is generated
ORA-14705: unique or primary keys referenced
by enabled foreign keys in table
74
RMAN
 Oracle 12c simplifies the use of SQL commands
― Simply issue the SQL command
RMAN> ALTER TABLESPACE user1 ONLINE;
RMAN> SELECT name, dbid, log_mode FROM v$database;
 New administrative privilege SYSBACKUP
― Includes permissions for backup and recovery, including the ability to
connect to a closed database
― Does not include data access privileges such as SELECT ANY TABLE
― Can be explicitly used in an RMAN connection by a SYSBACKUP
privileged user
$ RMAN TARGET 'sys/rmdb@rmdb' AS SYSBACKUP
― The SYSDBA privilege may also be used for backward compatibility
75
RMAN
 Beginning in Oracle 12c:
― Level 1 multisection incremental backups are allowed
• Oracle 11g allowed multisection only on level 0 incremental backups
― RMAN can take advantage of multisection for image copies
• The final result will be a single physical image copy of the source datafile
 Oracle 12c allows recovery of a single table or table partition from RMAN
backups
― Does not affect the remaining database objects
― Reduces time and disk space, compared to the earlier workflow like TSPITR
― RMAN optionally renames the recovered tables or table partitions with the REMAP
TABLE and the REMAP TABLESPACE clauses
• REMAP TABLE would allow you to recover a table to a different name
– Could then compare with the original to ascertain the extent of the
differences
76
Managing Memory in the PGA: PGA_AGGREGATE_LIMIT
 PGA_AGGREGATE_LIMIT specifies a limit on the aggregate PGA memory
consumed by the instance.
 Default value is the greater of:
1. 2 GB
2. 200 percent of PGA_AGGREGATE_TARGET
• So changing the target can affect the limit
• Will not exceed 90% of (physical memory size – total SGA size)
3. 3 MB times the PROCESSES parameter
 Cannot be set below its default value
― Except a value of 0 imposes no limit on PGA memory consumed
 If PGA_AGGREGATE_LIMIT is exceeded
― Initally, latest calls of sessions using the most untunable memory will be aborted
• If still over the limit, sessions using most untunable memory are terminated
― SYS and background processes will not be subjected to these actions
77
Statistics Gathering
 Statistics are gathered for objects created using Direct Path operations (CTAS)
and Insert as select operations
 Saves the extra full table scan that is required to gather statistics immediately
after creating a new object
― Useful in ETL Jobs
 Histograms are not generated by the online statistics gathering job
― Requires additional data scans
• Could impact overall time to create the object or perform batch Insert operation
78
Further Features
 Maximum length of VARCHAR2 is 32,767 if MAX_STRING_SIZE is set to EXTENDED
― This change is irreversible
― Uses CLOBs - stored out of line above 4000 characters
 Automatic full database caching
 Big table caching
 In Oracle 12c all passwords must be case sensitive
― The parameter SEC_CASE_SENSITIVE_LOGON is deprecated
 New routines for password verification
― ora12c_verify_function and ora12c_strong_verify_function
generates more stringent limits
79
Undocumented Parameters and Hints
 Around 2750 undocumented
parameters on 11g
 Around 3968 undocumented
parameters on 12c
SELECT ksppinm
,ksppstvl
FROM x$ksppi I
,x$ksppcv v
WHERE v.indx =i.indx
AND ksppinm LIKE '%join%';
 Listing of hints can be seen in v$sql_hint
― 273 hints on 11g
― 332 hints on 12c
80
Oracle 12c – Lesser Spotted New features
Carl Dudley
Tradba Ltd
Oracle ACE Director
carl.dudley@wlv.ac.uk
81
Download