1 copyright © 2011, oracle and/or its affiliates. all...

96
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8

Upload: others

Post on 18-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Page 2: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

2 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Five things you probably

didn’t know about SQL

Thomas Kyte

http://asktom.oracle.com/

Page 3: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

5

Who am I

• Been with Oracle since 1993

• User of Oracle since 1987

• The “Tom” behind AskTom in Oracle Magazinewww.oracle.com/oramag

• Expert Oracle Database Architecture

• Effective Oracle by Design

• Expert One on One Oracle

• Beginning Oracle

Page 4: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

6

<Insert Picture Here>

Five things you probably didn’t know about

SQL

• SQLNet Compression

• NULLS and Indexes and Cardinality

• You are being watched!

• Scalar Subquery Caching

• Calling statement level non-deterministic functions

Page 5: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

7

SQLNet

Compression

Page 6: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

8

SQLNet Compression

• How you retrieve the data matters

• Not all result sets are the same – even if they have the same data

Page 7: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

9

SQLNet Compression

ops$tkyte%ORA11GR2> create table t2 as3 select *4 from all_objects;

Table created.

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats( user, 'T' );3 end;4 /

PL/SQL procedure successfully completed.

Page 8: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

10

SQLNet Compression

ops$tkyte%ORA11GR2> set arraysize 15

ops$tkyte%ORA11GR2> set autotrace traceonly statistics

Page 9: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

11

SQLNet Compression

ops$tkyte%ORA11GR2> select * from t;72228 rows selected.

Statistics--------------------------------------------------------

5794 consistent gets8015033 bytes sent via SQL*Net to client53385 bytes received via SQL*Net from client4817 SQL*Net roundtrips to/from client72228 rows processed

Page 10: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

12

SQLNet Compression

ops$tkyte%ORA11GR2> select * from t order by timestamp;72228 rows selected.

Statistics--------------------------------------------------------

1031 consistent gets3427630 bytes sent via SQL*Net to client53385 bytes received via SQL*Net from client4817 SQL*Net roundtrips to/from client72228 rows processed

Page 11: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

13

SQLNet Compression

ops$tkyte%ORA11GR2> select * from t order by timestamp, object_type, owner;72228 rows selected.

Statistics----------------------------------------------------------

1031 consistent gets3280011 bytes sent via SQL*Net to client53385 bytes received via SQL*Net from client4817 SQL*Net roundtrips to/from client72228 rows processed

Page 12: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

14

SQLNet Compression

ops$tkyte%ORA11GR2> set arraysize 100ops$tkyte%ORA11GR2> set autotrace traceonly statistics

Page 13: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

15

SQLNet Compression

ops$tkyte%ORA11GR2> select * from t;72228 rows selected.

Statistics--------------------------------------------------------

1842 consistent gets7482943 bytes sent via SQL*Net to client

8362 bytes received via SQL*Net from client724 SQL*Net roundtrips to/from client

72228 rows processed

Page 14: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

16

SQLNet Compression

ops$tkyte%ORA11GR2> select * from t order by timestamp;72228 rows selected.

Statistics-------------------------------------------------------

1031 consistent gets2907819 bytes sent via SQL*Net to client

8362 bytes received via SQL*Net from client724 SQL*Net roundtrips to/from client

72228 rows processed

Page 15: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

17

SQLNet Compression

ops$tkyte%ORA11GR2> select * from t order by timestamp, object_type, owner;72228 rows selected.

Statistics----------------------------------------------------------

1031 consistent gets2760200 bytes sent via SQL*Net to client

8362 bytes received via SQL*Net from client724 SQL*Net roundtrips to/from client

72228 rows processed

Page 16: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

18

SQLNet Compression

No Order

15

Some Order

15

Very

Ordered

15

No Order

100

Some

Order

100

Very

Ordered

100

Bytes Sent8.01 m 3.42 m 3.28 m 7.48 m 2.90 m 2.76 m

% of original100% 43% 41% 93% 36% 34%

Consistent Gets5832 1033 1033 1741 1033 1033

ops$tkyte%ORA11GR2> select round(1033*8/1024,2) from dual;

ROUND(1033*8/1024,2)--------------------

8.07

Page 17: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

19

SQLNet Compression

No Order

1000

Some Order

1000

Very

Ordered

1000

No Order

100

Some

Order

100

Very

Ordered

100

