Upload
lamnguyet
View
269
Download
1
Embed Size (px)
Citation preview
Basel · Baden · Bern · Lausanne · Zurich · Düsseldorf · Frankfurt/M. · Freiburg i. Br. · Hamburg · Munich · Stuttgart · Vienna
The Good (and a few Bad and Ugly) 11g Features for SQL & PL/SQL
Peter WelkerSenior ConsultantTrivadis GmbH
Oracle Database New Features 11g for Developers – SQL 2 © 2008
Some Features – Not All Features!
� We only have an hour� We'll see some SQL and PL/SQL 11g features in detail
� But there are more…� Read Only Tables� DDL Wait Timeouts� Query Change Notification� Built In Function's Metadata� PL/Scope SourceCode Metadata� Hierarchical PL/SQL Profiler� DBMS_XA Package for Distribuited Transactions via PL/SQL� Direct Sequence Usage within PL/SQL� PL/SQL CONTINUE Statement� Warning Enhancements� PL/SQL Function Cache
Oracle Database New Features 11g for Developers – SQL 3 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 4 © 2008
Cross Table Reporting – Pivot Tables
� Many tools like Excel supports dynamic cross table reporting
Oracle Database New Features 11g for Developers – SQL 5 © 2008
PIVOT and UNPIVOT Operators
� Two new SQL operators are supported in 11g
� Switches the result of a SELECT statement from multiple rows to multiple columns (PIVOT)
� Switches the result of a select statement from multiple columns to multiple rows (UNPIVOT)
YEAR ORDER_MO SUM(ORDER_TOTAL) YEAR DIRECT ONLINE---- -------- ---------------- ���� ---- ---------- ----------1999 direct 1274078,8 1999 1274078,8 1271019,51999 online 1271019,5
YEAR DIRECT ONLINE YEAR ORDER_ YEARLY_TOTAL---- ---------- ---------- ���� ---- ------ ------------1999 1274078,8 1271019,5 1999 direct 1274078,8
1999 online 1271019,5
Oracle Database New Features 11g for Developers – SQL 6 © 2008
PIVOT Operator
� PIVOTs the query's result from rows to columns, aggregating data
� Performs an implicit GROUP BY based on all the columns not referred in the pivot clause
� Result is the grouping columns followed by the new columns described in the PIVOT_IN clause
� The PIVOT_IN clause is static if the output is tabular� Dynamic presentation only for XML output
SQL> SELECT *2 FROM (SELECT dname, sal FROM emp NATURAL JOIN dept)3 PIVOT (sum(sal) FOR dname IN ('ACCOUNTING' "ACC", 'RESEARCH' 4 "RES", 'SALES' "SALES",'OPERATIONS' "OP"));
ACC RES SALES OP----- ------ ----- --8750 10875 9400
Oracle Database New Features 11g for Developers – SQL 7 © 2008
UNPIVOT Operator (1)
� UNPIVOTs the query's result from columns to rows
� The measure values are defined in the UNPIVOT clause
� The descriptor values are defined by the PIVOT_FOR clause
� New descriptor values are defined in the UNPIVOT_IN clause� They are column names of the source table. These columns
will be suppressed in the new result
� New measure values are the values of the suppressed columns
� INCLUDE NULLS clause generates a new row even if there is no measure value for a descriptor value
Oracle Database New Features 11g for Developers – SQL 8 © 2008
UNPIVOT Operator (2)
� The UNPIVOT_IN clause is static too� There is no XML clause available
SQL> SELECT *2 FROM (SELECT *3 FROM (SELECT dname, sal FROM emp NATURAL JOIN dept)4 PIVOT (sum(sal) FOR dname IN 5 ('ACCOUNTING' "ACC", 'RESEARCH' "RES", 6 'SALES' "SALES", 'OPERATIONS' "OP")))7 UNPIVOT INCLUDE NULLS (total FOR dname IN 8 (ACC as 'acc', RES as 'res', 9 SALES as 'sales', OP as 'op'));
DNAME TOTAL----- ------------acc 8750res 10875sales 9400op
Oracle Database New Features 11g for Developers – SQL 9 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 10 © 2008
Enhanced ADD COLUMN Functionality
� Since now ALTER TABLE ADD COLUMN with DEFAULT VALUE updated each row
� Now the DEFAULT clause for a NOT NULL column stores the value as metadata (USER_TAB_COLUMNS.DATA_DEFAULT) but the column itself is not populated
0
5
10
15
20
5000 50000 100000
Rows
Sec
onds
10g11g
Adding a new column with NOT NULL DEFAULT VALUE and no restriction in a table containing 5’000, 50’000 and 100’000 rows
Oracle Database New Features 11g for Developers – SQL 11 © 2008
Enhanced ADD COLUMN Functionality – Limits
� The table cannot � Have any LOB columns� Be index-organized, temporary, part of a cluster, a queue table, an
object table, or a materialized view
� The added column cannot � Be encrypted, an object type column, or a nested table column
� Inserts – even if specifying DEFAULT – store the row on disk
� Adding an unsupported column to a table with existing not null default values does not copy default values to the table� But this behavior might be switched off in future releases
Oracle Database New Features 11g for Developers – SQL 12 © 2008
Risks and Unexpected behaviour
� Triggers are deactivated when adding a Not Null column!� This is a change in default behavior compared to 10g …� …but it's not a bug � "Works as expected"
� The feature is deactivated for existing columns whenever the first nullable column is added� Then all column values of the table are written into the table segment
- incl. default values� This may take a lot of time and produce many migrated rows� Afterwards the table may use more disk space and data access might
be slower than without this feature
Oracle Database New Features 11g for Developers – SQL 13 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 14 © 2008
� New column in a table derived by evaluating an expression� Columns from same table, Constants, SQL functions, user-defined
PL/SQL functions)
� Is not stored on disk
� Can be indexed (like a function-based index)
� Can be used as a partition or subpartition key
� Can be used in queries, DML, and DDL statements
� Statistics can be collected on them
� Integrity and check constraints can be created on them
Virtual Columns – Introduction
Oracle Database New Features 11g for Developers – SQL 15 © 2008
Virtual Columns – Syntax and Limits
� Not supported for index-organized, external, object, cluster, or temporary tables
� User defined functions must be deterministic (for index usage) – but no partition key allowed then
� The output must be a scalar value
� Examples
ALTER TABLE employees2 ADD (income AS (salary + (salary*commission_pct)))
ALTER TABLE employees2 ADD (income2 AS (total_income(salary,commission_pct)))
Oracle Database New Features 11g for Developers – SQL 16 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 17 © 2008
Compound Trigger – Overview (1)
� Like a simple package with a procedure per trigger type
� Easier to maintain� Simplify bypassing of mutating table issues
� Trigger INSERT, UPDATE and DELETE with the same code� Including AFTER and BEFORE…� …on STATEMENT and ROW level
� Also for view triggers� Instead Of trigger with declarations and state-variables� Still no STATEMENT parts possible
Oracle Database New Features 11g for Developers – SQL 18 © 2008
Compound Trigger – Overview (2)
� 10g
… 11g
�����
������������������������� ���������������
������������������������� ����!�� ��������"����
����#$%��$� &
�����'������
�������!���$�����!'(
������������������������� ���������������
������������������������� ����!�� ��������"����
�����������)���* �#$%��$� &��'������
�������!���$�����!'(������������������������� ���������������
������������������������ ����!�� ��������"����
�����������)���* �#$%��$� &��'������
�������!���$�����!'(������������������������� ���������������
������������������������ ����!�� ��������"����
����#$%��$� &
�����'������
�������!���$�����!'(
Oracle Database New Features 11g for Developers – SQL 19 © 2008
Compound Trigger – Avoiding Mutating Table Issues
� It is not possible to read/write the table currently triggered� In a ROW trigger� But possible in the AFTER STATEMENT trigger
� Workaround� Use persistent state collections in separate package� Filled during ROW triggers� Process the collections in AFTER STATEMENT trigger
� High effort and lead to memory leaks if the DML failed
� In 11g the compound trigger holds "trigger state" collections� "Living" only during DML� Automatically cleaned up afterwards
Oracle Database New Features 11g for Developers – SQL 20 © 2008
Compound Trigger – Example
CREATE TRIGGER compound_triggerFOR INSERT or UPDATE ON empCOMPOUND TRIGGER-- Declaration with trigger state variablesTYPE t1 is table of VARCHAR2(100) index by PLS_INTEGER;var1 t1; -- you can use this collection in each section…BEFORE STATEMENT ISBEGIN…
END BEFORE STATEMENT;BEFORE EACH ROW ISBEGIN…
END BEFORE EACH ROW;…
END compound_trigger;
Oracle Database New Features 11g for Developers – SQL 21 © 2008
Compound Trigger – Interesting facts (1)
� Must be implemented in PL/SQL and not call Java or C procedures
� View compound triggers only support one INSTEAD OF EACH ROW section
� Fire only if at least one section is affected
� Each FORALL Bulk DML Record causes a new Trigger call
� Initial section cannot define AUTONOMOUS_TRANSACTION
� No initialization block – No global exception section
� Can be combined with conventional triggers.
Oracle Database New Features 11g for Developers – SQL 22 © 2008
Trigger Order Definition with "following"
� It's possible to create more than one trigger� For the same table doing the same thing (= same timing point)� Example: Three BEFORE INSERT FOR EACH ROW triggers for
table EMP are possible
� Before 11g the firing order is undefined� To guarantee particular operations from all triggers in a certain order,
all triggers of the same type had to be combined into one trigger
� With 11g the "follows" keyword allows to build trigger chains
CREATE OR REPLACE TRIGGER emp_bir2BEFORE INSERT ON emp FOR EACH ROWFOLLOWS emp_bir1
DECLARE…
Oracle Database New Features 11g for Developers – SQL 23 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 24 © 2008
� In 10g a 32k Limit for EXECUTE IMMEDIATE and OPEN CURSOR FOR existed
� In 11g those limitations are eliminated
� It also works with dynamic PL/SQL and EXECUTE IMMEDIATE
DECLAREc CLOB;
BEGIN… -- fill the lob
dbms_output.put_line('CLOB size: ' || dbms_lob.getlength(c));EXECUTE IMMEDIATE c;
END;
CLOB size: 2975344PL/SQL procedure successfully completed.
Improved Native Dynamic SQL
Oracle Database New Features 11g for Developers – SQL 25 © 2008
Improved DBMS_SQL – Overview
� DBMS_SQL is still required whenever you can not fix� The SELECT list� The bind variables
� Oracle database 10g has several limitations DBMS_SQL� No interoperability between DBMS_SQL and REF CURSORs� Datatype restrictions for DBMS_SQL (e.g. Object Types)� No bulk binding with user defined collection types
� In Oracle database 11g all these limitations are eliminated
� Some limitations still exist – but they can be handled by conversion to native dynamic SQL� You cannot retrieve query rows into PL/SQL records� You cannot use SQL% attributes
Oracle Database New Features 11g for Developers – SQL 26 © 2008
Improved DBMS_SQL – Example
� Switch between DBMS_SQL and REF CURSORSCREATE OR REPLACE TYPE vc_array IS TABLE OF VARCHAR2(200);/
CREATE OR REPLACE FUNCTION getCursor (psql VARCHAR2, pbinds vc_array, pvalues vc_array)
RETURN sys_refcursor ISc NUMBER;r NUMBER;myCursor sys_refcursor;
BEGINc := dbms_sql.open_cursor;dbms_sql.parse(c, psql, dbms_sql.native);FOR i IN pbinds.FIRST..pbinds.LAST LOOP
dbms_sql.bind_variable(c, pbinds(i), pvalues(i));END LOOP;r := dbms_sql.execute(c);myCursor := dbms_sql.to_refcursor(c);RETURN myCursor;
END;/
Oracle Database New Features 11g for Developers – SQL 27 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 28 © 2008
Native Compilation – 9i and 10g
� Native compilation for PL/SQL components was introduced in 9i� In order to speed up calculation intensive code
� Quite complicated to handle – became a bit simpler in 10g
� Compile, linker, make utility etc. required on the database server� Plus setting of 7 parameters� Reduced to 4 in 10g
�����
��� ��
����� �������
���������� � �� �
�����
��� �� �� !
�"# ������ ��!��� �$� ��
��"� ����
%���"��
���&
Oracle Database New Features 11g for Developers – SQL 29 © 2008
Native Compilation Enhancements
� In Oracle database 11g everything becomes much simpler� Oracle database compiles/creates the code by itself� Native code is stored in the SYSTEM tablespace only� At first call in an instance the library loads once into shared memory� All Parameters from 9i and 10g except PLSQL_CODE_TYPE are
deprecated
� However…� Compilation does not help much for SQL intensive programs� Can require more shared memory
� For a short demo – see the SIMPLE_INTEGER demo in this chapter
Oracle Database New Features 11g for Developers – SQL 30 © 2008
SIMPLE_INTEGER Datatype
� A new datatype "SIMPLE_INTEGER" is provided� 4 bytes will be translated into a hardware compliant 4 byte signed
integer (with range from -2^31+1 to +2^31-1) on native compilation� Not Null PLS_INTEGER subtype without overflow checkingALTER SESSION SET plsql_code_type = 'INTERPRETED';CREATE OR REPLACE PROCEDURE simpleinttest IS
s SIMPLE_INTEGER := 0;BEGIN
LOOPs := s + 1;EXIT WHEN s > 100000000;
END LOOP;END;/exec simpleinttestElapsed 00:00:05.31
ALTER SESSION SET plsql_code_type = 'NATIVE';ALTER PROCEDURE simpleinttest COMPILE;exec simpleinttestElapsed 00:00:00.35
Oracle Database New Features 11g for Developers – SQL 31 © 2008
Inlining Optimization (1)
� It's often faster to just repeat parts of code instead of putting it to a separate procedure� No need for stack and heap initialization� Further optimizations can take place over the whole code� No parameter overhead� But usually we try to work modular!
� In 11g the compiler itself uses this optimization at compile time� It simply replaces procedure calls with the procedure's bodies
� There are two methods for using this kind of optimization� Setting PLSQL_OPTIMIZE_LEVEL to 3 (a new value in 11g)� Using PRAGMA INLINE in your code for inlining calls on next line
� Only LOCAL subroutines can be inlined!
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
PRAGMA INLINE (subfunc, 'YES')
Oracle Database New Features 11g for Developers – SQL 32 © 2008
Inlining Optimization (2)
� Recommended for small procedures that are frequently executedALTER SESSION SET plsql_optimize_level = 2; -- max in 10gCREATE OR REPLACE PROCEDURE inlinetest IS
s NUMBER := 0;FUNCTION subfunc (p NUMBER) RETURN NUMBER IS
v1 VARCHAR2(4000) := '12345678980';BEGIN
RETURN p + 1; END;
BEGINLOOP
s := subfunc(s);EXIT WHEN s >= 5000000;
END LOOP;END;/exec inlinetestElapsed: 00:00:02.39
ALTER SESSION SET plsql_optimize_level = 3; -- new in 11gALTER PROCEDURE inlinetest compile;exec inlinetest;Elapsed: 00:00:00.81
Oracle Database New Features 11g for Developers – SQL 33 © 2008
Agenda
Data are always part of the game.
� PIVOT and UNPIVOT Operators
� Enhanced ADD COLUMN Functionality
� Virtual Columns
� Trigger Enhancements
� Improved Dynamic SQL
� PL/SQL – Performance
� Miscellaneous PL/SQL
Oracle Database New Features 11g for Developers – SQL 34 © 2008
Fine Grained Functional Dependencies (1)
� In 10g views and PL/SQL stored programs became invalid if� A referenced table did any changes to column definitions� Even if the view or the program did not reference this column
CREATE TABLE tab_depdemo (text VARCHAR2(100),col2 VARCHAR2(100)
);CREATE OR REPLACE PROCEDURE tab_depdemoproc (pText VARCHAR2) IS
vText tab_depdemo.text%TYPE := pText;BEGIN
INSERT INTO tab_depdemo (text) VALUES (vText);END;/ALTER TABLE tab_depdemo ADD (newcol INTEGER);SELECT status FROM user_objects WHERE object_name LIKE '%DEMO%';
OBJECT_NAME OBJECT_TYPE STATUS------------------------------ ------------------- -------TAB_DEPDEMO TABLE VALIDTAB_DEPDEMOPROC PROCEDURE INVALID
Oracle Database New Features 11g for Developers – SQL 35 © 2008
Fine Grained Functional Dependencies (2)
� In 11g invalidation is done on column level, not on table level anymore� Also for PLSQL releations eg between a package and a procedure
ALTER TABLE tab_depdemo ADD (newcol INTEGER);SELECT status FROM user_objects WHERE object_name LIKE '%DEMO%';
OBJECT_NAME OBJECT_TYPE STATUS------------------------------ ------------------- -------TAB_DEPDEMO TABLE VALIDTAB_DEPDEMOPROC PROCEDURE VALID
ALTER table tab_depdemo drop column COL2;SELECT status FROM user_objects WHERE object_name LIKE '%DEMO%';
OBJECT_NAME OBJECT_TYPE STATUS------------------------------ ------------------- -------TAB_DEPDEMO TABLE VALIDTAB_DEPDEMOPROC PROCEDURE VALID
Oracle Database New Features 11g for Developers – SQL 36 © 2008
Regular Expression Enhancements (1)
� Regular expressions had been introduced with 10g
� 4 functions are supported in 10g� REGEXP_LIKE (string, pattern [, param]): Boolean� REGEXP_INSTR (string, pattern [, pos [, occurrence [, ret_opt
[, param]]]]): Number� REGEXP_SUBSTR ((string, pattern [, pos [, occurrence [, param]]]]): String� REGEXP_REPLACE ((string, pattern [, repl_string [, pos [, occurrence
[, param]]]]): String
SQL> SELECT address, regexp_replace(address,2 '(.*)\.(.*)@(.*)',3 '\2.\1@\3') as new_address4 FROM email5 WHERE regexp_like(address, '.*@.*\.(com|de)');
ADDRESS NEW_ADDRESS-----------------------------------------------------------------Vetter.Sven@trivadis.com [email protected]@trivadis.com [email protected]
Oracle Database New Features 11g for Developers – SQL 37 © 2008
Regular Expression COUNT
� In 11g a new function – REGEXP_COUNT – is introduced� REGEXP_COUNT (string, pattern [,pos [, param]]): NUMBER
� Used for counting the number of occurences of a pattern in a string
SQL> SELECT * FROM redemo;
TEXT------------------------------------------------------------------In and this row and are and a lot of and and that and are not and…And in this sentence is no inappropriate and
SQL> SELECT regexp_count(text, 'and', 1, 'i') AS occurrence 2 FROM redemo;
OCCURENCE---------
72
Oracle Database New Features 11g for Developers – SQL 38 © 2008
Named and Mixed Notation Function Call in SQL
� Calls to PL/SQL functions can now be done in named or mixed notationSQL> CREATE OR REPLACE PROCEDURE demo (p1 NUMBER, 2 p2 NUMBER := 0,3 p3 NUMBER) 4 RETURN NUMBER IS5 BEGIN6 RETURN p1 + p2 + nvl(p3,0);7 END;8 /
Function Created
SQL> SELECT demo(sal, p3 => comm) FROM emp;
Oracle Database New Features 11g for Developers – SQL 39 © 2008
SQL & PL/SQL – Core Messages
Knowledge transfer is only the beginning. Knowledge application is what counts.
� Static PIVOT not very useful
� Enhanced ADD COLUMN can cause better performance but be careful with nullable columns and row-triggers
� Virtual Columns provide interesting functionality also for other features
� Improved trigger handling & more trigger types
� Remove dynamic SQL restrictions
� Reduce unnecessary object invalidation
� Really simple native compilation