Beruflich Dokumente
Kultur Dokumente
? ?
Background y ?
h
50% mistype a statement W
&
9 out of 10 the incorrectly executed
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 2
Performance Tuning: How to write & run correctly
Programar siempre y de la misma manera es eficiente ! No!!
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 3
Performance Tuning: How to write & run correctly
Continuo aprendizaje
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 4
Performance Tuning: How to write & run correctly
Declaracin #1 absoluta
There's a tradeoff between efficiency (fewer conditional
statements) and ease of comprehension.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 5
Performance Tuning: How to write & run correctly
Declaracin #2 absoluta
The real world is very complicated; the software we write is supposed to map
those complexities into applications. The result is that we often end up
needing to deal with convoluted logical expressions.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 6
Performance Tuning: How to write & run correctly
Declaracin #3 absoluta
Following this best practice will make your code more readable and
expressive.
You avoid redundant code, always bad news in a program, since it increases
maintenance costs and the chance of introducing bugs into your code.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 7
Performance Tuning: How to write & run correctly
Declaracin #4 absoluta
You need not take out "programmer's insurance": "Gee, I don't know if I need
to declare that or not, so I'd better declare it." Instead, you make certain you
understand how PL/SQL works and write appropriate code.
"Vaya, no s si tengo que declararlo o no, as que ser mejor que lo declare.
En su lugar, asegrese de que entiende cmo funciona el PL/SQL y escriba
el cdigo apropiado.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 8
Performance Tuning: How to write & run correctly
Declaracin #5 absoluta
Your code doesn't do any unnecessary work and so executes more efficiently.
TIP: You can, in general, expect the performance of built-in functions such as
SUBSTR to work more efficiently in SQL than in PL/SQL, so move the
processing to the SQL layer whenever possible.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 9
Performance Tuning: How to write correctly
Thats it !
Thats it friends!
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 10
Performance Tuning: How to write correctly
quick example #1
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 11
employees by salary
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 12
Performance Tuning: How to write correctly
SQL>selectemployee_id,job_id,hire_date,salary
2fromemployees
3orderbysalary;
EMPLOYEE_IDJOB_IDHIRE_DATESALARY
132ST_CLERK10APR072100
136ST_CLERK06FEB082200
128ST_CLERK08MAR082200
127ST_CLERK14JAN072400
135ST_CLERK12DEC072400
191SH_CLERK19DEC072500
119PU_CLERK10AUG072500
140ST_CLERK06APR062500
144ST_CLERK09JUL062500
182SH_CLERK21JUN072500
131ST_CLERK16FEB052500
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 13
Performance Tuning: How to write correctly
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 14
Performance Tuning: How to write correctly
1selecte.employee_id,e.first_name,e.job_id,e.hire_date,e.salary,x.sequence
2fromemployeese,
3(selecte2.employee_id,count(*)sequencefromemployeese1,employeese2
4wheree1.hire_date<=e2.hire_date
5groupbye2.employee_id
6)x
7wheree.employee_id=x.employee_id
very
8*orderbysalary
SQL>/ complicated !!
EMPLOYEE_IDFIRST_NAMEJOB_IDHIRE_DATESALARYSEQUENCE
132TJST_CLERK10APR07210085
128StevenST_CLERK08MAR082200104
136HazelST_CLERK06FEB082200102
135KiST_CLERK12DEC07240095
127JamesST_CLERK14JAN07240078
144PeterST_CLERK09JUL06250071
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 15
Performance Tuning: How to write correctly
Is very simple ?
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 16
Performance Tuning: How to write correctly
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 17
Performance Tuning: How to write correctly
PLAN_TABLE_OUTPUT
3X FTS EMPLOYEES
Planhashvalue:2074273239
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
|0|SELECTSTATEMENT||107|5243|14(36)|00:00:01|
|1|SORTORDERBY||107|5243|14(36)|00:00:01|
|*2|HASHJOIN||107|5243|13(31)|00:00:01|
|3|TABLEACCESSFULL|EMPLOYEES|107|3424|3(0)|00:00:01|
|4|VIEW||107|1819|9(34)|00:00:01|
|5|HASHGROUPBY||107|2140|9(34)|00:00:01|
|6|MERGEJOIN||5783|112K|8(25)|00:00:01|
|7|SORTJOIN||107|856|4(25)|00:00:01|
|8|TABLEACCESSFULL|EMPLOYEES|107|856|3(0)|00:00:01|
|*9|SORTJOIN||107|1284|4(25)|00:00:01|
|10|TABLEACCESSFULL|EMPLOYEES|107|1284|3(0)|00:00:01|
PredicateInformation(identifiedbyoperationid):
2access("E"."EMPLOYEE_ID"="X"."EMPLOYEE_ID")
9access("E1"."HIRE_DATE"<="E2"."HIRE_DATE")
filter("E1"."HIRE_DATE"<="E2"."HIRE_DATE")24rowsselected.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 18
Performance Tuning: How to write correctly
Make it Simple !!
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 19
Performance Tuning: How to write correctly
SQL>selectemployee_id,first_name,job_id,hire_date,salary,rank()over(orderby
hire_date)ashire_seq
2fromemployees
3orderbysalary;
EMPLOYEE_IDFIRST_NAMEJOB_IDHIRE_DATESALARYHIRE_SEQ
132TJST_CLERK10APR07210085
136HazelST_CLERK06FEB082200102
128StevenST_CLERK08MAR082200104
127JamesST_CLERK14JAN07240078
135KiST_CLERK12DEC07240095
131JamesST_CLERK16FEB05250028
140JoshuaST_CLERK06APR06250065
144PeterST_CLERK09JUL06250071
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 20
Performance Tuning: How to write correctly
SQL>select*fromtable(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Planhashvalue:1701542182
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
|0|SELECTSTATEMENT||107|3424|5(40)|00:00:01|
|1|SORTORDERBY||107|3424|5(40)|00:00:01|
|2|WINDOWSORT||107|3424|5(40)|00:00:01|
|3|TABLEACCESSFULL|EMPLOYEES|107|3424|3(0)|00:00:01|
10rowsselected.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 21
Performance Tuning: How to write correctly
When Should I Use an Index?
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 22
Performance Tuning: How to write correctly
When Should I Use an Index?
No es un puntero
Segmento con almacenamiento
en un tablespace
Parmetros de concurrencia
Mejoran el acceso a los datos
Degrada gradualmente los
procesos de Update, Insert y
Delete de tuplas.
No son libres de mantenimiento
+20% entradas invlidas, el
motor omite el uso del ndice.
Requieren cada cierto tiempo
recrearse para balancear el rbol
y eliminar las entras invlidas
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 23
Performance Tuning: How to write correctly
When Should I Use an Index?
Optimizacin basada en RULE ( Regla ) 10gR2 o inferior
a a a a a b b b b
b b b b a a a a
c c c c c c
d d d
e ...
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 24
Performance Tuning: How to write correctly
When Should I Use an Index?
Optimizacin basada en COST (Costo ) 7.x o superior,
11g y superior slo optimiza basado en COSTO
a
b
c Un columna que es llave fornea
d Siempre debe estar indexada.
e
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 25
Performance Tuning: How to write correctly
Driving Table, ( RBO Only )
lt as
nsu
o
d ec
- 5%
c ra das
lu
+ invo
b la so
3 ta
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 26
Performance Tuning: How to write correctly
Parseo de instrucciones
La fase de anlisis sintctico para declaraciones se puede disminuir mediante
el uso eficiente de alias. Si un alias no est presente, el motor debe resolver
qu tablas poseen las columnas especificadas. El siguiente es un ejemplo.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 27
Performance Tuning: How to write correctly
Exists vs IN
Para la consulta A, todas las filas de TABLA2 sern
ledos por cada fila en la Tabla 1. El efecto ser
1.000.000 filas ledas de artculos.
En el caso de la consulta B, se leer un mximo de 1
registro de la TABLA2 para cada fila en la TABLA1,
reduciendo as la carga de procesamiento de la
declaracin
Si la mayora de los criterios de filtrado se encuentran en la
subconsulta entonces la variacin IN es ms eficiente.
Si la mayora de los criterios de filtrado estn en la consulta
entonces la variacin de EXISTS es ms eficiente.
Se sugiere que usted debe tratar ambas variantes y ver
cul funciona mejor.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 28
Performance Tuning: How to write correctly
Not using bind variable
SQL>host
[oracle@LAB1~]$viscript1.sql
[oracle@LAB1~]$morescript1.sql
es
ec
settimingon
begin
v
foriin1..100000
K
loop
0
executeimmediate
10
'insertintot(x,y)
values('||i||',''x'')';
g
endloop;
i n
end;
s
/
r
[oracle@LAB1~]$
Pa
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 29
Performance Tuning: How to write correctly
Using Bind variable
! ! !
[oracle@LAB1~]$morescript2.sql
settimingon
1 X
g
begin
in
foriin1..100000
loop
s
executeimmediate
'insertintot(x,y)
values(:i,''x'')'
a r
P
usingi;
endloop;
end;
/
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 30
Performance Tuning: How to run correctly
PLSQL_CODE_TYPE
PLSQL_CODE_TYPE specifies the compilation mode for
PL/SQL library units.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 31
Performance Tuning: How to run correctly
PLSQL_CODE_TYPE
When the value of this parameter is changed, it has no effect on
PL/SQL library units that have already been compiled. The value of
this parameter is stored persistently with each library unit.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 32
Performance Tuning: How to run correctly
Environment
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 33
Performance Tuning: How to run correctly
Example
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 34
Performance Tuning: How to run correctly
Example
set serveroutput on
set timing on
--- anonymous block
declare
x number;
begin
x := testeo(40);
dbms_output.put_line(x);
end;
/
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 35
Performance Tuning: How to run correctly
Example
[oracle@lab1 admin]$ sqlplus hr/hr@PDB1
SQL*Plus: Release 12.1.0.1.0 Production on Wed Apr 23 17:23:43 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Last Successful login time: Wed Jan 15 2014 12:14:36 -06:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit
Production
With the Partitioning, OLAP, Advanced Analytics and Real Application
Testing options
SQL>
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 36
Performance Tuning: How to run correctly
Example
SQL> @p1.pl
Function created.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 37
Performance Tuning: How to run correctly
Example
SQL> set linesize 200
SQL> @ver_parametro.pl
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 38
Performance Tuning: How to run correctly
Example
SQL> @ej1.pl
102334155
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 39
Performance Tuning: How to run correctly
Example ( Do not forget: COMPILE )
SQL> @ej1.pl R/ 102334155 SQL> @ej1.pl R/ 102334155
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 40
Performance Tuning: How to run correctly
Example 2 ( Compiled=NATIVE )
create or replace procedure insertar
as
begin SQL> execute insertar;
for i in 1 .. 100000 PL/SQL procedure successfully completed
Elapsed: 00:01:12.61
loop
execute immediate
'insert into t(x,y)
values (' || i ||',''x'')';
end loop;
end;
/
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 41
Performance Tuning: How to run correctly
Example 2, ( Compiled =NATIVE )
create or replace procedure insertarb
as
SQL> execute insertarb;
begin
PL/SQL procedure successfully completed
for i in 1 .. 100000
Elapsed: 00:00:05.05
loop
execute immediate
'insert into t(x,y)
values (:i,''x'')'
using i;
end loop;
end;
/
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 42
Performance Tuning: How to run correctly
Result_Cache
The Result Cache is a new memory structure in the SGA.
By adding the RESULT_CACHE clause to your function, Oracle will:
Cache the values returned by the function, along with the input
values.
Return the cached values if the same input values are provided
that is, not execute the function.
Share this cache among all sessions in the instance.
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 43
Performance Tuning: How to run correctly
Result_Cache
SQL> select count(*) from hr.employees2;
COUNT(*)
----------
10700214
Elapsed: 00:00:00.46
SQL> host
[oracle@lab1 ~]$ more cache2_result.sql
SELECT count(*)
FROM ( SELECT department_id, manager_id, count(*) count
FROM hr.employees
GROUP BY department_id, manager_id ) view1
WHERE department_id <> 30;
System altered.
Elapsed: 00:00:00.03
COUNT(*)
----------
25
Elapsed: 00:00:02.11
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 45
Performance Tuning: How to run correctly
Result_Cache
SQL> host
[oracle@lab1 ~]$ more cache2_result.sql
SQL> @cache2_result
For Oracle employees and authorized partners only. Do not distribute to third parties.
Function created.
2012 Oracle Corporation Proprietary and Confidential 46
Performance Tuning: How to run correctly
Result_Cache
SQL> select f1 from dual;
F1
----------
25
Elapsed: 00:00:03.45
F1
----------
25
Elapsed: 00:00:00.00
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 48
Performance Tuning: How to run correctly
Result_Cache
SQL> connect hr/hr@pdb1
Connected.
SQL> select f1 from dual;
F1
----------
25
Elapsed: 00:00:00.01
FOR item IN (
SELECT DISTINCT(SQRT(department_id)) col_alias
FROM empleados
ORDER BY col_alias
)
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 50
Performance Tuning: How to run correctly
Square root of dept. ID METHOD DISTINCT(SQRT)= 10
Square root of dept. ID METHOD DISTINCT(SQRT)=
10.48808848170151546991453513679937598475
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 51
Performance Tuning: How to run correctly
TIP: Hacer magia, mantener estadsticas actualizadas
EXEC DBMS_STATS.gather_table_stats('HR',
'EMPLOYEES');
EXEC DBMS_STATS.gather_index_stats(HR',
'EMPLOYEES_PK');
EXEC DBMS_STATS.gather_dictionary_stats;
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 52
Performance Tuning: How to run correctly
Concurrencia: Evitando bloqueos y aumentando la escalabilidad
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 53
@rovaque
oracledbacr.blogspot.com
rvargas@cruxconsultores.com
ronald.vargas.quesada@gmail.com
For Oracle employees and authorized partners only. Do not distribute to third parties.
2012 Oracle Corporation Proprietary and Confidential 54