Presentation is loading. Please wait.

Presentation is loading. Please wait.

Best Practices in PL/SQL

Similar presentations


Presentation on theme: "Best Practices in PL/SQL"— Presentation transcript:

1 Best Practices in PL/SQL
Karthikeyan M .

2 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 .

3 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 .

4 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 .

5 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 .

6 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 ? .

7 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. .

8 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 .

9 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 .

10 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 .

11 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; .

12 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” .

13 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 .

14 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 .

15 Books/ Materials “Beginning Oracle Programming “
Sean Dillon, Christopher Beck , Thomas Kyte “Code Complete” by Steve McConnell .


Download ppt "Best Practices in PL/SQL"

Similar presentations


Ads by Google