13. 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
14. Ferramentas para SQL Tuning -
Oracle
• SQL Trace (and TKPROF)
• Active Session History (ASH)
• EXPLAIN PLAN FOR
• AUTOTRACE
• SQL Developer
• DBMS_XPLAN
• SQL Monitor
15. ASH e AWR
• 1 sec snapshots of V$SESSION into ASH
• Every 10 ASH samples into AWR
**Requires Oracle Diagnostics Pack
*
23. 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*
28. Reativo - SQLT Diagnostic Tool
(Doc ID 215187.1)
• Prós: Suportada e atualizada pela Oracle
• Contras: Necessário instalação
Criada pelo Carlos Sierra
*
41. 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 */
42. SQL MONITOR
• Oracle Enterprise Manager
• EM Database Express (12c)
• SQL Developer
• Linha de Comando
**Part of the Oracle Tuning Pack
46. 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.”
47. 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';
48. 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)
49. Evento 10053
For another session:
SYS.DBMS_SYSTEM.SET_EV (SID,SERIAL ,10053,1)
Disable:
SYS.DBMS_SYSTEM.SET_EV (SID,SERIAL ,10053,0)
50. 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';
53. 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';
70. 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';
71. 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;
72. 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;
73. 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;
74. 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 );
75. 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;
80. 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
84. Adaptive Features
Oracle Database 12.2 introduces the new
split-up adaptive parameters:
• OPTIMIZER_ADAPTIVE_PLANS
• OPTIMITER_ADAPTIVE_STATISTICS.
85. Adaptive Features
• On Oracle Database 12.1this can be
achieved by installing two patches.
86. 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.
89. Hints
• Use para fornecer dados ao otimizador
que as estatísticas falharam em informar;
• Use com precaução;
• Use como último recurso.
90. 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
97. Restore Statistics from History
Check the retention:
select
DBMS_STATS.GET_STATS_HISTORY_RETENTI
ON from dual;
Default is 31 days.
98. 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';
99. Restore Statistics from History
Begin
dbms_stats.restore_table_stats(
'SCOTT',
'EMP',
‘08-NOV-16 11.38.05.015640 AM +08:00’);
End;
100. 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;
101. Export and Import schema
statistics
EXPDP / IMPDP
begin
dbms_stats.import_schema_stats( ownname=>user
, stattab=>'MY_STATS_TABLE'
, statid=>'CURRENT_STATS'
);
End;
104. Invisible Indexes
• CREATE INDEX index_name ON
table_name(column_name) INVISIBLE;
• ALTER INDEX index_name INVISIBLE;
• ALTER INDEX index_name VISIBLE;
109. 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);
110. 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;
117. 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';