Bytes Sent7.39 m 2.82 m 2.67 m 7.48 m 2.90 m 2.76 m

% of original92% 35% 33% 93% 36% 34%

Consistent Gets1105 1033 1033 1741 1033 1033

ops$tkyte%ORA11GR2> select round(1033*8/1024,2) from dual;

ROUND(1033*8/1024,2)--------------------

8.07

Page 18: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

20

NULLS and

Indexes and

Cardinality

Page 19: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

21

“Wrong cardinality = Wrong Plan”

Page 20: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

22

NULLs and Cardinality

ops$tkyte%ORA11GR2> create table t2 pctfree 203 as4 select a.*,5 case when mod(rownum,100) <= 506 then last_ddl_time7 end end_date8 from all_objects a;

Table created.

Page 21: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

23

NULLs and Cardinality

ops$tkyte%ORA11GR2> create index t_idx2 on t(end_date);

Index created.

Page 22: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

24

NULLs and Cardinality

ops$tkyte%ORA11GR2> select count(*)2 from t3 where end_date4 between to_date('01-sep-2010', 'dd-mon-yyyy')5 and to_date('30-sep-2010', 'dd-mon-yyyy');

COUNT(*)----------

36267

Page 23: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

25

NULLs and Cardinality

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats(user, 'T');3 end;4 /

PL/SQL procedure successfully completed.

Page 24: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

26

NULLs and Cardinality

ops$tkyte%ORA11GR2> select count(*),2 count(distinct end_date),3 count(end_date),4 min(end_date),5 max(end_date)6 from t;

CNT CNTD CNT2 MIN MAX---------- ---------- ---------- --------- ---------

72228 703 36850 01-OCT-02 30-SEP-11

Page 25: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

27

NULLs and Cardinality

ops$tkyte%ORA11GR2> set autotrace traceonly explainops$tkyte%ORA11GR2> select *2 from t3 where end_date4 between to_date( '01-sep-2010', 'dd-mon-yyyy' )5 and to_date( '30-sep-2010', 'dd-mon-yyyy' );

Execution Plan----------------------------------------------------------Plan hash value: 1601196873

Page 26: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

28

NULLs and Cardinality

--------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 36024 | 3588K| 339 (1)| 00:00:05 ||* 1 | TABLE ACCESS FULL| T | 36024 | 3588K| 339 (1)| 00:00:05 |--------------------------------------------------------------------------

Predicate Information (identified by operation id):---------------------------------------------------

1 - filter("END_DATE"<=TO_DATE(' 2010-09-30 00:00:00', 'syyyy-mm-ddhh24:mi:ss') AND "END_DATE">=TO_DATE(' 2010-09-01 00:00:00','syyyy-mm-dd hh24:mi:ss'))

Page 27: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

29

NULLs and Cardinality

ops$tkyte%ORA11GR2> update t2 set end_date = 3 to_date( '01-jan-9999','dd-mon-yyyy' )4 where end_date is null;

35378 rows updated.

ops$tkyte%ORA11GR2> commit;

Commit complete.

Page 28: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

30

NULLs and Cardinality

ops$tkyte%ORA11GR2> begin 2 dbms_stats.gather_table_stats(user, 'T');3 end;4 /

PL/SQL procedure successfully completed.

Page 29: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

31

NULLs and Cardinality

ops$tkyte%ORA11GR2> select *2 from t3 where end_date4 between to_date('01-sep-2010', 'dd-mon-yyyy')5 and to_date('30-sep-2010', 'dd-mon-yyyy');

Execution Plan-------------------------------------------------Plan hash value: 470836197

Page 30: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

32

NULLs and Cardinality

---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| ---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 175 | 18375 | 10 (0)| | 1 | TABLE ACCESS BY INDEX ROWID| T | 175 | 18375 | 10 (0)| |* 2 | INDEX RANGE SCAN | T_IDX | 175 | | 2 (0)| ---------------------------------------------------------------------------

Predicate Information (identified by operation id):---------------------------------------------------

1 - filter("END_DATE"<=TO_DATE(' 2010-09-30 00:00:00', 'syyyy-mm-ddhh24:mi:ss') AND "END_DATE">=TO_DATE(' 2010-09-01 00:00:00','syyyy-mm-dd hh24:mi:ss'))

Page 31: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

33

“Wrong cardinality = Wrong Plan”

Page 32: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

34

<Insert Picture Here>

Nulls and Indexes

