Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQL TUNING 101

15 views

Published on

Session for Oracle Code One 2018

Published in: Technology
  • Be the first to comment

  • Be the first to like this

SQL TUNING 101

  1. 1. SQL TUNING 101 ALEX ZABALLA
  2. 2. ALEX ZABALLA CHRISTIAN, HUSBAND, FATHER, DBA, ACE DIRECTOR, WRITER AT OTN, SPEAKER AND BLOGGER. 276 – Including OPN http://alexzaballa.blogspot.com/ @alexzaballa https://www.linkedin.com/in/alexzaballa Copyright © 2018 Accenture. All rights reserved. 2
  3. 3. Copyright © 2018 Accenture. All rights reserved. 3 Worked for 3 years in Brazil as a Clipper/Delphi Developer (15 years old) 1997-1999 Worked for 7 years in Brazil as an Oracle Developer 2000 – 2007 Worked for 8 years in Angola as an Oracle DBA for the Ministry of Finance. 2007 - 2015
  4. 4. ACCENTURE ENKITEC GROUP CAPABILITIES OVERVIEW • Global systems integrator focused on the Oracle platform • Consultants average 15+ years of Oracle experience • Worldwide specialist in Engineered Systems implementations • 14 Oracle ACE members, specialist recognized by Oracle for their technical expertiseELITE Our consultants have been published in multiple subject areas and additional online resources that demonstrate Accenture’s experience and expertise with the OES platformTHOUGHT LEADERSHIP SUCCESS EXPERTISE ORACLE SPECIALIZATIONS* • Oracle Exadata • Oracle Exalogic ORACLE ENGINEERED SYSTEMS NUMBERS • 700+ Oracle Engineered Systems which AEG have configured, patched or supported. • 100+ AEG resources which have an average 15+ years of Oracle experience • AEG Support across 9 countries • 200 Oracle Engineered Systems (Exadata/Exalogic etc) currently under management directly by AEG • 200+ customers in either the AEG Managed Services program or remote DBA program • 50,000 Accenture Oracle IDC resources that can be leveraged for Level 1 & Level 2 support • Oracle Database • Oracle GoldenGate • Oracle Data Integrator • Oracle Database • Oracle Data Warehouse • Oracle Real Application Cluster • Oracle Performance Tuning • Oracle Database Security Copyright © 2018 Accenture. All rights reserved. 4
  5. 5. 3 MEMBERSHIP TIERS • Oracle ACE Director • Oracle ACE • Oracle ACE Associate bit.ly/OracleACEProgram Nominate yourself or someone you know: acenomination.oracle.com oracle-ace_ww@oracle.com Facebook.com/oracleaces @oracleace 500+ TECHNICAL EXPERTS HELPING PEERS GLOBALLY CONNECT: Copyright © 2018 Accenture. All rights reserved. 5
  6. 6. SQL TUNING 101 BEFORE START….
  7. 7. Initial beta with JavaScript Stored Procedures • http://www.oracle.com/technetwork/database/multiling ual-engine/overview/index.html ORACLE DATABASE MULTILINGUAL ENGINE – BETA 12.2 Copyright © 2018 Accenture. All rights reserved. 7
  8. 8. ORACLE DATABASE MULTILINGUAL ENGINE – BETA 12.2 Copyright © 2018 Accenture. All rights reserved. 8
  9. 9. ORACLE DATABASE MULTILINGUAL ENGINE – BETA 12.2 Copyright © 2018 Accenture. All rights reserved. 9
  10. 10. • http://www.oracle.com/technetwork/pt/articles/database- performance/odb-12-2-multilingual-4394909-ptb.html ORACLE DATABASE MULTILINGUAL ENGINE – BETA 12.2 Copyright © 2018 Accenture. All rights reserved. 10
  11. 11. https://livesql.oracle.com Copyright © 2018 Accenture. All rights reserved. 11
  12. 12. Copyright © 2018 Accenture. All rights reserved. 12
  13. 13. Copyright © 2018 Accenture. All rights reserved. 13
  14. 14. Copyright © 2018 Accenture. All rights reserved. 14
  15. 15. ORACLE DATABASE CLOUD CLOUD 101 Infrastructure as a Service (IaaS) Platform as a Service (PaaS) Software as a Service (SaaS) Database as a Service (Dbaas)
  16. 16. WHAT'S THE DIFFERENCE BETWEEN SQL TUNING ON-PREMISES AND IN THE ORACLE CLOUD?
  17. 17. NOTHING ZERO NIENTE NADA
  18. 18. TRUE IF YOU ARE NOT GOING TO: Copyright © 2018 Accenture. All rights reserved. 18
  19. 19. True if you are not going from a regular hardware to: • Exadata Cloud Service • Exadata Cloud at Customer
  20. 20. 20Copyright © 2018 Accenture. All rights reserved. OVERVIEW DATABASE TUNING vs SQL TUNING
  21. 21. OVERVIEW DEVELOPERS vs SQL
  22. 22. I have a pretty simple mantra when it comes to developing database software, and I have written this many times over the years: • You should do it in a single SQL statement if at all possible. • If you cannot do it in a single SQL statement, do it in PL/SQL. • If you cannot do it in PL/SQL, try a Java stored procedure. • If you cannot do it in Java, do it in a C external procedure. • If you cannot do it in a C external procedure, you might want to seriously think about why it is you need to do it. TOM KYTE Copyright © 2018 Accenture. All rights reserved. 22
  23. 23. SQL TUNING 101 LET’S BEGIN….
  24. 24. SQL TUNING 101 THE APPLICATION IS RUNNING SLOW TODAY! NOTHING HAS CHANGED!
  25. 25. • Database upgraded • Statistics gathered (automatic?) • Schema changed • Database parameter changed • Application changed • Operating system (OS) and hardware changed • Data volume changed • More active users YES, SOMETHING HAS CHANGED Copyright © 2018 Accenture. All rights reserved. 25
  26. 26. • One SQL? • All the database? • ASH and AWR HOW TO START TROUBLESHOOTING? Copyright © 2018 Accenture. All rights reserved. 26 **Requires Oracle Diagnostics Pack
  27. 27. Copyright © 2018 Accenture. All rights reserved. 27 SQL TUNING PROACTIVE
  28. 28. Copyright © 2018 Accenture. All rights reserved. 28 SQL TUNING REACTIVE
  29. 29. Copyright © 2018 Accenture. All rights reserved. 29 PROACTIVE – TOOLS
  30. 30. Copyright © 2018 Accenture. All rights reserved. 30 EDB360 - TOP SQLS
  31. 31. Copyright © 2018 Accenture. All rights reserved. 31 EDB360 - TOP EVENTS
  32. 32. EDB360 DEMO
  33. 33. • SQL ordered by Elapsed Time • SQL ordered by CPU Time • SQL ordered by User I/O Wait Time • SQL ordered by Gets • SQL ordered by Reads • SQL ordered by Physical Reads (UnOptimized) • SQL ordered by Executions • SQL ordered by Parse Calls • SQL ordered by Sharable Memory • SQL ordered by Version Count • SQL ordered by Cluster Wait Time AWR REPORT Copyright © 2018 Accenture. All rights reserved. 33
  34. 34. AWR REPORT DEMO
  35. 35. REACTIVE - TOOLS Copyright © 2018 Accenture. All rights reserved. 35
  36. 36. • Pros: Supported by Oracle • Cons: Requires Installation SQLT (SQLTXPLAIN) DIAGNOSTIC TOOL (DOC ID 215187.1) Copyright © 2018 Accenture. All rights reserved. 36
  37. 37. SQLT (SQLTXPLAIN) DIAGNOSTIC TOOL (DOC ID 215187.1) Copyright © 2018 Accenture. All rights reserved. 37
  38. 38. • Pros: No Installation • Cons: Might miss some information under rare circumstances SQLD360 Copyright © 2018 Accenture. All rights reserved. 38
  39. 39. https://mauro-pagano.com/2017/04/29/sqltxplain-vs-sqld360-differences-and-similarities/ SQLTXPLAIN VS SQLD360 Copyright © 2018 Accenture. All rights reserved. 39
  40. 40. Download the free tool: • https://github.com/mauropagano/sqld360/ Execution: • SQL> sqld360.sql <sql_id> [N|D|T] SQLD360 – HOW TO USE Copyright © 2018 Accenture. All rights reserved. 40
  41. 41. SQLD360 Copyright © 2018 Accenture. All rights reserved. 41
  42. 42. SQLD360 Copyright © 2018 Accenture. All rights reserved. 42
  43. 43. SQLD360 Copyright © 2018 Accenture. All rights reserved. 43
  44. 44. SQLD360 Copyright © 2018 Accenture. All rights reserved. 44
  45. 45. SQLD360 DEMO
  46. 46. If you want to know more: • https://mauro-pagano.com/category/sqld360/ SQLD360 Copyright © 2018 Accenture. All rights reserved. 46
  47. 47. https://mauro-pagano.com/category/sqld360/ Copyright © 2018 Accenture. All rights reserved. 47 SQLDB360
  48. 48. EXPLAIN PLAN HOW ABOUT EXPLAIN PLAN?
  49. 49. Copyright © 2018 Accenture. All rights reserved. 49 EXPLAIN PLAN Explain Plain lies!
  50. 50. It does not actually execute the SQL statement. Runtime factors can influence choice of plan: • Dynamic Sampling • Bind Variable Peeking • Adaptive Features EXPLAIN PLAN Copyright © 2018 Accenture. All rights reserved. 50
  51. 51. • DBMS_XPLAN.DISPLAY_CURSOR • V$SQL_PLAN% NOW WHAT? Copyright © 2018 Accenture. All rights reserved. 51
  52. 52. SQL_ID CURSOR_CHILD_NO (default 0) FORMAT • TYPICAL = DEFAULT • ALL = TYPICAL + QB + PROJECTION + ALIAS + REMOTE • ADVANCED = ALL + OUTLINE + BINDS • ALLSTATS = IOSTATS + MEMSTATS (all executions) • ALLSTATS LAST (last execution) • ADAPTIVE (12c) DBMS_XPLAN.DISPLAY_CURSOR Copyright © 2018 Accenture. All rights reserved. 52
  53. 53. DBMS_XPLAN Copyright © 2018 Accenture. All rights reserved. 53
  54. 54. Display plan for the last SQL executed: • select * from table(dbms_xplan.display_cursor()); DBMS_XPLAN Copyright © 2018 Accenture. All rights reserved. 54
  55. 55. Display plan for one specific SQL • Get SQL_ID: DBMS_XPLAN Copyright © 2018 Accenture. All rights reserved. 55
  56. 56. SQL_ID - 18C Copyright © 2018 Accenture. All rights reserved. 56
  57. 57. Copyright © 2018 Accenture. All rights reserved. 57 DBMS_XPLAN
  58. 58. DBMS_XPLAN DEMO
  59. 59. DBMS_XPLAN CHILD NUMBER?
  60. 60. DBMS_XPLAN DEMO
  61. 61. /*+ gather_plan_statistics */ or Alter session set statistics_level = ALL https://blogs.oracle.com/optimizer/entry/how_do_i_know_if ESTIMATE VS ACTUAL Copyright © 2018 Accenture. All rights reserved. 61
  62. 62. ESTIMATE VS ACTUAL DEMO
  63. 63. • Introduced in Oracle Database 11g. • Automatically started when a SQL statement either runs in parallel or has consumed at least 5 seconds of combined CPU and I/O. • Hint /*+ MONITOR */ SQL MONITOR Copyright © 2018 Accenture. All rights reserved. 63
  64. 64. • Oracle Enterprise Manager • EM Database Express (12c) • SQL Developer • Command Line SQL MONITOR Copyright © 2018 Accenture. All rights reserved. 64 **Part of the Oracle Tuning Pack
  65. 65. select dbms_sqltune.report_sql_monitor( sql_id => 'gjabwvvr07w09’, report_level=>'ALL’, type => 'ACTIVE') from dual; SQL MONITOR Copyright © 2018 Accenture. All rights reserved. 65
  66. 66. SQL MONITOR Copyright © 2018 Accenture. All rights reserved. 66
  67. 67. SQL MONITOR Copyright © 2018 Accenture. All rights reserved. 67
  68. 68. SQL MONITOR DEMO
  69. 69. COST OF AN INDEX Copyright © 2018 Accenture. All rights reserved. 69
  70. 70. Why this session? • “FTS is bad” biggest myth in SQL Tuning • FTS distracts people from real root cause • FTS often not performed at its best FULL TABLE SCAN Copyright © 2018 Accenture. All rights reserved. 70 https://www.slideshare.net/MauroPagano3/full-table-scan-friend-or-foe
  71. 71. Why FTS rocks? • Can crunch A LOT of data efficiently (*) – Couple of O more data than index scans per disk • Full Scan (~200MB/s per disk) – Wait IO seek + latency per (large) chunk – Parallelizes well, increasing bandwidth (GB/s) • Index Scan (~1.5MB/s per disk) – Wait IO seek + latency per block FULL TABLE SCAN Copyright © 2018 Accenture. All rights reserved. 71 https://www.slideshare.net/MauroPagano3/full-table-scan-friend-or-foe
  72. 72. FULL TABLE SCAN Copyright © 2018 Accenture. All rights reserved. 72 https://richardfoote.wordpress.com/2008/05/12/index-scan-or-full-table-scan-the-magic-number-magic-dance/
  73. 73. • Clustering Factor --> How well ordered the rows in the table are in relation to the index. • Selectivity of the query • Number of table blocks • Effective multiblock read count • Relative cost of single vs. multiblock I/Os • Parallelism • Etc FULL TABLE SCAN Copyright © 2018 Accenture. All rights reserved. 73
  74. 74. Why FTS doesn’t rocks? • FTS read ~100x faster than index • Need to read the whole segment – GB to read just few rows • Concurrent user share bandwidth – More user less resources for each • Index faster if filters less than ~1% of data – Assuming data comes all from disk FULL TABLE SCAN Copyright © 2018 Accenture. All rights reserved. 74 https://www.slideshare.net/MauroPagano3/full-table-scan-friend-or-foe
  75. 75. • Not always good • Not always bad FULL TABLE SCAN Copyright © 2018 Accenture. All rights reserved. 75
  76. 76. Copyright © 2018 Accenture. All rights reserved. 76 ANALYZING TABLES
  77. 77. ANALYZING TABLES Copyright © 2018 Accenture. All rights reserved. 77 https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4005.htm
  78. 78. https://blog.jooq.org/2018/04/19/the-performance-difference-between-sql-row-by-row-updating-batch-updating- and-bulk-updating/ Copyright © 2018 Accenture. All rights reserved. 78 ROW-BY-ROW PROCESSING
  79. 79. Copyright © 2018 Accenture. All rights reserved. 79 ROW-BY-ROW
  80. 80. Copyright © 2018 Accenture. All rights reserved. 80 ROW-BY-ROW
  81. 81. Copyright © 2018 Accenture. All rights reserved. 81 PL/SQL FORALL
  82. 82. Copyright © 2018 Accenture. All rights reserved. 82 ONE UPDATE
  83. 83. Copyright © 2018 Accenture. All rights reserved. 83 EVENT 10053
  84. 84. ALTER SESSION SET EVENTS '10053 trace name context forever, level 1'; SELECT * FROM EMP WHERE ENAME = 'SCOTT'; ALTER SESSION SET EVENTS '10053 trace name context off'; EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 84
  85. 85. Level 1 or Level 2? • Parameters used by the optimizer (level 1 only) • Index statistics (level 1 only) • Column statistics • Single Access Paths • Join Costs • Table Joins Considered • Join Methods Considered (NL/MS/HA) EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 85
  86. 86. For another session: SYS.DBMS_SYSTEM.SET_EV (SID,SERIAL ,10053,1) Disable: SYS.DBMS_SYSTEM.SET_EV (SID,SERIAL ,10053,0) EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 86
  87. 87. 11G R1 ALTER SESSION SET EVENTS 'trace[rdbms.SQL_Optimizer.*][sql:my_sql_id]'; ALTER SESSION SET EVENTS 'trace[rdbms.SQL_Optimizer.*] off'; EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 87
  88. 88. 11G R2 – 18c dbms_sqldiag.dump_trace( p_sql_id=>’92c3fw9svc3rc’, p_child_number=>0, p_component=>'Compiler', p_file_id=>’MY_Trace_File'); EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 88 ** Doesn’t require you to re-execute the statement.
  89. 89. EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 89
  90. 90. ALTER SESSION SET EVENTS '10053 trace name context forever, level 1'; SELECT * FROM tb_master p WHERE EXISTS (SELECT 1 FROM tb_detail f WHERE f.id_master=p.id); ALTER SESSION SET EVENTS '10053 trace name context off'; EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 90
  91. 91. Copyright © 2018 Accenture. All rights reserved. 91 EVENT 10053
  92. 92. Copyright © 2018 Accenture. All rights reserved. 92 EVENT 10053
  93. 93. Copyright © 2018 Accenture. All rights reserved. 93 EVENT 10053 QUERY BLOCK – 2 parts
  94. 94. Copyright © 2018 Accenture. All rights reserved. 94 EVENT 10053 Parallelism - Disabled
  95. 95. Copyright © 2018 Accenture. All rights reserved. 95 EVENT 10053 Subtitles
  96. 96. Copyright © 2018 Accenture. All rights reserved. 96 EVENT 10053 Database Parameters
  97. 97. Copyright © 2018 Accenture. All rights reserved. 97 EVENT 10053 System Statistics
  98. 98. Copyright © 2018 Accenture. All rights reserved. 98 EVENT 10053 No statistics for these tables
  99. 99. Copyright © 2018 Accenture. All rights reserved. 99 EVENT 10053 Dynamic Sampling
  100. 100. Copyright © 2018 Accenture. All rights reserved. 100 EVENT 10053 Full Table Scan – No Index
  101. 101. Copyright © 2018 Accenture. All rights reserved. 101 EVENT 10053 Original: Final: Final Query
  102. 102. Copyright © 2018 Accenture. All rights reserved. 102 EVENT 10053 Final Plan
  103. 103. EVENT 10053 EXAMPLE 2
  104. 104. Copyright © 2018 Accenture. All rights reserved. 104 EVENT 10053 Let’s run this one
  105. 105. Copyright © 2018 Accenture. All rights reserved. 105 EVENT 10053 Original: Final:
  106. 106. EVENT 10053 QUERY TRANSFORMATIONS
  107. 107. Copyright © 2018 Accenture. All rights reserved. 107 OR EXPANSION Original: Changed: SELECT * FROM emp WHERE job = 'CLERK' OR deptno = 10; SELECT * FROM emp WHERE job = 'CLERK’ UNION ALL SELECT * FROM emp WHERE deptno = 10 AND job <> 'CLERK';
  108. 108. Copyright © 2018 Accenture. All rights reserved. 108 SUBQUERY UNNESTING Original: Changed: SELECT * FROM accounts WHERE custno IN (SELECT custno FROM customers); SELECT accounts.* FROM accounts, customers WHERE accounts.custno = customers.custno;
  109. 109. CREATE VIEW emp_10 AS SELECT empno, ename, job, sal, comm, deptno FROM emp WHERE deptno = 10; SELECT empno FROM emp_10 WHERE empno > 7800; Copyright © 2018 Accenture. All rights reserved. 109 VIEW MERGING Original: Changed: SELECT empno FROM emp WHERE deptno = 10 AND empno > 7800;
  110. 110. CREATE VIEW two_emp_tables AS SELECT empno, ename, job, sal, comm, deptno FROM emp1 UNION SELECT empno, ename, job, sal, comm, deptno FROM emp2; SELECT ename FROM two_emp_tables WHERE deptno = 20; Copyright © 2018 Accenture. All rights reserved. 110 PREDICATE PUSHING Original: Changed: SELECT ename FROM ( SELECT empno, ename, job,sal, comm, deptno FROM emp1 WHERE deptno = 20 UNION SELECT empno, ename, job,sal, comm, deptno FROM emp2 WHERE deptno = 20 );
  111. 111. SELECT * FROM emp, dept WHERE emp.deptno = 20 AND emp.deptno = dept.deptno; Copyright © 2018 Accenture. All rights reserved. 111 TRANSITIVITY Original: Changed: SELECT * FROM emp, dept WHERE emp.deptno = 20 AND emp.deptno = dept.deptno AND dept.deptno = 20;
  112. 112. COUNT(1) VS COUNT(*) WHAT IS THE DIFFERENCE BETWEEN COUNT(1) AND COUNT(*) ?
  113. 113. COUNT(1) VS COUNT(*) NOTHING
  114. 114. Copyright © 2018 Accenture. All rights reserved. 114 COUNT(1) VS COUNT(*)
  115. 115. MORE ABOUT THE EVENT 10053 Copyright © 2018 Accenture. All rights reserved. 115
  116. 116. Copyright © 2018 Accenture. All rights reserved. 116
  117. 117. Oracle Database 12.1 OPTIMIZER_ADAPTIVE_FEATURES Default TRUE ADAPTIVE FEATURES Copyright © 2018 Accenture. All rights reserved. 117
  118. 118. Oracle Database 12.2 introduces the new split-up adaptive parameters: • OPTIMIZER_ADAPTIVE_PLANS • OPTIMITER_ADAPTIVE_STATISTICS ADAPTIVE FEATURES Copyright © 2018 Accenture. All rights reserved. 118
  119. 119. On Oracle Database 12.1 this can be achieved by installing two patches. ADAPTIVE FEATURES Copyright © 2018 Accenture. All rights reserved. 119
  120. 120. The patch for bug# 22652097 introduces the two parameters OPTIMIZER_ADAPTIVE_PLANS and OPTIMIZER_ADAPTIVE_STATISTICS, and in addition removes the parameter OPTIMIZER_ADAPTIVE_FEATURES. The patch for bug# 21171382 disables the automatic creation of extended statistics unless the optimizer preference AUTO_STATS_EXTENSIONS is set to ON. ADAPTIVE FEATURES Copyright © 2018 Accenture. All rights reserved. 120
  121. 121. HINTS SHOULD I USE?
  122. 122. Query running on M7: 180 Minutes Query after db clone P8+Storage ? • Time: 29 hours Query running on M7 after SQL tuning: 17 minutes SQL TUNING ON EXADATA Copyright © 2018 Accenture. All rights reserved. 122
  123. 123. SQL TUNING ON EXADATA Copyright © 2018 Accenture. All rights reserved. 123
  124. 124. Copyright © 2018 Accenture. All rights reserved. 124
  125. 125. Set table preferences: begin dbms_stats.set_table_prefs ( ownname => 'SCOTT’, tabname => 'EMP', pname => 'PUBLISH', pvalue => 'FALSE' ); end; Collect the statistics. PENDING STATISTICS Copyright © 2018 Accenture. All rights reserved. 125
  126. 126. select num_rows, to_char(last_analyzed,'dd/mm/rrrr hh24:mi:ss’) from all_tab_pending_stats where table_name = 'EMP'; PENDING STATISTICS Copyright © 2018 Accenture. All rights reserved. 126
  127. 127. alter session set optimizer_use_pending_statistics = true; Test your queries. PENDING STATISTICS Copyright © 2018 Accenture. All rights reserved. 127
  128. 128. If it’s ok: dbms_stats.publish_pending_stats('SCOTT', 'EMP’); or: dbms_stats.delete_pending_stats(’SCOTT',’EMP'); PENDING STATISTICS Copyright © 2018 Accenture. All rights reserved. 128
  129. 129. Check the retention: select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual; Default is 31 days. RESTORE STATISTICS FROM HISTORY Copyright © 2018 Accenture. All rights reserved. 129
  130. 130. Statistics available for the table: SELECT OWNER, TABLE_NAME, STATS_UPDATE_TIME FROM dba_tab_stats_history WHERE table_name='MY_TABLE'; RESTORE STATISTICS FROM HISTORY Copyright © 2018 Accenture. All rights reserved. 130
  131. 131. Begin dbms_stats.restore_table_stats( 'SCOTT', 'EMP', ‘08-NOV-16 11.38.05.015640 AM +08:00’); End; RESTORE STATISTICS FROM HISTORY Copyright © 2018 Accenture. All rights reserved. 131
  132. 132. begin dbms_stats.CREATE_STAT_TABLE( ownname=>user , stattab=>'MY_STATS_TABLE’ ); end; begin dbms_stats.export_schema_stats( ownname=>user , stattab=>'MY_STATS_TABLE’ , statid=>'CURRENT_STATS’ ); end; EXPORT AND IMPORT SCHEMA STATISTICS Copyright © 2018 Accenture. All rights reserved. 132
  133. 133. EXPDP / IMPDP begin dbms_stats.import_schema_stats( ownname=>user , stattab=>'MY_STATS_TABLE’ , statid=>'CURRENT_STATS’ ); End; EXPORT AND IMPORT SCHEMA STATISTICS Copyright © 2018 Accenture. All rights reserved. 133
  134. 134. Incremental statistics for partitioned tables: dbms_stats.set_table_prefs(null,'SALES','INCREMENTAL','TRUE') INCREMENTAL STATISTICS Copyright © 2018 Accenture. All rights reserved. 134
  135. 135. INCREMENTAL STATISTICS Copyright © 2018 Accenture. All rights reserved. 135
  136. 136. CREATE INDEX index_name ON table_name(column_name) INVISIBLE; ALTER INDEX index_name INVISIBLE; ALTER INDEX index_name VISIBLE; INVISIBLE INDEXES Copyright © 2018 Accenture. All rights reserved. 136
  137. 137. ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES=TRUE; INVISIBLE INDEXES Copyright © 2018 Accenture. All rights reserved. 137
  138. 138. Copyright © 2018 Accenture. All rights reserved. 138
  139. 139. ALTER SESSION SET sql_trace=TRUE; ALTER SESSION SET sql_trace=FALSE; EXEC DBMS_SESSION.set_sql_trace(sql_trace => TRUE); EXEC DBMS_SESSION.set_sql_trace(sql_trace => FALSE); ALTER SESSION SET EVENTS '10046 trace name context forever, level 12’; ALTER SESSION SET EVENTS '10046 trace name context off’; EXEC DBMS_SYSTEM.set_sql_trace_in_session(sid=>0000, serial#=>0000, sql_trace=>TRUE); EXEC DBMS_SYSTEM.set_sql_trace_in_session(sid=>0000, serial#=>0000, sql_trace=>FALSE); SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 139
  140. 140. CONN sys/password AS SYSDBA; ORADEBUG SETMYPID; ORADEBUG SETOSPID 0000; ORADEBUG SETORAPID 000000; ORADEBUG EVENT 10046 TRACE NAME CONTEXT FOREVER, LEVEL 12; ORADEBUG EVENT 10046 TRACE NAME CONTEXT OFF; SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 140
  141. 141. EXEC DBMS_SUPPORT.start_trace(waits=>TRUE, binds=>TRUE); EXEC DBMS_SUPPORT.stop_trace; EXEC DBMS_SUPPORT.start_trace_in_session(sid=>0000, serial=>000000, waits=>TRUE, binds=>TRUE); EXEC DBMS_SUPPORT.stop_trace_in_session(sid=>0000, serial=>000000); SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 141
  142. 142. 10g: DBMS_MONITOR.session_trace_enable(waits=>TRUE, binds=>FALSE); DBMS_MONITOR.session_trace_enable(session_id =>0000, serial_num=>000000, waits=>TRUE, binds=>TRUE); DBMS_MONITOR.client_id_trace_enable(client_id=>'my_client', waits=>TRUE, binds=>TRUE); DBMS_MONITOR.serv_mod_act_trace_enable(service_name=>'my_srv', module_name=>'my_test', action_name=>'calculating', waits=>TRUE, binds=>TRUE); SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 142
  143. 143. ALTER SESSION SET TRACEFILE_IDENTIFIER = "MY_TRC_FILE"; SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 143
  144. 144. SELECT p.tracefile FROM v$session s JOIN v$process p ON s.paddr = p.addr WHERE s.sid = MY_SID; SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 144
  145. 145. 12.2 v$diag_trace_file v$diag_trace_file_contents SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 145
  146. 146. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 146
  147. 147. ALTER SESSION SET EVENTS '10046 trace name context forever, level 12'; SELECT * FROM tb_pai p WHERE EXISTS (SELECT 1 FROM tb_filho f WHERE f.id_pai=p.id); ALTER SESSION SET EVENTS '10046 trace name context off'; EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 147
  148. 148. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 148
  149. 149. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 149
  150. 150. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 150
  151. 151. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 151
  152. 152. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 152
  153. 153. EVENT 10046 Copyright © 2018 Accenture. All rights reserved. 153
  154. 154. • TKPROF • TRCSESS • Trace Analyzer TRCANLZR (TRCA) • Method R Tools SQL TRACE, 10046, TRCSESS AND TKPROF Copyright © 2018 Accenture. All rights reserved. 154
  155. 155. Copyright © 2018 Accenture. All rights reserved. 155
  156. 156. Copyright © 2018 Accenture. All rights reserved. 156 SQL TRACE, 10046, TRCSESS AND TKPROF http://www.oraclenerd.com/2011/02/sql-developer-mr-trace.html
  157. 157. Copyright © 2018 Accenture. All rights reserved. 157 SQLCL http://www.oracle.com/technetwork/developer-tools/sqlcl/downloads/index.html
  158. 158. QUESTIONS?
  159. 159. THANK YOUSlides will be available at www.slideshare.net

×