+ All Categories
Home > Documents > Oracle Database Features Every Developer Should Know About...Oracle Database Features Every...

Oracle Database Features Every Developer Should Know About...Oracle Database Features Every...

Date post: 27-Jul-2020
Category:
Upload: others
View: 37 times
Download: 0 times
Share this document with a friend
56
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018 @SQLMaria
Transcript
Page 1: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Oracle Database Features Every Developer Should Know About

Maria ColganOracle Database Product ManagementFebruary, 2018

@SQLMaria

Page 2: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor StatementThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

2

Page 3: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Program Agenda

Invisible Columns

Virtual Columns

External Tables

Useful Supplied PL/SQL Packages

How to Find Your Needle in the Haystack

1

2

3

4

3

5

Page 4: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible Columns

4

Page 5: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible columns

Difficulties with the relational model today:

• Different applications may need different columns from a single table but all applications are impacted by any DDL change on a table– Dropping a column will break queries still referring to it

– Adding a column breaks “SELECT *” queries

– Dropping a column will delete the data from all the rows, even those that are already archived

5

Flexible schemas in a relational model

12c

Page 6: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible columns

6

Dropping columns is disruptiveSQL> SELECT first_name, last_name, initials

FROM customers;

FIRST_NAME LAST_NAME IN---------- ---------- --George Kolaski GKJoseph Smith JSGerald Teller GT

SQL> SELECT first_name, last_name, initialsFROM customers;

FIRST_NAME LAST_NAME IN---------- ---------- --George Kolaski GKJoseph Smith JSGerald Teller GT

App A

App B

FIRST_NAME LAST_NAMEINITIALS

Page 7: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible columns

7

Dropping columns is disruptive

SQL> SELECT first_name, last_name, initialsFROM customers;

SELECT first_name, last_name, initials*

ERROR at line 1:ORA-00904: "INITIALS": invalid identifier

App A

App B

LAST_NAMEFIRST_NAME

ALTER TABLE customers DROP COLUMN initials;

SQL> SELECT first_name, last_nameFROM customers;

FIRST_NAME LAST_NAME --------- ----------George KolaskiJoseph Smith Gerald Teller

Page 8: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible columns

8

Dropping columns is disruptiveSQL> SELECT first_name, last_name

FROM customers;

FIRST_NAME LAST_NAME --------- ----------Geroge KolaskiJoseph Smith Gerald Teller

App A

App B

FIRST_NAME LAST_NAMEINITIALS

ALTER TABLE customers MODIFY initials INVISIBLE;

SQL> SELECT first_name, last_nameFROM customers;

FIRST_NAME LAST_NAME ---------- ----------George KolaskiJoseph Smith Gerald Teller

SQL> SELECT first_name, last_name, initialsFROM customers;

FIRST_NAME LAST_NAME IN---------- ---------- --George Kolaski GKJoseph Smith JSGerald Teller GT

Page 9: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible columns

9

Adding columns is disruptive

App A

App C

LAST_NAMEFIRST_NAME

SQL> SELECT first_name, last_nameFROM customers;

FIRST_NAME LAST_NAME ---------- ----------George KolaskiJoseph Smith Gerald Teller

SQL> SELECT *FROM customers;

FIRST_NAME LAST_NAME ---------- ----------George KolaskiJoseph Smith Gerald Teller

Page 10: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

SQL> SELECT *FROM customers;

FIRST_NAME LAST_NAME DOB---------- ---------- ---------George Kolaski 12-SEP-88Joseph Smith 24-MAR-93Gerald Teller 02-NOV-69

Invisible columns

10

Adding columns is disruptiveSQL> SELECT first_name, last_name, dob

FROM customers;

FIRST_NAME LAST_NAME DOB---------- ---------- ---------George Kolaski 12-SEP-88Joseph Smith 24-MAR-93Gerald Teller 02-NOV-69

App A

App C

LAST_NAMEFIRST_NAME DOB

ALTER TABLE customers ADD dob date;

Page 11: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

SQL> SELECT *FROM customers;

FIRST_NAME LAST_NAME ---------- ----------George KolaskiJoseph SmithGerald Teller

Invisible columns

11

Adding columns is disruptive

App A

App C

LAST_NAMEFIRST_NAME DOB

ALTER TABLE customers ADD dob date INVISIBLE;

