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.

Oracle SQL Tuning

123 views

Published on

Presentation for DBA Brasil 2.0

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Oracle SQL Tuning

  1. 1. Oracle SQL Tuning 101 Alex Zaballa, Accenture Enkitec Group
  2. 2. Alex Zaballa http://alexzaballa.blogspot.com/ @alexzaballa https://www.linkedin.com/in/alexzaballa Worked for 3 years as a Clipper/Delphi Developer (15 years old) Worked for 7 years in Brazil as an Oracle Developer. Worked 8 years for the Ministry of Finance In Angola as a DBA March - 2007 until March - 2015*
  3. 3. TUNING e não TANING
  4. 4. Oracle Database Enterprise Edition + Diagnostics Pack + Tuning Pack *
  5. 5. Database Tuning Vs SQL Tuning *
  6. 6. Desenvolvedores vs SQL *
  7. 7. •Aplicações multi-banco •ORM : Object Relational Mapper
  8. 8. ”The Oracle optimizer uses the constraints to make better decisions on join paths.”
  9. 9. Cost of an Index FKs ** In-Memory** *
  10. 10. Nada mudou e hoje a aplicação está lenta!
  11. 11. Mudanças que podem trazer problemas • Database upgraded • Statistics gathered • Schema changed • Database parameter changed • Application changed • Operating system (OS) and hardware changed • Data volume changed by more active users
  12. 12. Ferramentas para SQL Tuning - Oracle • SQL Trace (and TKPROF) • Active Session History (ASH) • EXPLAIN PLAN FOR • AUTOTRACE • SQL Developer • DBMS_XPLAN • SQL Monitor
  13. 13. ASH e AWR • 1 sec snapshots of V$SESSION into ASH • Every 10 ASH samples into AWR **Requires Oracle Diagnostics Pack *
  14. 14. Tom Kyte
  15. 15. Tipos de SQL Tuning Proativo
  16. 16. Tipos de SQL Tuning Reativo *
  17. 17. Proativo
  18. 18. eDB360 *
  19. 19. Top SQLs *
  20. 20. Top Events
  21. 21. AWR • 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*
  22. 22. SQL ordered by Elapsed Time
  23. 23. SQL ordered by Executions - RAC
  24. 24. Reativo
  25. 25. *
  26. 26. Reativo - SQLT Diagnostic Tool (Doc ID 215187.1) • Prós: Suportada e atualizada pela Oracle • Contras: Necessário instalação Criada pelo Carlos Sierra *
  27. 27. Reativo - SQLD360 Criada pelo Mauro Pagano *
  28. 28. Reativo - SQLD360
  29. 29. Reativo - SQLD360
  30. 30. Explain Plan?
  31. 31. Explain Plan Apenas tenta prever o plano que será executado!
  32. 32. Explain Plan • Bind Variable Peeking • Adaptive Features • Etc
  33. 33. Now what? • DBMS_XPLAN.DISPLAY_CURSOR • V$SQL_PLAN%
  34. 34. DBMS_XPLAN.DISPLAY_CURSOR • 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)
  35. 35. DBMS_XPLAN • DISPLAY (from PLAN_TABLE) • DISPLAY_CURSOR • DISPLAY_AWR • DISPLAY_SQL_PLAN_BASELINE • DISPLAY_SQL_SET SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('sql_id',child,'ADVANCED'));
  36. 36. By Carlos Sierra
  37. 37. Estimate vs Actual https://blogs.oracle.com/optimizer/entry/how_do_i_know_if /*+ gather_plan_statistics */ or Alter session set statistics_level = ALL 
  38. 38. SQL MONITOR • Real-time SQL monitoring foi introduzido no Oracle Database 11g. • Parallel queries, DML e DDL são automaticamente monitorados. • SQLs que consumem 5 segundos ou mais segundos de CPU ou I/O em uma única execução • Hint /*+ MONITOR */
  39. 39. SQL MONITOR • Oracle Enterprise Manager • EM Database Express (12c) • SQL Developer • Linha de Comando **Part of the Oracle Tuning Pack
  40. 40. SQL MONITOR select dbms_sqltune.report_sql_monitor( sql_id => 'gjabwvvr07w09', report_level=>'ALL', type => 'ACTIVE') from dual;
  41. 41. SQL MONITOR
  42. 42. SQL MONITOR
  43. 43. Evento 10053 Mais conhecido como ”Wolfganging” ” Wolfgang Breitling was the first guy to really analyze the content of these 10053 trace files and publish his findings.” Wolfgang wrote a paper called “A Look Under the Hood of CBO.”
  44. 44. Evento 10053 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';
  45. 45. Evento 10053 Level 1 or Level 2? •1. Parameters used by the optimizer (level 1 only) •2. Index statistics (level 1 only) •3. Column statistics •4. Single Access Paths •5. Join Costs •6. Table Joins Considered •7. Join Methods Considered (NL/MS/HA)
  46. 46. Evento 10053 For another session: SYS.DBMS_SYSTEM.SET_EV (SID,SERIAL ,10053,1) Disable: SYS.DBMS_SYSTEM.SET_EV (SID,SERIAL ,10053,0)
  47. 47. Evento 10053 11G R1 – SQL Inside PL/SQL ALTER SESSION SET EVENTS 'trace[rdbms.SQL_Optimizer.*][sql:my_sql_id]'; ALTER SESSION SET EVENTS 'trace[rdbms.SQL_Optimizer.*] off';
  48. 48. Evento 10053 11G R2 dbms_sqldiag.dump_trace( p_sql_id=>’92c3fw9svc3rc’, p_child_number=>0, p_component=>'Compiler', p_file_id=>’MY_Trace_File'); ** Doesn’t require you to re-execute the statement.
  49. 49. Evento 10053
  50. 50. Evento 10053 ALTER SESSION SET EVENTS '10053 trace name context forever, level 1'; SELECT * FROM tb_pai p WHERE EXISTS (SELECT 1 FROM tb_filho f WHERE f.id_pai=p.id); ALTER SESSION SET EVENTS '10053 trace name context off';
  51. 51. Evento 10053
  52. 52. Evento 10053
  53. 53. Evento 10053 QUERY BLOCK – 2 partes da query
  54. 54. Evento 10053 Paralelismo desabilitado
  55. 55. Evento 10053
  56. 56. Evento 10053
  57. 57. Evento 10053 System Statistics
  58. 58. Evento 10053 Sem Estatísticas
  59. 59. Evento 10053 Dynamic Sampling
  60. 60. Evento 10053 Table Scan – Sem Índices
  61. 61. Evento 10053 Best Join Order
  62. 62. Evento 10053
  63. 63. Evento 10053
  64. 64. Evento 10053 Exemplo 2
  65. 65. Evento 10053
  66. 66. Evento 10053
  67. 67. OR Expansion Query Original: SELECT * FROM emp WHERE job = 'CLERK' OR deptno = 10; Query Transformada: SELECT * FROM emp WHERE job = 'CLERK’ UNION ALL SELECT * FROM emp WHERE deptno = 10 AND job <> 'CLERK';
  68. 68. Subquery Unnesting Query Original: SELECT * FROM accounts WHERE custno IN (SELECT custno FROM customers); Query Transformada: SELECT accounts.* FROM accounts, customers WHERE accounts.custno = customers.custno;
  69. 69. View Merging Query Original: 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; Query transformada: SELECT empno FROM emp WHERE deptno = 10 AND empno > 7800;
  70. 70. Predicate Pushing Query Original: 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;
  71. 71. Predicate Pushing Query Transformada: 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 );
  72. 72. Transitivity Query Original: SELECT * FROM emp, dept WHERE emp.deptno = 20 AND emp.deptno = dept.deptno; Query Transformada: SELECT * FROM emp, dept WHERE emp.deptno = 20 AND emp.deptno = dept.deptno AND dept.deptno = 20;
  73. 73. Evento 10053
  74. 74. Full Table Scan https://www.slideshare.net/MauroPagano3/ full-table-scan-friend-or-foe *
  75. 75. Full Table Scan https://www.slideshare.net/MauroPagano3/ full-table-scan-friend-or-foe
  76. 76. Full Table Scan https://richardfoote.wordpress.com/2008/05/12/index-scan- or-full-table-scan-the-magic-number-magic-dance/ *
  77. 77. Full Table Scan • 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
  78. 78. Full Table Scan https://www.slideshare.net/MauroPagano3/ full-table-scan-friend-or-foe
  79. 79. Full Table Scan
  80. 80. Adaptive Features Oracle Database 12.1 •OPTIMIZER_ADAPTIVE_FEATURES Default TRUE
  81. 81. Adaptive Features Oracle Database 12.2 introduces the new split-up adaptive parameters: •OPTIMIZER_ADAPTIVE_PLANS  •OPTIMITER_ADAPTIVE_STATISTICS.
  82. 82. Adaptive Features • On Oracle Database 12.1this can be achieved by installing two patches.
  83. 83. Adaptive Features 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.
  84. 84. Statistics https://blogs.oracle.com/optimizer/entry/improvement_of_auto_sampling_statistics _gathering_feature_in_oracle_database_11g estimate_percent => dbms_stats.auto_sample_size
  85. 85. Hints • Devo utilizar ? • O desenvolvedores/dbas sabem mais que o otimizador?
  86. 86. Hints • Use para fornecer dados ao otimizador que as estatísticas falharam em informar; • Use com precaução; • Use como último recurso.
  87. 87. SQL Tuning on EXADATA • Tempo da query na M7: 180 minutos • Tempo após clone - P8+Storage ? • Tempo após ajuste da query: 29 horas • Tempos após ajuste no M7: 17 minutos
  88. 88. SQL Tuning on EXADATA
  89. 89. Pending Statistics Set table preferences: begin dbms_stats.set_table_prefs ( ownname => 'SCOTT', tabname => 'EMP', pname => 'PUBLISH', pvalue => 'FALSE' ); end; Collect the statistics.
  90. 90. Pending Statistics select num_rows, to_char(last_analyzed,'dd/mm/rrrr hh24:mi:ss') from all_tab_pending_stats where table_name = 'EMP';
  91. 91. Pending Statistics alter session set optimizer_use_pending_statistics = true; Test the queries.
  92. 92. Pending Statistics If it’s ok: dbms_stats.publish_pending_stats('SCOTT', 'EMP'); or: dbms_stats.delete_pending_stats(’SCOTT', ’EMP');
  93. 93. Restore Statistics from History Check the retention: select DBMS_STATS.GET_STATS_HISTORY_RETENTI ON from dual; Default is 31 days.
  94. 94. Restore Statistics from History Statistics available for the table: SELECT OWNER, TABLE_NAME, STATS_UPDATE_TIME FROM dba_tab_stats_history WHERE table_name='MY_TABLE';
  95. 95. Restore Statistics from History Begin dbms_stats.restore_table_stats( 'SCOTT', 'EMP', ‘08-NOV-16 11.38.05.015640 AM +08:00’); End;
  96. 96. Export and Import schema statistics 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;
  97. 97. Export and Import schema statistics EXPDP / IMPDP begin dbms_stats.import_schema_stats( ownname=>user , stattab=>'MY_STATS_TABLE' , statid=>'CURRENT_STATS' ); End;
  98. 98. Incremental Statistics Estatísticas incrementais para Tabelas Particionadas: dbms_stats.set_table_prefs(null,'SALES' ,'INCREMENTAL','TRUE') 11g**
  99. 99. Incremental Statistics
  100. 100. Invisible Indexes • CREATE INDEX index_name ON table_name(column_name) INVISIBLE; • ALTER INDEX index_name INVISIBLE; • ALTER INDEX index_name VISIBLE;
  101. 101. Invisible Indexes ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES=TRUE;
  102. 102. Row-by-Row Processing vs Bulk Processing Instead of fetching a single row at a time it is possible to use the bulk features.
  103. 103. Row-by-Row Processing vs Bulk Processing
  104. 104. Row-by-Row Processing vs Bulk Processing *
  105. 105. SQL trace, 10046, trcsess and tkprof 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);
  106. 106. SQL trace, 10046, trcsess and tkprof 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;
  107. 107. SQL trace, 10046, trcsess and tkprof 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);
  108. 108. SQL trace, 10046, trcsess and tkprof 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);
  109. 109. SQL trace, 10046, trcsess and tkprof ALTER SESSION SET TRACEFILE_IDENTIFIER = "MY_TRC_FILE";
  110. 110. SQL trace, 10046, trcsess and tkprof SELECT p.tracefile FROM v$session s JOIN v$process p ON s.paddr = p.addr WHERE s.sid = MY_SID;
  111. 111. SQL trace, 10046, trcsess and tkprof 12.2 v$diag_trace_file v$diag_trace_file_contents
  112. 112. Evento 10046
  113. 113. Evento 10046 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';
  114. 114. Evento 10046
  115. 115. Evento 10046
  116. 116. Evento 10046
  117. 117. Evento 10046
  118. 118. Evento 10046
  119. 119. Evento 10046
  120. 120. SQL trace, 10046, trcsess and tkprof • TKPROF • TRCSESS • Trace Analyzer TRCANLZR (TRCA) • Method R Tools
  121. 121. SQL trace, 10046, trcsess and tkprof
  122. 122. SQL trace, 10046, trcsess and tkprof http://www.oraclenerd.com/2011/02/sql-developer-mr-trace.html
  123. 123. Questions?
  124. 124. Thank You Slides Available: http://www.slideshare.net/

×