SlideShare ist ein Scribd-Unternehmen logo
1 von 18
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
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
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
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
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
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
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
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
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
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 |
--------------------------------------
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
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.
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.
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 |
-----------------------------------
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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Oracle Course
Oracle CourseOracle Course
Oracle Course
rspaike
 

Was ist angesagt? (20)

The Changing Role of a DBA in an Autonomous World
The Changing Role of a DBA in an Autonomous WorldThe Changing Role of a DBA in an Autonomous World
The Changing Role of a DBA in an Autonomous World
 
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
 
Oracle Data Redaction - UKOUG - TECH14
Oracle Data Redaction - UKOUG - TECH14Oracle Data Redaction - UKOUG - TECH14
Oracle Data Redaction - UKOUG - TECH14
 
Sql parametrized queries
Sql parametrized queriesSql parametrized queries
Sql parametrized queries
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
One Less Thing For DBAs to Worry About: Automatic Indexing
One Less Thing For DBAs to Worry About: Automatic IndexingOne Less Thing For DBAs to Worry About: Automatic Indexing
One Less Thing For DBAs to Worry About: Automatic Indexing
 
Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015 Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015
 
Oracle Course
Oracle CourseOracle Course
Oracle Course
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data Redaction
 
The Database Environment Chapter 8
The Database Environment Chapter 8The Database Environment Chapter 8
The Database Environment Chapter 8
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practices
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data Redaction
 
Oracle DB Performance Tuning Tips
Oracle DB Performance Tuning TipsOracle DB Performance Tuning Tips
Oracle DB Performance Tuning Tips
 
Database Objects
Database ObjectsDatabase Objects
Database Objects
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
PL/SQL 3 DML
PL/SQL 3 DMLPL/SQL 3 DML
PL/SQL 3 DML
 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 

Ähnlich wie Useful PL/SQL Supplied Packages

SSMS-waitstats
SSMS-waitstatsSSMS-waitstats
SSMS-waitstats
E Blake
 

Ähnlich wie Useful PL/SQL Supplied Packages (20)

07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Les11.ppt
Les11.pptLes11.ppt
Les11.ppt
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
plsql les06
 plsql les06 plsql les06
plsql les06
 
Sql views
Sql viewsSql views
Sql views
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
War of the Indices- SQL Server and Oracle
War of the Indices-  SQL Server and OracleWar of the Indices-  SQL Server and Oracle
War of the Indices- SQL Server and Oracle
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should Know
 
Msql
Msql Msql
Msql
 
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
Application_Performance_V1
Application_Performance_V1Application_Performance_V1
Application_Performance_V1
 
War of the Indices- SQL vs. Oracle
War of the Indices-  SQL vs. OracleWar of the Indices-  SQL vs. Oracle
War of the Indices- SQL vs. Oracle
 
SSMS-waitstats
SSMS-waitstatsSSMS-waitstats
SSMS-waitstats
 

Mehr von Maria Colgan

Mehr von Maria Colgan (10)

Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
 
Part4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer HintsPart4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer Hints
 
Ground Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous DatabaseGround Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous Database
 
What to Expect From Oracle database 19c
What to Expect From Oracle database 19cWhat to Expect From Oracle database 19c
What to Expect From Oracle database 19c
 
Oracle Database in-Memory Overivew
Oracle Database in-Memory OverivewOracle Database in-Memory Overivew
Oracle Database in-Memory Overivew
 
JSON and the Oracle Database
JSON and the Oracle DatabaseJSON and the Oracle Database
JSON and the Oracle Database
 
Harnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer HintsHarnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer Hints
 
Oracle optimizer bootcamp
Oracle optimizer bootcampOracle optimizer bootcamp
Oracle optimizer bootcamp
 
What_to_expect_from_oracle_database_12c
What_to_expect_from_oracle_database_12cWhat_to_expect_from_oracle_database_12c
What_to_expect_from_oracle_database_12c
 
Oracle database 12c_and_DevOps
Oracle database 12c_and_DevOpsOracle database 12c_and_DevOps
Oracle database 12c_and_DevOps
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 

Useful PL/SQL Supplied Packages

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

Hinweis der Redaktion

  1. Oracle Database 8i had only 60 pl/sql packages while 18c has 300!
  2. Oracle Database 8i only 60 PL/SQL packages