SQL> SELECT first_name, last_name, dobFROM customers;

FIRST_NAME LAST_NAME DOB---------- ---------- ---------George Kolaski 12-SEP-88Joseph Smith 24-MAR-93Gerald Teller 02-NOV-69

Page 12: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Invisible columns

• Invisible columns can be used as partition keys• Be aware that column order changes when setting columns invisible• Use SET COLINVISIBLE ON; to see invisible columns in DESC

12

Flexible schemas in a relational model

SQL> SET COLINVISIBLE ON; SQL> DESC CUSTOMERS; Name Null? Type ----------------- -------- ------------ FIRST_NAME VARCHAR2(10) LAST_NAME VARCHAR2(10) DOB (INVISIBLE) DATE

LAST_NAMEFIRST_NAME DOB

Page 13: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual Columns

13

Page 14: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual Columns

• Example: Compute total sales price• Net = Price + (Price * Tax)• As expressions get more complex it’s

possible developers will accidentally define them differently in their code• This results in inconsistent reporting• You could change the data model

every time the business determines a new measure & create a trigger to populate the data

14

Analytic queries often contain complex expressions

PRODUCT TAXPRICE

Pric

e +

Pric

e *

Tax

11g

Page 15: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual Columns

15

Add column with insert trigger

PRODUCT TAXPRICE

Price

+ P

rice

* Ta

x

NET

SQL> ALTER TABLE sales ADD net number INVISIBLE;

Table Altered.

SQL> UPDATE sales set net = price+(price*tax);

15 rows updated.

SQL> CREATE OR REPLACE TRIGGER set_netBEFORE INSERT or UPDATE ON salesFOR EACH ROWBEGIN:new.net := :new.price +

(:new.price * :new.tax);END;/

Trigger created.

Page 16: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual Columns

16

Add column with insert trigger

PRODUCT TAXPRICE

Price

+ P

rice

* Ta

x

NET

