Best Practices in PL/SQL

Slides:



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

April 14, 2004 Next Information Systems 1 Bulk Collections and Inserts in Oracle 9i & 10g Simay Alpöge Next Information Systems, Inc.
Chapter 4B: More Advanced PL/SQL Programming
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Oracle PL/SQL Injection David Litchfield. What is PL/SQL? Procedural Language / Structured Query Language Oracle’s extension to standard SQL Programmable.
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
PL/SQL and the Table API. Benefits of Server-Side Code Speedy Pizza MENU NAPOLITAINE PIZZA Reduced network traffic Maintainability Data integrity Triggers.
Cursor and Exception Handling By Nidhi Bhatnagar.
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
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.
Stored Procedures Functions Packages
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.
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.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Oracle Data Integrator Procedures, Advanced Workflows.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
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.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Database Application Development using PL/SQL Programming.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
Best Practices for Writing SQL In Oracle PL/SQL Oracle PL/SQL Best Practices Steven Feuerstein Quest Software,
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
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.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Oracle SQL.
PL/SQL MULTIPLE CHOICE QUESTION.
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Listing 9.1 ShowLocalConnection.aspx
UNIT - V STORED PROCEDURE.
SQL PL/SQL Presented by: Dr. Samir Tartir
Database Management Systems 2
Interacting with the Oracle Server
Advanced PL/SQL Programing
PL/SQL Programing : Triggers
PL/SQL week10.
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Chapter 8 Advanced SQL Pearson Education © 2009.
Triggers.
Handling Data in PL/SQL Blocks
Database Programming Using Oracle 11g
Presentation transcript:

Best Practices in PL/SQL Karthikeyan M http://karthikeyanmect.blogspot.com .

Why best practices? Many ways of writing a code Code Best code Will give solution (some how) Time taken and resource consumed ? Best code Use optimal resource Provides quicker solution .

Spend more time in design Modularized Design Bad design Dump your logic in a single procedure Having lots of selects inserts updates and deletes….etc Good design Break your logic into small blocks Grouping related logic as a single block or program Spend more time in design Less time in coding .

Modularize Modularize will Use Packages reduce complexity make your tasks manageable make your resulting code maintainable Use Packages For each major functionality With repeated DML as procedure With repeated select as functions .

Naming convention Follow a standard throughout your code Example Easy to understand Easy for maintain and change Example Local variable l_var_name Procedure parameter p_var_name Global variable g_var_name Follow the standard throughout your code and application .

Avoid hard coding As begin select d_no into an_document_number as CREATE OR REPLACE PROCEDURE GEN_SWIP ( an_document_number IN number, an_serv_item_id IN number, an_srsi_ip_addr_seq IN varchar(20)) As begin select d_no into an_document_number from task; end; CREATE OR REPLACE PROCEDURE GEN_SWIP ( an_document_number IN asap.serv_req_si.document_number%TYPE, an_serv_item_id IN asap.serv_req_si.serv_item_id%TYPE, an_srsi_ip_addr_seq IN asap.srsi_ip_addr.srsi_ip_addr_seq%TYPE,) as begin null; end; What happens if the d_no column changed to varchar2 type ? .

Avoid SQL ! SQL is equivalent to hard-coding Encapsulate your SQL Drag the performance down Difficult to maintain Encapsulate your SQL Selects inside functions DMLs inside procedures And call these inside your business logic We will see an example for this later. .

Repeated SQL as functions This is quintessential for performance Avoid repeating the SQL in different places Hard parsing will be avoided Identify at the time of designing So spend more time in design rather than coding .

Exception handling A common package for Normally Error handling Error logging Log as more a data as possible (debugging) Normally Date of occurrence Error number , name Program name and program data…etc .

Use BULK COLLECT for Queries CREATE OR REPLACE FUNCTION get_a_mess_o_emps (deptno_in IN dept.depno%TYPE) RETURN emplist_t IS emplist emplist_t := emplist_t(); TYPE numTab IS TABLE OF NUMBER; TYPE charTab IS TABLE OF VARCHAR2(12); TYPE dateTab IS TABLE OF DATE; enos numTab; names charTab; hdates dateTab; BEGIN SELECT empno, ename, hiredate BULK COLLECT INTO enos, names, hdates FROM emp WHERE deptno = deptno_in; emplist.EXTEND(enos.COUNT); FOR i IN enos.FIRST..enos.LAST LOOP emplist(i) := emp_t(enos(i), names(i), hiredates(i)); END LOOP; RETURN emplist; END; BULK COLLECT performs bulk bind of results from SQL select statement Returns each selected expression in a table of scalars .

Use the FORALL Statement Instead of the individual FOR operations, you can do this: Used to reduce the context switching between SQL and PL/SQL engine PROCEDURE update_employee (p_name customer.name%TYPE) IS BEGIN FORALL cust_dt IN (select cust_name ,cust_id,cust_age from customer where cust_name= p_name )Loop Begin --bussiness logic End; END; .

Procedural statement executor SQL statement executor Conventional Bind Oracle server PL/SQL Runtime Engine SQL Engine Procedural statement executor PL/SQL block SQL statement executor FOR aDept IN deptlist.FIRST.. deptlist.LAST LOOP DELETE emp WHERE deptno = deptlist(aDept); END LOOP; Performance penalty for many “context switches” .

Procedural statement executor SQL statement executor Enter the “Bulk Bind” Oracle server PL/SQL Runtime Engine SQL Engine Procedural statement executor PL/SQL block SQL statement executor FORALL aDept IN deptlist.FIRST.. deptlist.LAST DELETE emp WHERE deptno = deptlist(aDept); Much less overhead for context switching .

Points to remember Take more time in designing Follow coding standard Avoid hard coding Avoid writing more SQL Write tiny chunk of code Don’t repeat anything .

Books/ Materials “Beginning Oracle Programming “ Sean Dillon, Christopher Beck , Thomas Kyte http://asktom.oracle.com http://www.toadworld.com/sf “Code Complete” by Steve McConnell .