• There is a pervasive myth that indexes and NULLs are like matter and anti-matter

• There is the thought that “where column is null” cannot use an index

• There is a thought that NULLs are not indexed

• None of that is trueE

Page 33: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

35

NULLs and Indexes

ops$tkyte%ORA11GR2> create table t2 as3 select a.*,4 case when mod(rownum,100) > 15 then object_type6 end otype7 from all_objects a;

Table created.

Page 34: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

36

NULLs and Indexes

ops$tkyte%ORA11GR2> select count(*) from t where otype is null;

COUNT(*)----------

1445

Page 35: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

37

NULLs and Indexes

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats( user, 'T' );3 end;4 /

PL/SQL procedure successfully completed.

Page 36: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

38

NULLs and Indexes

ops$tkyte%ORA11GR2> create index t_idxon t(otype,owner);

Index created.

Page 37: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

39

NULLs and Indexes

ops$tkyte%ORA11GR2> set autotrace traceonly explainops$tkyte%ORA11GR2> select * from t where otype is null;

Execution Plan--------------------------------------------------------Plan hash value: 470836197

Page 38: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

40

NULLs and Indexes

-------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1445 | 149K| 96 (0)| 00:00:02 || 1 | TABLE ACCESS BY INDEX ROWID| T | 1445 | 149K| 96 (0)| 00:00:02 ||* 2 | INDEX RANGE SCAN | T_IDX | 1445 | | 7 (0)| 00:00:01 |-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):---------------------------------------------------

2 - access("OTYPE" IS NULL)

Page 39: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

41

NULLs and Indexes

ops$tkyte%ORA11GR2> drop index t_idx;

Index dropped.

ops$tkyte%ORA11GR2> create index t_idxon t(otype,0);

Index created.

Page 40: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

42

NULLs and Indexes

-------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1445 | 149K| 96 (0)| 00:00:02 || 1 | TABLE ACCESS BY INDEX ROWID| T | 1445 | 149K| 96 (0)| 00:00:02 ||* 2 | INDEX RANGE SCAN | T_IDX | 1445 | | 7 (0)| 00:00:01 |-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):---------------------------------------------------

2 - access("OTYPE" IS NULL)

Page 41: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

43

<Insert Picture Here>

Nulls and Indexes

• What is true is that entirely NULL key entries are not made in B*Tree indexes

• Therefore, an index on just OTYPE cannot be used to find NULLs

• But – what about B*Tree cluster indexes and Bitmap indexes?

Page 42: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

44

You are being

WATCHED!

Page 43: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

45

<Insert Picture Here>

You are being WATCHED!

• 9i and before – V$ tables

• 10g – ASH and AWR are obvious

• But there is more

– We watch what you ask for and change how statistics are gathered based on that.

Page 44: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

46

You are being WATCHED!

ops$tkyte%ORA11GR2> create table t2 as3 select a.*,4 case when rownum < 5005 then 16 else 997 end some_status8 from all_objects a9 /

Table created.

Page 45: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

47

You are being WATCHED!

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats(user,'T');3 end;4 /

PL/SQL procedure successfully completed.

Page 46: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

48

You are being WATCHED!

ops$tkyte%ORA11GR2> select histogram2 from user_tab_cols3 where table_name = 'T'4 and column_name = 'SOME_STATUS';

HISTOGRAM---------------NONE

Page 47: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

49

You are being WATCHED!

ops$tkyte%ORA11GR2> create index t_idxon t(some_status);

Index created.

Page 48: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

50

You are being WATCHED!

ops$tkyte%ORA11GR2> set autotrace traceonly explainops$tkyte%ORA11GR2> select * from t where some_status = 1;

Execution Plan----------------------------------------------------------Plan hash value: 1601196873

--------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 36115 | 3526K| 300 (1)| 00:00:04 ||* 1 | TABLE ACCESS FULL| T | 36115 | 3526K| 300 (1)| 00:00:04 |--------------------------------------------------------------------------

Page 49: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

51

You are being WATCHED!

ops$tkyte%ORA11GR2> select * from t where some_status = 99;

Execution Plan----------------------------------------------------------Plan hash value: 1601196873

--------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 36115 | 3526K| 300 (1)| 00:00:04 ||* 1 | TABLE ACCESS FULL| T | 36115 | 3526K| 300 (1)| 00:00:04 |--------------------------------------------------------------------------

