Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Useful Supplied
PL/SQL Packages
1
Maria Colgan
Mast...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_SESSION.SLEEP()
2
Accessible sleep function wi...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_SESSION.SLEEP()
3
Accessible sleep function wi...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_UTILITY.EXPAND_SQL_TEXT
4
SELECT view_name
FRO...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_UTILITY.EXPAND_SQL_TEXT
set serveroutput on
DE...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_UTILITY.EXPAND_SQL_TEXT
SELECT "A1"."order_id"...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_UTILITY.EXPAND_SQL_TEXT
set serveroutput on
DE...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_UTILITY.EXPAND_SQL_TEXT
SELECT "A1"."order_id"...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_ADVANCED_REWRITE
9
Fixing the unfixable SQL st...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_ADVANCED_REWRITE
10
Fixing the unfixable SQL s...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_ADVANCED_REWRITE
11
Fixing the unfixable SQL s...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_ADVANCED_REWRITE
12
Fixing the unfixable SQL s...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_ADVANCED_REWRITE
13
Fixing the unfixable SQL s...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_ADVANCED_REWRITE
14
Fixing the unfixable SQL s...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_COMPARISON
• Trying to figure out why things b...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_COMPARISON
• Execute the
COMPARE function
to p...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DBMS_COMPARISON
17
SELECT c.COLUMN_NAME, r.INDEX_VA...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Many More Powerful Supplied PL/SQL Packages
• Oracl...
Upcoming SlideShare
Loading in …5
×

Useful PL/SQL Supplied Packages

This is one of the 15 minute "TED" style talk presented as part of the Database Symposium at the ODTUG Kscope18 conference. In this presentation @SQLMaria shares details on 4 useful supplied PL/SQL package with the Oracle Database

  • Be the first to comment

