21st Century PLSQL

Download 21st Century PLSQL

Post on 19-Jan-2016




3 download

Embed Size (px)


  • 21st Century PL/SQL

    Breaking out of Oracle7, Oracle8 and Oracle8i programming ruts

    Steven Feuersteinsteven@stevenfeuerstein.comwww.oracleplsqlprogramming.comAll non-technical views expressed are those of Steven Feuerstein and do not necessarily (and not likely!) reflect those of Oracle Corporation.

  • New stuff is good stuff.Major Oracle10g PL/SQL compiler upgradesOptimizing compiler, compile-time warningsCollectionsString-based indexes, multi-level collections, high level set operations for nested tables, table functions, and more.Advanced topics in Dynamic SQLDynamic PL/SQL, method 4 dynamic SQL, when and how to use DBMS_SQL.Handy new built-in package functionalitySchedule jobs, send email, recompile codeMiscellaneous wonderful capabilities in DBMS_OUTPUT and DBMS_UTILITY.

  • "All" about Steven FeuersteinBachelor's degree in mathematics (1980), with three computer 101 classes to my name (a self-taught programmer).Five years with Oracle Corporation (1987 - 1992), with too much time spent helping salespeople sell. Life is too short...Author/co-author of nine texts on PL/SQL, most notably Oracle PL/SQL ProgrammingSenior Technology Advisor for Quest SoftwareI live in Chicago with one wife (Veva), two sons (Chris and Eli), and three cats (Sister, Moshe and Mica). www.stevenfeuerstein.com

  • Ten Years of Writing on the Oracle PL/SQL Language

  • Software used (or recommended)You can download all my training materials and demonstration scripts from:http://oracleplsqlprogramming.com/resources.htmlToad and/or SQL Navigator: make sure you've got a top-notch IDE, otherwise you are wasting lots of time.Ounit and utPLSQL, software for unit testing of PL/SQL code at www.ounit.com.MasterMind and Set: have fun while keeping your brain tuned up.Qnxo, active mentoring software: a repository of reusable and templated code, www.qnxo.com.plsql_ides.txt

  • Qnxo is..."Quality iN, eXcellence Out"A searchable, customizable repository for reusable code and templates.Starter set: "PL/SQL by Feuerstein"You can create your own toolboxes and libraries.A flexible code generator that helps you avoid writing tedious, repetitive code.An error manager for PL/SQL-based applications.www.qnxo.com

  • Qnxo is....NOT an integrated development environment, aka IDE, aka editor, for PL/SQL programming.It complements Toad, SQL Navigator, PL/SQL Developer, etc.NOT needed in order to benefit from this class. Qnxo contains a repository of examples.The Qnxo backend reflects my latest (and, I believe, best) thinking on how to build high quality PL/SQL applications.NOT free. There is a 30-day trial version available at www.qnxo.com.One student will win a one year subscription to Qnxo.

  • Major Oracle10g PL/SQL compiler upgradesOptimizing compilerRecompile in 10g and experience 100% improvement in performance (results may vary).Compile-time warningsNow the PL/SQL compiler tells you more than simply compilation errors.

  • Wow! An optimizing compiler!Yes, the PL/SQL compiler now has the ability to automatically optimize your code.Possible rearrangements to the code itself (under the covers).You can choose the level of optimization through the plsql_optimize_level setting:2 Most aggressive, maximum possible code transformations, biggest impact on compile time. [default]1 Smaller scale change, less impact on compile times0 Pre-10g compilation without optimization ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL = 1; 10g_optimize_cfl.sqlOracle10g

  • Learn about the PL/SQL optimizerPL/SQL Just Got Faster Explains the workings of the PL/SQL compiler and runtime system and shows how major improvements on this scale are indeed possible.PL/SQL Performance Measurement Harness Describes a performance experiment whose conclusion is the large factors quoted above. Weve provided a downloadable kit to enable you to repeat the experiment yourself.Freedom, Order, and PL/SQL Optimization Intended for professional PL/SQL programmers, explores the use and behavior of the new compiler.PL/SQL Performance Debunking the MythsRe-examines some old notions about PL/SQL performance.http://www.oracle.com/technology/tech/pl_sql/htdocs/new_in_10gr1.htm

  • Optimizing compiler detailsOracle retains optimizer settings on a module-by-module basis. When you recompile a particular module with non-default settings, the settings will "stick," allowing you to recompile later using REUSE SETTINGS. For example:


  • Wow! Compile-time warnings!You can now enable compiler warnings, helping you avoid nuisance issues in your code.Generally, these are not severe errors, but potential problems with code structure or performance.To use compiler warnings, you must turn them on in your session.[ENABLE | DISABLE | ERROR]:[ALL|SEVERE|INFORMATIONAL|PERFORMANCE|warning_number]

    REM To enable all warnings in your session execute:ALTER SESSION SET plsql_warnings = 'enable:all;

    REM If you want to enable warning message number 06002 and all warnings in REM the performance category, and treat warning 5005 as a "hard" compile error: ALTER SESSION SET plsql_warnings = 'enable:06002', 'enable:performance', 'ERROR:05005'; Oracle10g

  • Compiler time warnings - exampleCheck for unreachable end code.SQL> CREATE OR REPLACE PROCEDURE unreachable_code IS2 x NUMBER := 10;3 BEGIN4 IF x = 10 THEN5 x := 20;6 ELSE7 x := 100; -- unreachable code8 END IF;9 END unreachable_code;10 /SP2-0804: Procedure created with compilation warningsSQL> show errErrors for PROCEDURE UNREACHABLE_CODE:LINE/COL ERROR-------- -------------------------------------7/7 PLW-06002: Unreachable code


  • Useful data dictionary viewsALL_PLSQL_OBJECT_SETTINGSNew to Oracle10g, this view provides information about the characteristics of a PL/SQL object that can be modified through the ALTER-SET DDL command, such as the optimization level, debug settings and more.

    ALL_PROCEDURESIntroduced in Oracle9i, this view lists all functions and procedures (stand-alone or packaged), along with associated properties, including whether or not a function is pipelined, parallel enabled, or aggregate.

  • Using the object settings viewShow all the program units that are not fully leveraging code optimization:

    Show all objects which have had one or more compile-time warnings disabled:SELECT owner, name FROM all_plsql_object_settings WHERE plsql_optimize_level IN (1,0);SELECT owner, NAME, plsql_warnings FROM all_plsql_object_settings WHERE plsql_warnings LIKE '%DISABLE%' AND owner NOT IN ('SYS', 'SYSTEM');

  • Using the ALL_PROCEDURES viewThe following query will show the AUTHID status (DEFINER or CURRENT_USER) for all programs in the specified package.In general, this view is handy because it does list each of the individual program units in a package specification.Previously, this information could only be deduced from ALL_ARGUMENTS.SELECT AUTHID , p.object_name program_name , procedure_name subprogram_name FROM all_procedures p, all_objects o WHERE p.owner = o.owner AND p.object_name = o.object_name AND p.object_name LIKE '&1'ORDER BY AUTHID, procedure_name;program_start_end.ddlpackage_analyzer.*

  • PL/SQL CollectionsEver wonder why PL/SQL doesn't have good, old-fashioned arrays?It's a good question, and one that Oracle seems to answer as follows:Who needs arrays, when you have collections?

  • What we will cover on collectionsBrief review of basic functionalityIndexing collections by stringsWorking with collections of collectionsBulk processing with FORALL and BULK COLLECTTable functions and pipelined functionsMULTISET operators for nested tables

  • What is a collection?A collection is an "ordered group of elements, all of the same type." (PL/SQL User Guide and Reference)That's a very general definition; lists, sets, arrays and similar data structures are all types of collections.Each element of a collection may be addressed by a unique subscript, usually an integer but in some cases also a string.Collections are single-dimensional, but you can create collections of collections to emulate multi-dimensional structures.abcdefsfqrrrswq...1 2 3 4 22 23

  • Why use collections?Emulate bi-directional cursors, which are not yet supported within PL/SQL.Bypass mutating table restrictions in DB triggers.Avoid many scenarios that produce "Snapshot too old" and "Rollback segment too small" errors. Using BULK COLLECT and FORALL....Parallelize execution of PL/SQL functions inside SQL statements. With table functions....Dramatically improve multi-row querying, inserting, updating and deleting the contents of tables. Combined with BULK COLLECT and FORALL....Cache data in program memory for faster access. The difference between PGA and SGA....

  • System Global Area (SGA) of RDBMS InstanceRefresher: PL/SQL in Shared MemoryShared PoolLarge PoolReserved Poolshow_empscalc_totalsupd_salariesSelect * from empShared SQLPre-parsedUpdate emp Set sal=...Library cacheSession 1Session 2mysess.pkgSess2.sql

  • Three Types of CollectionsAssociative arrays (aka index-by tables) Similar to hash tables in other languages, allows you to access elements via arbitrary subscript values. Nested tables Can be defined in PL/SQL and SQL. Use to store large amounts of persistent data in the column of a table.Required for some features, such as table functionsVarrays (aka variable size arrays)Can be defined in PL/SQL and SQL; useful for defining small lists in columns of relational tables.

  • About Associative ArraysUnbounded, practically speaking. Valid row numbers range from -2,147,483,647