Page 50: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

52

You are being WATCHED!

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats( user, 'T' );3 end;4 /

PL/SQL procedure successfully completed.

Page 51: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

53

You are being WATCHED!

ops$tkyte%ORA11GR2> select histogram2 from user_tab_cols3 where table_name = 'T'4 and column_name = 'SOME_STATUS';

HISTOGRAM---------------FREQUENCY

Page 52: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

54

You are being WATCHED!

ops$tkyte%ORA11GR2> select * from t where some_status = 1;

Execution Plan----------------------------------------------------------Plan hash value: 470836197

--------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time --------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 539 | 53900 | 10 (0)| 00:00| 1 | TABLE ACCESS BY INDEX ROWID| T | 539 | 53900 | 10 (0)| 00:00|* 2 | INDEX RANGE SCAN | T_IDX | 539 | | 2 (0)| 00:00--------------------------------------------------------------------------------

Page 53: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

55

You are being WATCHED!

ops$tkyte%ORA11GR2> select * from t where some_status = 99;

Execution Plan----------------------------------------------------------Plan hash value: 1601196873

--------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 71683 | 7000K| 300 (1)| 00:00:04 ||* 1 | TABLE ACCESS FULL| T | 71683 | 7000K| 300 (1)| 00:00:04 |--------------------------------------------------------------------------

Page 54: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

56

You are being WATCHED!