Useful PL/SQL Supplied Packages

  1. 1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Useful Supplied PL/SQL Packages 1 Maria Colgan Master Product Manager Oracle Database Server Technologies June 2018 JEFF @SQLMaria
  2. 2. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_SESSION.SLEEP() 2 Accessible sleep function within PL/SQL SQL> set timing on; SQL> DECLARE v_start date; v_end date; BEGIN v_start := SYSDATE; -- Sleep for 10 seconds dbms_lock.sleep(10); v_end := SYSDATE; END; / PL/SQL procedure successfully completed. Elapsed: 00:00:10.02 • DBMS_LOCK includes other, more sensitive methods • Therefore not granted to public • Requires DBA intervention to get a accessible sleep function in PL/SQL
  3. 3. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_SESSION.SLEEP() 3 Accessible sleep function within PL/SQL SQL> set timing on; SQL> DECLARE v_start date; v_end date; BEGIN v_start := SYSDATE; -- Sleep for 10 seconds DBMS_SESSION.SLEEP(10); v_end := SYSDATE; END; / PL/SQL procedure successfully completed. Elapsed: 00:00:10.02 • Doesn’t require GRANT anymore – Before required explicit grant on DBMS_LOCK – DBMS_SESSION granted to public • Compatible with DBMS_LOCK.SLEEP – You can search/replace • Introduced thanks to the Oracle community – https://community.oracle.com/ideas/4852
  4. 4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_UTILITY.EXPAND_SQL_TEXT 4 SELECT view_name FROM user_views WHERE view_name LIKE 'SALE%'; VIEW_NAME ------------------- SALES_REPORTING2_V SALES_REPORTING_V Determining if this really is the right view to use 12c • Views can be a useful way to hide complexity from developers • But they can also cause problems • It's easy to write apparently simple statements, that result in extremely complex SQL being sent to the database • The DBMS_UTILITY.EXPAND_SQL_TEXT procedure expands references to views, turning them into subqueries in the original statement
  5. 5. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_UTILITY.EXPAND_SQL_TEXT set serveroutput on DECLARE l_clob CLOB; BEGIN dbms_utility.Expand_sql_text( input_sql_text =>'SELECT * FROM SALES_REPORTING_V', output_sql_text => l_clob); dbms_output.Put_line(l_clob); END; / 5
  6. 6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_UTILITY.EXPAND_SQL_TEXT SELECT "A1"."order_id" "ORDER_ID", "A1"."time_id" "TIME_ID", "A1"."cust_id" "CUST_ID", "A1"."prod_id" "PROD_ID" FROM (SELECT "A3"."order_id" "ORDER_ID", "A3"."time_id" "TIME_ID", "A3"."cust_id" "CUST_ID", "A3"."prod_id" "PROD_ID" FROM "SH"."sales" "A3", "SH"."products" "A2" WHERE "A3"."prod_id" = "A2"."prod_id") "A1" 6
  7. 7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_UTILITY.EXPAND_SQL_TEXT set serveroutput on DECLARE l_clob CLOB; BEGIN dbms_utility.Expand_sql_text( input_sql_text =>'SELECT * FROM SALES_REPORTING2_V', output_sql_text => l_clob); dbms_output.Put_line(l_clob); END; / 7
  8. 8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_UTILITY.EXPAND_SQL_TEXT SELECT "A1"."order_id" "ORDER_ID", "A1"."time_id" "TIME_ID", "A1"."cust_id" "CUST_ID", "A1"."prod_id" "PROD_ID" FROM (SELECT "A2"."order_id" "ORDER_ID", "A2"."time_id" "TIME_ID", "A2"."cust_id" "CUST_ID", "A2"."prod_id" "PROD_ID" FROM "SH"."sales" "A2”) "A1" 8
  9. 9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_ADVANCED_REWRITE 9 Fixing the unfixable SQL statement • You’ve been told you need to “fix” a SQL statement that is no longer returning results in the correct order • But you can’t change the application code directly as that would require down time • What do you do? SQL> SELECT x, y, sum(z) FROM t GROUP BY x, y; X Y SUM(Z) ---------- ---------- ---------- 1 4 110 2 3 84 10g Something changes and now you get SQL> SELECT x, y, sum(z) FROM t GROUP BY x, y; X Y SUM(Z) ---------- ---------- ---------- 2 3 84 1 4 110
  10. 10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_ADVANCED_REWRITE 10 Fixing the unfixable SQL statement Step 1 • Check what changed with the execution plan ORIGINIAL PLAN ----------------------------------- | Id | Operation | Name | ----------------------------------- | 0 | SELECT STATEMENT | | | 1 | HASH GROUP BY | | | 2 | TABLE ACCESS FULL| T | ----------------------------------- NEW PLAN -------------------------------------- | Id | Operation | Name | -------------------------------------- | 0 | SELECT STATEMENT | | | 1 | SORT GROUP BY NOSORT| | | 2 | INDEX FULL SCAN |T_IDX | --------------------------------------
  11. 11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_ADVANCED_REWRITE 11 Fixing the unfixable SQL statement Step 2 • Check the order of the columns in the index SQL> SELECT column_position, column_name FROM user_ind_columns WHERE index_name='T_IDX' ORDER BY column_position; COLUMN_POSITION COLUMN --------------- ------ 1 Y 2 X 3 Z • New index returns the rows in a different order because its sorted on column Y rather than X
  12. 12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_ADVANCED_REWRITE 12 Fixing the unfixable SQL statement Step 3 • Create a view v that includes an order by clause so the query results will always be order based on the values column x SQL> CREATE OR REPLACE VIEW v 2 AS 3 SELECT x, y, sum(z) "SUM(Z)" 4 FROM t 5 GROUP BY x, y 6 ORDER BY x, y; View created.
  13. 13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_ADVANCED_REWRITE 13 Fixing the unfixable SQL statement Step 4 • Use the DBMS_ADVANCED_REWRITE procedure to map the original query to a simple SELECT * FROM v; SQL> BEGIN 2 sys.dbms_advanced_rewrite.declare_rewrite_equivalence( 3 name => 'DEMO_TIME', 4 source_stmt => 'SELECT x, y, sum(z) FROM t GROUP BY x, y', 5 destination_stmt => 'SELECT * FROM v', 6 validate => FALSE, 7 rewrite_mode => 'TEXT_MATCH'); 8 END; 9 / PL/SQL procedure successfully completed.
  14. 14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_ADVANCED_REWRITE 14 Fixing the unfixable SQL statement • Each time our original SQL statement is issued the DBMS_ADVANCED_REWRITE procedure rewrites it to be SELECT * FROM v; • Always get the query output in an order list SQL> SELECT x, y, sum(z) FROM t GROUP BY x, y; X Y SUM(Z) ---------- ---------- ---------- 1 4 110 2 3 84 ----------------------------------- | Id | Operation | Name | ----------------------------------- | 0 | SELECT STATEMENT | | | 1 | VIEW | V | | 2 | SORT GROUP BY | | | 3 | INDEX FULL SCAN|T_IDX | -----------------------------------
  15. 15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_COMPARISON • Trying to figure out why things behave differently in production then they do in test can be time consuming and painful • The DBMS_COMPARISON package allows you to compare objects, schemas or data between databases or schemas • For a table comparison you do need a unique index on both tables • Begin by creating the comparison 15 Figuring out why production is different to test SQL> BEGIN DBMS_COMPARISON.CREATE_COMPARISON ( comparison_name => 'COMP_SALES', schema_name => 'SH', object_name => 'SALES', dblink_name => 'orcl2_test' ); END; / 11g
  16. 16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_COMPARISON • Execute the COMPARE function to perform the compare operation • Returns a boolean to say if there is or is not consistency • The scan_id allows you to find out what the differences are 16 SQL> DECLARE scan_info DBMS_COMPARISON.COMPARISON_TYPE; BEGIN IF NOT DBMS_COMPARISON.COMPARE ( comparison_name => 'COMP_SALES' , scan_info => scan_info , perform_row_dif => TRUE ) THEN DBMS_OUTPUT.PUT_LINE('Scan ID:'|| scan_info.scan_id); END IF; END; / Scan ID: 1
  17. 17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DBMS_COMPARISON 17 SELECT c.COLUMN_NAME, r.INDEX_VALUE, case when r.LOCAL_ROWID is null then ‘No’ else 'Yes’ end LOCAL, Case when r.REMOTE_ROWID is null then 'No’ else 'Yes’ end REMOTE FROM USER_COMPARISON_COLUMNS c, USER_COMPARISON_ROW_DIF r, USER_COMPARISON_SCAN s WHERE c.COMPARISON_NAME = 'COMP_SALES‘ AND c.INDEX_COLUMN = 'Y’ AND r.STATUS = 'DIF’ AND c.COMPARISON_NAME = r.COMPARISON_NAME AND r.SCAN_ID = s.SCAN_ID AND s.SCAN_ID = 1 ORDER BY r.INDEX_VALUE; COLUMN_NAME INDEX_VALUE LOCAL REMOTE ----------- ----------- ----- ------ TAX_CODE 0.05 No Yes Test environment has an different tax code
  18. 18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Many More Powerful Supplied PL/SQL Packages • Oracle Database 18c provides 300 PL/SQL packages • Some of my favorites include: – DBMS_STATS – DBMS_SHARED_POOL – DBMS_SPM – DBMS_AUTO_TASK_ADMIN – DBMS_SQLTUNE • Check out more details in the PL/SQL Packages and Types Reference 18

×