Oracle PL/SQL Practices. Critical elements of PL/SQL Best Practices Build your development toolbox Unit test PL/SQL programs Optimize SQL in PL/SQL programs.

Slides:



Advertisements
Similar presentations
PL/SQL : Stop making the same performance mistakes
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
April 14, 2004 Next Information Systems 1 Bulk Collections and Inserts in Oracle 9i & 10g Simay Alpöge Next Information Systems, Inc.
Copyright © 200\8 Quest Software High Performance PL/SQL Guy Harrison Chief Architect, Database Solutions.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
Agenda Journalling More Embedded SQL. Journalling.
Cursor and Exception Handling By Nidhi Bhatnagar.
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
Oracle PL/SQL Programming Steven Feuerstein All About the (Amazing) Function Result Cache of Oracle Database 11g.
Advanced PL/SQL and Oracle ETL Doug Cosman Senior Oracle DBA SageLogix, Inc. Open World 2003.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
© Dennis Shasha, Philippe Bonnet – 2013 Communicating with the Outside.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
1 Chapter 13 Parallel SQL. 2 Understanding Parallel SQL Enables a SQL statement to be: – Split into multiple threads – Each thread processed simultaneously.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
implicit and an explicit cursor
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Advanced Databases More Advanced PL/SQL Programing 1.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Preface IIntroduction Course Objectives I-2 Oracle Complete Solution I-3 Course Agenda I-4 Tables Used in This Course I-5 The Order Entry Schema I-6 The.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
11 Copyright © 2009, Oracle. All rights reserved. Enhancing ETL Performance.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Best Practices in PL/SQL
Advanced PL/SQL Programing
CS 440 Database Management Systems
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
The PROCESS of Queries John Deardurff
Chapter 8 Advanced SQL.
Handling Data in PL/SQL Blocks
Prof. Arfaoui. COM390 Chapter 7
Database Programming Using Oracle 11g
Presentation transcript:

Oracle PL/SQL Practices

Critical elements of PL/SQL Best Practices Build your development toolbox Unit test PL/SQL programs Optimize SQL in PL/SQL programs Manage errors effectively and consistently Write readable, maintainable code

High Level, High Impact Important principles... Assume everything will change. Aim for a single point of definition (a SPOD). Top four tips.... Drink lots of water. Write tiny chunks of code. Stop writing so much SQL. Stop guessing and start testing.

Let's read some code! Move blocks of complex code into the declaration section Replace them with descriptive names The code is now easier to read and maintain You can more easily identify areas for improvement

Anchor Declarations of Variables

Always Fetch into Cursor Records

Input Parameters

Select-in multi-columns

Update-set multi-columns

Avoid SQL-PL/SQL Naming Conflicts

Row Count

Performance Discussion

dynamic sql without bind variable DECLARE cur SYS_REFCURSOR; tmp crm_partner%ROWTYPE; BEGIN FOR j IN LOOP OPEN cur FOR 'SELECT * FROM crm_partner p WHERE rownum <= '|| j; LOOP FETCH cur INTO tmp; EXIT WHEN cur%NOTFOUND; NULL; END LOOP; CLOSE cur; END LOOP; END; / PL/SQL procedure successfully completed Executed in seconds

dynamic sql with bind variable DECLARE cur SYS_REFCURSOR; tmp crm_partner%ROWTYPE; BEGIN FOR j IN LOOP OPEN cur FOR 'SELECT * FROM crm_partner p WHERE rownum <= :i' USING j; LOOP FETCH cur INTO tmp; EXIT WHEN cur%NOTFOUND; NULL; END LOOP; CLOSE cur; END LOOP; END; / PL/SQL procedure successfully completed Executed in seconds