ops$tkyte%ORA11GR2> select *2 from3 (4 select *5 from sys.col_usage$6 where obj# = (select object_id7 from dba_objects8 where object_name = 'T'9 and owner = 'OPS$TKYTE' )10 )11 unpivot (value for x in12 ( EQUALITY_PREDS, EQUIJOIN_PREDS, NONEQUIJOIN_PREDS,13 RANGE_PREDS, LIKE_PREDS, NULL_PREDS ) )14 /

Page 55: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

57

You are being WATCHED!

OBJ# INTCOL# TIMESTAMP X VALUE---------- ---------- --------- ----------------- ----------

98040 16 30-SEP-11 EQUALITY_PREDS 198040 16 30-SEP-11 EQUIJOIN_PREDS 098040 16 30-SEP-11 NONEQUIJOIN_PREDS 098040 16 30-SEP-11 RANGE_PREDS 098040 16 30-SEP-11 LIKE_PREDS 098040 16 30-SEP-11 NULL_PREDS 0

6 rows selected.

Page 56: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

58

You are being WATCHED!

ops$tkyte%ORA11GR2> select * from t where some_status > 100;

no rows selected

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats( user, 'T' );3 end;4 /

PL/SQL procedure successfully completed.

Page 57: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

59

You are being WATCHED!

OBJ# INTCOL# TIMESTAMP X VALUE---------- ---------- --------- ----------------- ----------

98040 16 30-SEP-11 EQUALITY_PREDS 298040 16 30-SEP-11 EQUIJOIN_PREDS 098040 16 30-SEP-11 NONEQUIJOIN_PREDS 098040 16 30-SEP-11 RANGE_PREDS 198040 16 30-SEP-11 LIKE_PREDS 098040 16 30-SEP-11 NULL_PREDS 0

6 rows selected.

Page 58: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

60

<Insert Picture Here>

You are being WATCHED!

• You can ‘seed’ column stats pre-emptively

• Adds more “watching”

• Suggests possible extended statistics as well

Page 59: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

61

You are being WATCHED!

ops$tkyte%ORA11GR2> begin2 dbms_stats.seed_col_usage( null, null, 10 );3 end;4 /

PL/SQL procedure successfully completed.

Page 60: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

62

You are being WATCHED!

ops$tkyte%ORA11GR2> select *2 from t3 where owner = 'SYS'4 and object_type = 'DIMENSION';

no rows selected

Page 61: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

63

You are being WATCHED!

ops$tkyte%ORA11GR2> select dbms_stats.report_col_usage( user, 'T' )2 from dual;

DBMS_STATS.REPORT_COL_USAGE(USER,'T')--------------------------------------------------------------------------------LEGEND:.......

EQ : Used in single table EQuality predicateRANGE : Used in single table RANGE predicateLIKE : Used in single table LIKE predicateNULL : Used in single table is (not) NULL predicateEQ_JOIN : Used in EQuality JOIN predicateNONEQ_JOIN : Used in NON EQuality JOIN predicateFILTER : Used in single table FILTER predicateJOIN : Used in JOIN predicateGROUP_BY : Used in GROUP BY expression

Page 62: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

64

You are being WATCHED!

ops$tkyte%ORA11GR2> select dbms_stats.report_col_usage( user, 'T' )2 from dual;

COLUMN USAGE REPORT FOR OPS$TKYTE.T...................................

1. OBJECT_TYPE : EQ2. OWNER : EQ3. SOME_STATUS : EQ RANGE4. (OWNER, OBJECT_TYPE) : FILTER###############################################################################

Page 63: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

65

Scalar Subquery

Caching

Page 64: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

66

<Insert Picture Here>

Scalar Subquery Caching

• A scalar subquery is a query that returns zero or one rows and a single column

• Can be used anywhere an expression can be used

• Is executed conceptually once for each row it is processed against

• For example:

Page 65: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

67

Scalar Subquery Caching

Select dname, (select count(*)from emp

where emp.deptno = dept.deptno)from dept;

Is a lot like�.

Page 66: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

68

Scalar Subquery Caching

Beginfor x in (select dname, deptno from dept)loop

select count(*) into cntfrom emp

where deptno = X.DEPTNO;

dbms_output.put_line( x.dname || ‘ ‘ || x.cnt );

end loop;End;

Page 67: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

69

<Insert Picture Here>

Scalar Subquery Caching

• Conceptually it is like thatE

• In reality there is caching going on

• Up to 255 entries can be saved

• Only for the duration of the query! Not across queries

Page 68: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

70

Scalar Subquery Caching

ops$tkyte%ORA11GR2> create table t2 as3 select *4 from all_objects;

Table created.

Page 69: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

71

Scalar Subquery Caching

ops$tkyte%ORA11GR2> begin2 dbms_stats.gather_table_stats( user,'T' );3 end;4 /

PL/SQL procedure successfully completed.

Page 70: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

72

Scalar Subquery Caching

ops$tkyte%ORA11GR2> create or replace function f( x in varchar2 )

2 return number3 as4 begin5 dbms_application_info.set_client_info6 ( to_number(userenv('client_info'))+1 );7 8 return length(x);9 end;

10 /Function created.

Page 71: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

73

Scalar Subquery Caching

ops$tkyte%ORA11GR2> variable startcpu number;ops$tkyte%ORA11GR2> begin

2 dbms_application_info.set_client_info(0);3 :startcpu := dbms_utility.get_cpu_time;4 end;5 /

PL/SQL procedure successfully completed.

This is run before every subsequent query…

Page 72: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

74

Scalar Subquery Caching

ops$tkyte%ORA11GR2> select owner, f(owner) from t;

72233 rows selected.

ops$tkyte%ORA11GR2> select userenv('client_info') ci,2 dbms_utility.get_cpu_time-:startcpu cpu3 from dual;

CI CPU---------- ----------72233 115

Page 73: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

75

Scalar Subquery Caching

ops$tkyte%ORA11GR2> select owner, (select f(owner) from dual) from t;

72233 rows selected.

ops$tkyte%ORA11GR2> select userenv('client_info') ci,2 dbms_utility.get_cpu_time-:startcpu cpu3 from dual;

CI CPU---------- ----------69 25

Page 74: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

76

Scalar Subquery Caching

ops$tkyte%ORA11GR2> create or replace function f( x in varchar2 )

2 return number3 DETERMINISTIC4 as

…10 end;11 /

Function created.

Page 75: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

77

Scalar Subquery Caching

ops$tkyte%ORA11GR2> select owner, f(owner) from t;

72233 rows selected.

ops$tkyte%ORA11GR2> select userenv('client_info') ci,2 dbms_utility.get_cpu_time-:startcpu cpu3 from dual;

CI CPU---------- ----------8278 74

Page 76: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

78

Scalar Subquery Caching

ops$tkyte%ORA11GR2> create or replace function f( x in varchar2 )

2 return number3 RESULT_CACHE4 as5 begin

…10 end;11 /

Function created.

Page 77: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

79

Scalar Subquery Caching

ops$tkyte%ORA11GR2> select owner, f(owner) from t;

72233 rows selected.

ops$tkyte%ORA11GR2> select userenv('client_info') ci,2 dbms_utility.get_cpu_time-:startcpu cpu3 from dual;

CI CPU---------- ----------35 60

Page 78: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

80

Scalar Subquery Caching

ops$tkyte%ORA11GR2> select owner, f(owner) from t;

72233 rows selected.

ops$tkyte%ORA11GR2> select userenv('client_info') ci,2 dbms_utility.get_cpu_time-:startcpu cpu3 from dual;

CI CPU---------- ----------0 62

Page 79: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

81

Scalar Subquery Caching

ops$tkyte%ORA11GR2> select owner, (select f(owner) from dual) from t;

72233 rows selected.

ops$tkyte%ORA11GR2> select userenv('client_info') ci,2 dbms_utility.get_cpu_time-:startcpu cpu3 from dual;

CI CPU---------- ----------0 22

Page 80: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

82

Scalar Subquery Caching

Select * from T where owner = g(‘scott’);

How many times will g(‘scott’) be invoked?

It depends of courseE

Page 81: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

83

Scalar Subquery Caching

Select * from T where owner = (select g(‘scott’) from dual);

Now How many times will g(‘scott’) be invoked?

It won’t depend this timeE

Page 82: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

84

Statement Level

non-Deterministic

Functions

Page 83: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

85

<Insert Picture Here>

Statement level non-deterministic functions

• What is a deterministic function?

• What is a statement level deterministic function?

• Why do we care?

Page 84: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

86 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> create table t2 as3 select *4 from all_users5 where rownum <= 5;

Table created.

Page 85: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

87 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> create or replace function f2 return number3 as4 pragma autonomous_transaction;5 l_cnt number;6 begin7 select count(*) into l_cnt from t;8 9 insert into t (username, user_id, created )10 values ( 'hello', 123, sysdate );11 commit;12 13 return l_cnt;14 end;15 /

Page 86: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

88 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> select count(*) over () cnt1,2 (select count(*) from t) cnt2,3 f() cnt3,4 (select f() from dual) cnt45 from t;

Page 87: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

89 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> select count(*) over () cnt1,2 (select count(*) from t) cnt2,3 f() cnt3,4 (select f() from dual) cnt45 from t;

CNT1 CNT2 CNT3 CNT4---------- ---------- ---------- ----------

5 5 5 65 5 7 65 5 8 65 5 9 65 5 10 6

Page 88: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

90 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> create or replace function f(p_scn in number)

2 return number3 as4 pragma autonomous_transaction;5 l_cnt number;6 begin7 select count(*) into l_cnt from t8 as of scn p_scn;9

10 insert into t (username, user_id, created )11 values ( 'hello', 123, sysdate );12 commit;13 14 return l_cnt;15 end;16 /

Page 89: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

91 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> variable scn numberops$tkyte%ORA11GR2> exec :scn :=

dbms_flashback.get_system_change_number

PL/SQL procedure successfully completed.

Page 90: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

92 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> select count(*) over () cnt1,2 (select count(*) from t) cnt2,3 f(:scn) cnt3,4 (select f(:scn) from dual) cnt45 from t;

CNT1 CNT2 CNT3 CNT4---------- ---------- ---------- ----------

5 5 5 55 5 5 55 5 5 55 5 5 55 5 5 5

Page 91: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

93 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> insert into t (username, user_id, created ) values ( 'x', 1, sysdate );

1 row created.

ops$tkyte%ORA11GR2> exec :scn := dbms_flashback.get_system_change_number

PL/SQL procedure successfully completed.

Page 92: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

94 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Statement level non-deterministic functions

ops$tkyte%ORA11GR2> select count(*) over () cnt1,2 (select count(*) from t) cnt2,3 f(:scn) cnt3,4 (select f(:scn) from dual) cnt45 from t;

CNT1 CNT2 CNT3 CNT4---------- ---------- ---------- ----------

6 6 5 56 6 5 56 6 5 56 6 5 56 6 5 56 6 5 5

6 rows selected.

Page 93: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

95

<Insert Picture Here>

Five things you probably didn’t know about

SQL

• SQLNet Compression

• NULLS and Indexes and Cardinality

• You are being watched!

• Scalar Subquery Caching

• Calling statement level non-deterministic functions

Page 94: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

96 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Q&A

Page 95: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

97 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8

Page 96: 1 Copyright © 2011, Oracle and/or its affiliates. All ...nyoug.org/Presentations/2011/December/Kyte_5_things_sql.pdf · 6  Five things you probably didn’t

98 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Insert Information Protection Policy Classification from Slide 8