SQL> INSERT INTO sales (product, price, tax)VALUES('Toy Train','5',’0.08');

1 row created.

SQL> COMMIT;

Commit complete.

SQL> SELECT product, price, tax, netFROM sales WHERE …;

PRODUCT PRICE TAX NET---------- ---------- ---------- ----------Toy Train 5 0.08 5.4

Page 17: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual columns

17

Appear to be normal table columns

PRODUCT TAXPRICE NET

SQL> ALTER TABLE sales ADD net number INVISIBLE GENERATED ALWAYS AS (price+(price*tax))VIRTUAL;

Table Altered

SQL> INSERT INTO sales (product, price, tax)VALUES('Toy Train','5',’0.08');

1 row created.

SQL> COMMIT;Commit complete.

SQL> SELECT prod, price, tax, netFROM sales WHERE …;

PRODUCT PRICE TAX NET---------- ---------- ---------- ----------Toy Train 5 0.08 5.4

Price

+ P

rice

* Ta

x

Page 18: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual columns

18

Can be used in where clause

PRODUCT TAXPRICE NET

SQL> SELECT product, price, tax, netFROM salesWHERE net > 5;

PRODUCT PRICE TAX NET---------- ---------- ---------- ----------Toy Train 5 0.08 5.4

SQL> SELECT * FROMTABLE(DBMS_XPLAN.DISPLAY_CURSOR());

PLAN_TABLE_OUTPUT-----------------------------------------------------------------------------Plan hash value: 781590677-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost |-----------------------------------------------------------------------------| 0 |SELECT STATEMENT | | 1788 | 228K| 20 | | 1|TABLE ACCESS FULL | SALES | 1788 | 228K| 20 | ------------------------------------------------------------------------------

Pric

e +

Pric

e *

Tax

Page 19: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Virtual columns

19

Can be indexed

PRODUCT TAXPRICE NET

SQL> SELECT prod, price, tax, netFROM salesWHERE net > 5;

PROD PRICE TAX NET---------- ---------- ---------- ----------Toy Train 5 0.08 5.4

SQL> SELECT * FROMTABLE(DBMS_XPLAN.DISPLAY_CURSOR());

PLAN_TABLE_OUTPUT-----------------------------------------------------------------------------Plan hash value: 781590677------------------------------------------------------------------------------| Id | Operation | Name | Rows | -------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1788 | | 1 | TABLE ACCESS BY INDEX ROWID | SALES | 1788 || 2| INDEX RANGE SCAN | IDX_NET | 1788 | ------------------------------------------------------------------------------

CREATE INDEX idx_netON sales(net);

Pric

e +

Pric

e *

Tax

Page 20: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External Tables

20

SALES TABLE (external)

UK Partition Germany Partition USA Partition

SQL

Page 21: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

• Query flat files as if they were tables in the database

– Provides the full power of SQL over flat files

• Preprocessor capabilities

– Un-compress, pre-format, etc. data before being read by the database

• Enables easy data loading

– Use INSERT INTO … SELECT (incl. DML error logging)

• Query data pump files

– Run queries on last nights backup to confirm it validity

• Query data store on HDFS/Hive

21

The universal out-of-database query interface

9i

Page 22: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

22

Query files as if they were tables

CREATE TABLE customers_ext(first_name varchar2 (10),int varchar2 (2),last_name varchar2 (10),dob date

)ORGANIZATION EXTERNAL (TYPE ORACLE_LOADERDEFAULT DIRECTORY HOME_DIRACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINESKIP 1FIELDS TERMINATED BY ',' (first_name char(10),last_name char(10),int char(2),DOB char (10)DATE_FORMAT DATE 'YYYY-MM-DD')

)LOCATION ('customers*.csv'))

REJECT LIMIT UNLIMITED;

CREATE DIRECTORY home_dir AS‘/home/maria/’;

/home/maria/customers_1.csv

FName,LName,Initials,DOBGeorge,Kolaski,GK,1988-09-12Joseph,Smith,JS,1993-03-24Gerald,Teller,GT,19691102

::

Page 23: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

23

Query files as if they were tablesSQL> SELECT first_name, last_name, dob

FROM customers_ext;

FIRST_NAME LAST_NAME DOB---------- ---------- ---------Geroge Kolaski 12-SEP-88Joseph Smith 24-MAR-93Gerald Teller 02-NOV-69

SQL> SELECT first_name, last_nameFROM customers_extWHERE dob > TO_DATE('1990','YYYY');

FIRST_NAME LAST_NAME ---------- ---------Joseph Smith

FName,LName,Initials,DOBGeorge,Kolaski,GK,1988-09-12Joseph,Smith,JS,1993-03-24Gerald,Teller,GT,19691102

::

/home/maria/customers_1.csv

Page 24: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

24

Un-compress files on the fly

CREATE TABLE customers_ext_comp(...

)ORGANIZATION EXTERNAL (TYPE ORACLE_LOADERDEFAULT DIRECTORY HOME_DIRACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINEPREPROCESSOR USR_BIN:zcatSKIP 1FIELDS TERMINATED BY ',' (...

))LOCATION ('customers*.csv.gz')

)REJECT LIMIT UNLIMITED;/home/maria/customers_1.csv.gz

�Ycustomers_1.csv�A �0@�}�2�$�b�R�bz�����є$����< �A�Ѧ��r~(/XH�2����8�~{���t�A�Ѧ��r~(/XH�2��

11g

Page 25: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

25

Un-compress files on the flySQL> SELECT int, last_name, dob

FROM customers_ext_comp;

INT LAST_NAME DOB--- ---------- ---------GK Kolaski 12-SEP-88JS Smith 24-MAR-93GT Teller 02-NOV-69

SQL> SELECT first_name, last_nameFROM customers_ext_compWHERE first_name COLLATE_BINARY_CILIKE 'GE%';

FIRST_NAME LAST_NAME---------- ---------George KolaskiGerald Teller

Page 26: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

26

Easy data loadingSQL> CREATE TABLE customers AS

SELECT * FROM customers_ext_comp;

Table created.

SQL> SELECT first_name, int, last_name, dobFROM customers;

FIRST_NAME INT LAST_NAME DOB---------- --- ---------- ---------Geroge GK Kolaski 12-SEP-88Joseph JS Smith 24-MAR-93Gerald GT Teller 02-NOV-69

CUSTOMERS

Page 27: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

27

Easy data exportSQL> CREATE TABLE customers_dumpORGANIZATION EXTERNAL(TYPE ORACLE_DATAPUMPDEFAULT DIRECTORY HOME_DIRLOCATION ('customers.dmp')

)AS SELECT * FROM customers;

Table created.

CUSTOMERS

11g

Page 28: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

28

Easily query your dump files

CREATE TABLE customers_dmp (first_name varchar2(10),int varchar2 (2),last_name varchar2 (10),dob date

)ORGANIZATION EXTERNAL(TYPE ORACLE_DATAPUMPDEFAULT DIRECTORY HOME_DIRLOCATION ('customers.dmp')

);

SQL> SELECT int, dobFROM customers_dmpWHERE last_name = 'Teller';

INT DOB--- ---------GT 02-NOV-69

Page 29: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

External tables

29

Easily query on HDFS/Hive

CREATE TABLE customers_ext(...

)ORGANIZATION EXTERNAL (TYPE ORACLE_LOADERDEFAULT DIRECTORY HDFS_DIRACCESS PARAMETERS (RECORDS DELIMITED BY 0X'0A'

PREPROCESSOR ”OSCH_BIN_PATH":'hdfs_stream'

SKIP 1FIELDS TERMINATED BY ',' (...

))LOCATION ('osch-20140515719-2786-1')

)REJECT LIMIT UNLIMITED;

CAT osch-20140515719-2786-1

<?xml version="1.0"?><configuration><property>

<name>oracle.hadoop.exttab.tableName</name><value>SH.SALES_EXT_DIR</value>

</property><property><name>oracle.hadoop.exttab.dataPaths</name><value>/data/s1/*.csv,/data/s2/*.csv</value>

</property>

Requires Oracle SQL Connector for Hadoop

12c

Page 30: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 30

• External Tables allow transparent access to data outside the DB• In-Memory For External Tables

builds in-memory column cache of data outside the DB for ultra-fast analytics on external data • All In-Memory Optimizations apply– Vector processing, JSON expressions

extend transparently to external data

• Up to 100X faster

In-memoryExternal

Tables

RDBMSIn-memory

DatabaseTables

External Data

DB TABLES

In-Memory For External TablesFast Analytics on External Data

Object Storage

Flat Files

Page 31: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Useful Supplied PL/SQL Packages

31

Page 32: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_SESSION.SLEEP()

32

Accessible sleep function within PL/SQL

SQL> set timing on;SQL> DECLARE

v_start date;v_end date;

BEGINv_start := SYSDATE;-- Sleep for 10 secondsdbms_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

Page 33: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_SESSION.SLEEP()

33

Accessible sleep function within PL/SQL

SQL> set timing on;SQL> DECLARE

v_start date;v_end date;

BEGINv_start := SYSDATE;-- Sleep for 10 secondsDBMS_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

Page 34: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_UTILITY.EXPAND_SQL_TEXT

• 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_TEXTprocedure expands references to views, turning them into subqueries in the original statement

34

SQL> SELECT * FROM sales_v;

Determining if this really is the right view to use

12c

Page 35: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_UTILITY.EXPAND_SQL_TEXTset serveroutput onDECLARE

l_clob CLOB;BEGIN

dbms_utility.Expand_sql_text(-input_sql_text => 'SELECT * FROM sales_v', -output_sql_text => l_clob);dbms_output.Put_line(l_clob);

END;/

35

Page 36: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_UTILITY.EXPAND_SQL_TEXTSELECT "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"

36

Page 37: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

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

37

Figuring out why production is different to test

SQL> BEGINDBMS_COMPARISON.CREATE_COMPARISON(comparison_name => 'COMP_SALES',schema_name => 'SH',object_name => 'SALES',dblink_name => 'orcl2_test');END;/

11g

Page 38: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

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

38

SQL> DECLAREscan_info DBMS_COMPARISON.COMPARISON_TYPE;BEGINIF 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

Page 39: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_COMPARISON

39

SELECT c.COLUMN_NAME, r.INDEX_VALUEcase 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_NAMEAND r.SCAN_ID = s.SCAN_IDAND s.SCAN_ID = 1ORDER BY r.INDEX_VALUE;

COLUMN_NAME INDEX_VALUE LOCAL REMOTE----------- ----------- ----- ------TAX_CODE 0.05 No Yes

Test environment has an different tax code

Page 40: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_ADVANCED_REWRITE

40

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 tGROUP BY x, y;

X Y SUM(Z)---------- ---------- ----------

1 4 1102 3 84

10g

Something changes and now you get

SQL> SELECT x, y, sum(z) FROM tGROUP BY x, y;

X Y SUM(Z)---------- ---------- ----------

2 3 841 4 110

Page 41: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_ADVANCED_REWRITE

41

Fixing the unfixable SQL statementStep 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 |--------------------------------------

Page 42: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_ADVANCED_REWRITE

42

Fixing the unfixable SQL statementStep 2• Check the order of the columns in the

index

SQL> SELECT column_position, column_name

FROM user_ind_columnsWHERE index_name='T_IDX'ORDER BY column_position;

COLUMN_POSITION COLUMN--------------- ------

1 Y2 X3 Z

• New index returns the rows in a different order because its sorted on column Y rather than X

Page 43: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_ADVANCED_REWRITE

43

Fixing the unfixable SQL statementStep 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 v2 AS3 SELECT x, y, sum(z) "SUM(Z)"4 FROM t5 GROUP BY x, y6 ORDER BY x, y;

View created.

Page 44: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_ADVANCED_REWRITE

44

Fixing the unfixable SQL statementStep 4• Use the DBMS_ADVANCED_REWRITE procedure to map the original query

to a simple SELECT * FROM v;

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

Page 45: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

DBMS_ADVANCED_REWRITE

45

Fixing the unfixable SQL statement• Each time our original SQL statement

is issued the DBMS_ADVANCED_REWRITEprocedure rewrites it to be SELECT * FROM v;• Always get the query output in an order list

SQL> SELECT x, y, sum(z) FROM tGROUP BY x, y;

X Y SUM(Z)---------- ---------- ----------

1 4 1102 3 84

-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | VIEW | V || 2 | SORT GROUP BY | || 3 | INDEX FULL SCAN|T_IDX |-----------------------------------

Page 46: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 46

How to Find your Needle in the Haystack

Page 47: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

• Databases are high concurrency, multi-user data management systems• A performance issue in your application requires to filter out the noise

from everybody else, but how to do that?• If only there was a way to clearly identify the different modules and

actions from your application…

47

How to filter the noise from YOUR workload

Page 48: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

DBMS_APPLICATION_INFO• SET_CLIENT_INFO()– Supply additional information about the client application

• SET_MODULE()– Set the name of the current application or application module

• SET_ACTION()– Set the name of the current action within the module

• SET_SESSION_LONGOPS()– Record the on-going progress of a long running operation in V$SESSION_LONGOPS

48

How to filter the noise from YOUR workload

8i

Page 49: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

49

Native APIs - Python

conn = cx_Oracle.connect(db_user,db_password,cx_Oracle.makedsn(db_host, db_port, service_name=db_name))

conn.client_identifier = "Twitter Streamer”conn.module = "DBLoader”conn.action = “persist”cursor = conn.cursor()cursor.prepare("INSERT INTO TWITTER (tweet) VALUES (:tweet)")

Page 50: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

50

Native APIs – Node.js

oracledb.getConnection({user : db_user,password : db_password,connectString : db_url

},function(err, connection) {if (err) { console.error(err.message); return; }

connection.clientId = ”Twitter Streamer";connection.module = ”DBLoader";connection.action = ”persist";

connection.execute(”INSERT INTO TWITTER (tweet) VALUES (:tweet)",function(err, result) {. . .

Page 51: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

51

Native APIs – Java

Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);conn.setClientInfo("OCSID.CLIENTID", ”Twitter Streamer");conn.setClientInfo("OCSID.MODULE", ”DBLoader");conn.setClientInfo("OCSID.ACTION", ”persist");Statement stmt = conn.createStatement();stmt.execute("INSERT INTO TWITTER (tweet) VALUES (?)");

Page 52: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

52

AWR report

Page 53: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

53

Active Monitor

Page 54: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Finding YOUR needle in the haystack

54

Active Monitor – SQL details

Page 55: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

If you have more questions later, feel free to ask

Page 56: Oracle Database Features Every Developer Should Know About...Oracle Database Features Every Developer Should Know About Maria Colgan Oracle Database Product Management February, 2018

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Join the Conversation

56

Related Videos•Maria’s YouTube Channel•Oracle Database PM Channel

Any Additional Questions •My emails: [email protected]

Additional Resources

https://twitter.com/SQLMaria

https://sqlmaria.comhttps://www.facebook.com/SQLMaria


Recommended