1 / 37

SAGE Computing Services Customised Oracle Training Workshops and Consulting

Get the most out of PL/SQL with hints, tricks, and forgotten features. This workshop covers bulk binds, implicit cursors, table functions, reminders, NOCOPY, scalar subqueries, functions in the WITH clause, PGA memory issues, and when to use PL/SQL instead of SQL.

gustavoa
Download Presentation

SAGE Computing Services Customised Oracle Training Workshops and Consulting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 forgotten Kate Marshall Systems Consultant

  2. Making the most of PL/SQL • Bulk binds • Implicit cursors • Table functions • Reminders • NOCOPY • Scalar subqueries • Functions in the WITH clause • PGA • Using PL/SQL when we should just use SQL

  3. Making the most of PL/SQL • Bulk binds • Implicit cursors • Table functions • Reminders • NOCOPY • Scalar subqueries • Functions in the WITH clause • PGA • Using PL/SQL when we should just use SQL

  4. Oracle Server PL/SQL Runtime Engine SQL Runtime Engine PL/SQL Block Procedural statement executor SQL statement executor FOR r_rec IN c_rec LOOP UPDATE emp SET salary = salary * 1.1 WHERE empno = r_rec.empno; END LOOP;

  5. Bulk Binding • We can use bulk binding to pass collections of rows between the SQL engine and PLSQL engine • Reduce overheads from context switching • If a single DML statement is not possible and you need to process many rows individually use bulk binds. • If the collection has 20 elements, bulk binding lets you perform the equivalent of 20 SELECT, INSERT, UPDATE, or DELETE statements using a single operation. • BULK COLLECT • SELECT statements that fetch multiple rows with a single fetch into a collection before returning to the PLSQL engine • FORALL • DML statements that use collections to change multiple rows • The keyword FORALL lets you run multiple DML statements very efficiently. It can only repeat a single DML statement, unlike a general-purpose FOR loop.

  6. BULK COLLECT • SELECT statements that fetch multiple rows with a single fetch into a collection before returning to the PLSQL engine • FORALL • DML statements that use collections to change multiple rows

  7. Cursor FOR Loop • There are occasions when a cursor FOR loop is better than a BULK COLLECT • When you are performing complex processing on each row within the loop, and perhaps need to exit the loop processing • BULK COLLECT will use up PGA memory to store the collections. FETCH employees_cur BULK COLLECT INTO l_employee_ids LIMIT 100;

  8. Implicit v Explicit Cursors • EXPLICIT • Declare cursor • Open loop • Open cursor • Fetch cursor • Close cursor • End Loop • IMPLICIT • Open loop • Execute cursor • End Loop

  9. Implicit v Explicit Cursors • PL/SQL is an interpreted language so every extra line of code adds to the total processing time.  • Choose EXPLICIT only if you are planning to reuse the cursor

  10. Table Functions • Data Loading INSERT INTO employees_new (emp_no, first_name, last_name, salary) SELECT emp_no, first_name, last_name, salary FROM TABLE(emppivot_pkg.pipe_emp(CURSOR(SELECT * FROM employees))); FUNCTION pipe_emp(p_source_data IN emppivot_pkg.emptable_rct ) RETURN emppivot_nttPIPELINED;

  11. Table Functions • Data Loading INSERT INTO employees_new (emp_no, first_name, last_name, salary) SELECT emp_no, first_name, last_name, salary FROM TABLE(emppivot_pkg.pipe_emp(CURSOR(SELECT * FROM employees))); FUNCTION pipe_emp(p_source_data IN emppivot_pkg.emptable_rct ) RETURN emppivot_nttPIPELINED; • Avoid Report Redundancy • Remove the need for similar Apex IR reports • Move the complexity of the query into a database function that determines the query required and returns a collection back to the TABLE function

  12. Reminders • Short circuit evaluation • COALESCE v NVL SELECT NVL(expr1, expr2) FROM some_table; SELECT COALESCE(expr1, expr2, ..., exprN) FROM some_table;

  13. Reminders • Short circuit evaluation • COALESCE v NVL SELECT NVL(expr1, expr2) FROM some_table; SELECT COALESCE(expr1, expr2, ..., exprN) FROM some_table; • CASE CASEwhen n_numb = 1 then v_status := ‘very small’;when n_numb < 4 then v_status := ‘small’;when n_numb = 5 then v_status := ‘even’;when n_numb > 4 then v_status := ‘large’;else v_status := ‘very large’; END CASE;

  14. Reminders • Short circuit evaluation • COALESCE v NVL SELECT NVL(expr1, expr2) FROM some_table; SELECT COALESCE(expr1, expr2, ..., exprN) FROM some_table; • CASE CASEwhen n_numb = 1 then v_status := ‘very small’;when n_numb < 4 then v_status := ‘small’;when n_numb = 5 then v_status := ‘even’;when n_numb > 4 then v_status := ‘large’;else v_status := ‘very large’; END CASE; • Group related subprograms into packages

  15. Reminders • Avoid implicit datatype conversions • Speed

  16. Reminders • Avoid implicit datatype conversions • Speed • Error WHERE job_no = 1311100; • CREATE TABLE job ( • job_noVARCHAR2(12)...

  17. Reminders • Avoid implicit datatype conversions • Speed • Error • Incorrect

  18. Reminders • Function Based Indexes • Use subselects to reduce unnecessary function calls .

  19. NOCOPY • OUT and IN OUT parameters passed by value

  20. Scalar Subqueries • SELECT clause • WHERE clause

  21. Functions in the WITH CLAUSE

  22. PGA Memory Issues • The Program Global Area is used to hold: • Bind variables • PL/SQL arrays • SQL work areas (hash operations, sorting) • Cursors and cursor state information • Our extra use of collections will use extra PGA memory • Ensure that the PGA memory size is appropriate to actions being performed • Remember the LIMIT clause when using bulk binds

  23. Using PLSQL when you should just use SQL • Regular Expressions • Remove unnecessary loops, functions and conditional processing with regular expressions • https://regex101.com/

  24. v_string 'www.sagecomputing.com.au' SELECT :v_string, LEVEL AS element_no, REGEXP_SUBSTR(:v_string, '[^\.]+', 1, LEVEL) AS single_element FROM dual CONNECT BY LEVEL <= REGEXP_COUNT(:v_string, '\.') + 1;

  25. Using PLSQL when you should just use SQL • Methods previously mentioned can reduce context switching load. • Many advanced data operations can be done without using PL/SQL and are readily available as a SQL built-in function.

  26. ROLLUP and CUBE • These two operators allow a select statement to calculate totals and a grand total at any level • Totals for • Job and Stage • Job • Grand Total

  27. CUBE • Totals for • Job and Stage • Job • Grand Total • Job and Trade • Stage and Trade • Stage Total • Trade Total

  28. CUBE • Totals for • Job and Stage • Job • Grand Total • Job and Trade • Stage and Trade • Stage Total • Trade Total

  29. Other Analytic Functions • RANK and DENSE RANK • RATIO_TO_REPORT • LAG and LEAD • LISTAGG

  30. Other Analytic Functions • RANK and DENSE RANK • RATIO_TO_REPORT • LAG and LEAD • LISTAGG

  31. Other Analytic Functions • RANK and DENSE RANK • RATIO_TO_REPORT • LAG and LEAD • LISTAGG

  32. Other Analytic Functions • RANK and DENSE RANK • RATIO_TO_REPORT • LAG and LEAD • LISTAGG

  33. Making the most of PL/SQL • Bulk binds • Implicit cursors • Table functions • Reminders • NOCOPY • Scalar subqueries • Functions in the WITH clause • PGA • Using PL/SQL when we should just use SQL

  34. Making the most of PL/SQL • Bulk binds • Implicit cursors • Table functions • Reminders • NOCOPY • Scalar subqueries • Functions in the WITH clause • PGA • Using PL/SQL when we should just use SQL

  35. SAGE Computing Services Customised Oracle Training Workshops and Consulting Presentations are available from our website: http://www.sagecomputing.com.au enquiries@sagecomputing.com.au kate.marshall@sagecomputing.com.au

More Related