dynamic sql fetch all one time DECLARE cur SYS_REFCURSOR; TYPE type_tab IS TABLE OF crm_partner%ROWTYPE; tmp type_tab; BEGIN FOR j IN LOOP OPEN cur FOR 'SELECT * FROM crm_partner p WHERE rownum <= :i' USING j; FETCH cur BULK COLLECT INTO tmp; FOR i IN 1.. tmp.count LOOP NULL; END LOOP; CLOSE cur; END LOOP; END; / PL/SQL procedure successfully completed Executed in seconds

static cursor DECLARE CURSOR get_data(p_row_no IN NUMBER) IS SELECT * FROM crm_partner p WHERE rownum <= p_row_no; BEGIN FOR j IN LOOP FOR i IN get_data(j)LOOP NULL; END LOOP; END; / PL/SQL procedure successfully completed Executed in 0.39 seconds

static cursor fetch all one time DECLARE CURSOR cur(p_row_no IN NUMBER) IS SELECT * FROM crm_partner p WHERE rownum <= p_row_no; TYPE type_tab IS TABLE OF crm_partner%ROWTYPE; tmp type_tab; BEGIN FOR j IN LOOP OPEN cur(j); FETCH cur BULK COLLECT INTO tmp; FOR i IN 1.. tmp.count LOOP NULL; END LOOP; CLOSE cur; END LOOP; END; / PL/SQL procedure successfully completed Executed in seconds

dml single insert/select or bulk collect/forall You should do it in a single SQL statement if at all possible. If you cannot do it in a single SQL Statement, then do it in PL/SQL. If you cannot do it in PL/SQL, try a Java Stored Procedure. If you cannot do it in Java, do it in a C external procedure. If you cannot do it in a C external routine, you might want to seriously

Bulk collect-For-Insert ops$tkyte%ORA11GR2> declare 2 type array is table of t1%rowtype index by binary_integer; 3 cursor test_curs is select * from t1; 4 l_data array; 5 begin 6 open test_curs; 7 loop 8 fetch test_curs bulk collect into l_data limit 2000; 9 for i in 1..l_data.COUNT 10 LOOP 11 insert into t2 values l_data(i); 12 END loop; 13 exit when test_curs%notfound; 14 END LOOP; 15 close test_curs; 16 end; 17 / PL/SQL procedure successfully completed.

Bulk collect-Forall-Insert ops$tkyte%ORA11GR2> declare 2 type array is table of t1%rowtype index by binary_integer; 3 cursor test_curs is select * from t1; 4 l_data array; 5 begin 6 open test_curs; 7 loop 8 fetch test_curs bulk collect into l_data limit 2000; 9 forall i in 1..l_data.COUNT 10 insert into t3 values l_data(i); 11 exit when test_curs%notfound; 12 END LOOP; 13 close test_curs; 14 end; 15 /

Insert-into-from insert into t4 select * from t1;

Switching between PL/SQL and SQL engines

Conventional Bind

Bulk Bind

Direct path inserts NOTE: a) it isn't necessarily faster in general. It does a direct path load to disk - bypassing the buffer cache. There are many cases - especially with smaller sets - where the direct path load to disk would be far slower than a conventional path load into the cache. b) a direct path load always loads above the high water mark, since it is formatting and writing blocks directly to disk - it cannot reuse any existing space. Think about this - if you direct pathed an insert of a 100 byte row that loaded say just two rows - and you did that 1,000 times, you would be using at least 1,000 blocks (never reuse any existing space) - each with two rows. Now, if you did that using a conventional path insert - you would get about 70/80 rows per block in an 8k block database. You would use about 15 blocks. Which would you prefer? c) you cannot query a table after direct pathing into it until you commit. (current session) d) how many people can direct path into a table at the same time? One - one and only one. It would cause all modifications to serialize. No one else could insert/update/delete or merge into this table until the transaction that direct paths commits. insert /*+ APPEND*/ into t4 select * from t1; Direct path inserts should be used with care, in the proper circumstances. A large load - direct path, but most of the time - conventional path.

direct path vs. conventional path

Thanks! Questions & Answers Shawn Huang (黄其晟) Data Architect Desk: Mobile: