Performance Tuning: How to write correctly
Ronald Vargas Quesada, Oracle ACE
Expertise Database Management & Performance
T...
Performance Tuning: How to write correctly
<function> ( <arg>,<arg>,..)
OVER (
<partition clause>
<sorting clause>
<window...
Performance Tuning: How to write correctly
<function> ( <arg>,<arg>,..)
OVER | KEEP | WITHING GROUP (
<partition clause>
<...
Performance Tuning: How to write correctly

That´s it !
That´s it friends!

For Oracle employees and authorized partners o...
Performance Tuning: How to write correctly

quick example #1

For Oracle employees and authorized partners only. Do not di...
employees by salary

For Oracle employees and authorized partners only. Do not distribute to third parties.
© 2012 Oracle ...
Performance Tuning: How to write correctly
SQL> select employee_id, job_id, hire_date, salary
2 from employees
3 order by ...
Performance Tuning: How to write correctly

Who hired first ?
hiring sequence

For Oracle employees and authorized partner...
Performance Tuning: How to write correctly
1

2
3
4
5
6
7
8*
SQL>

select e.employee_id, e.first_name, e.job_id, e.hire_da...
Performance Tuning: How to write correctly

Is very simple ?

For Oracle employees and authorized partners only. Do not di...
Performance Tuning: How to write correctly

Much or little work ?

For Oracle employees and authorized partners only. Do n...
Performance Tuning: How to write correctly
PLAN_TABLE_OUTPUT
-------------------------------------------------------------...
Performance Tuning: How to write correctly

Make it Simple !!

For Oracle employees and authorized partners only. Do not d...
Performance Tuning: How to write correctly
SQL> select employee_id, first_name, job_id, hire_date, salary, rank() over (or...
Performance Tuning: How to write correctly
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------...
Performance Tuning: How to write correctly
Not using bind variable
SQL> host
[oracle@LAB1 ~]$ vi script1.sql
[oracle@LAB1 ...
Performance Tuning: How to write correctly
Using Bind variable
[oracle@LAB1 ~]$ more script2.sql
set timing on
begin
for i...
@rovaque
oracledbacr.blogspot.com
ronald.vargas.quesada@gmail.com

For Oracle employees and authorized partners only. Do n...
Upcoming SlideShare
Loading in...5
×

Performance tuning how to write correctly sql statement

561

Published on

Estén pendientes de la disponibilidad de este Webinar gratis en la red.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
561
On Slideshare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Performance tuning how to write correctly sql statement"

  1. 1. Performance Tuning: How to write correctly Ronald Vargas Quesada, Oracle ACE Expertise Database Management & Performance Technical PreSales Consultant, Netsoft de Centroamérica For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 1
  2. 2. Performance Tuning: How to write correctly <function> ( <arg>,<arg>,..) OVER ( <partition clause> <sorting clause> <windowing clause> ) For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 2
  3. 3. Performance Tuning: How to write correctly <function> ( <arg>,<arg>,..) OVER | KEEP | WITHING GROUP ( <partition clause> <sorting clause> <windowing clause> ) For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 3
  4. 4. Performance Tuning: How to write correctly That´s it ! That´s it friends! For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 4
  5. 5. 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 5
  6. 6. employees by salary For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 6
  7. 7. Performance Tuning: How to write correctly SQL> select employee_id, job_id, hire_date, salary 2 from employees 3 order by salary; EMPLOYEE_ID ----------132 136 128 127 135 191 119 140 144 182 131 JOB_ID ---------ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK SH_CLERK PU_CLERK ST_CLERK ST_CLERK SH_CLERK ST_CLERK HIRE_DATE SALARY --------- ---------10-APR-07 2100 06-FEB-08 2200 08-MAR-08 2200 14-JAN-07 2400 12-DEC-07 2400 19-DEC-07 2500 10-AUG-07 2500 06-APR-06 2500 09-JUL-06 2500 21-JUN-07 2500 16-FEB-05 2500 For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 7
  8. 8. Performance Tuning: How to write correctly Who hired first ? hiring sequence For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 8
  9. 9. Performance Tuning: How to write correctly 1 2 3 4 5 6 7 8* SQL> select e.employee_id, e.first_name, e.job_id, e.hire_date, e.salary, x.sequence from employees e, (select e2.employee_id, count(*) sequence from employees e1, employees e2 where e1.hire_date <= e2.hire_date group by e2.employee_id ) x where e.employee_id = x.employee_id order by salary / EMPLOYEE_ID ----------132 128 136 135 127 144 very complicated !! FIRST_NAME -------------------TJ Steven Hazel Ki James Peter JOB_ID ---------ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK HIRE_DATE SALARY SEQUENCE --------- ---------- ---------10-APR-07 2100 85 08-MAR-08 2200 104 06-FEB-08 2200 102 12-DEC-07 2400 95 14-JAN-07 2400 78 09-JUL-06 2500 71 For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 9
  10. 10. 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 10
  11. 11. Performance Tuning: How to write correctly Much or little work ? For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 11
  12. 12. Performance Tuning: How to write correctly PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------Plan hash value: 2074273239 ------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 107 | 5243 | 14 (36)| 00:00:01 | | 1 | SORT ORDER BY | | 107 | 5243 | 14 (36)| 00:00:01 | |* 2 | HASH JOIN | | 107 | 5243 | 13 (31)| 00:00:01 | | 3 | TABLE ACCESS FULL | EMPLOYEES | 107 | 3424 | 3 (0)| 00:00:01 | | 4 | VIEW | | 107 | 1819 | 9 (34)| 00:00:01 | | 5 | HASH GROUP BY | | 107 | 2140 | 9 (34)| 00:00:01 | | 6 | MERGE JOIN | | 5783 | 112K| 8 (25)| 00:00:01 | | 7 | SORT JOIN | | 107 | 856 | 4 (25)| 00:00:01 | | 8 | TABLE ACCESS FULL| EMPLOYEES | 107 | 856 | 3 (0)| 00:00:01 | |* 9 | SORT JOIN | | 107 | 1284 | 4 (25)| 00:00:01 | | 10 | TABLE ACCESS FULL| EMPLOYEES | 107 | 1284 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------2 - access("E"."EMPLOYEE_ID"="X"."EMPLOYEE_ID") 9 - access("E1"."HIRE_DATE"<="E2"."HIRE_DATE") filter("E1"."HIRE_DATE"<="E2"."HIRE_DATE") 24 rows selected. For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 12
  13. 13. 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 13
  14. 14. Performance Tuning: How to write correctly SQL> select employee_id, first_name, job_id, hire_date, salary, rank() over (order by hire_date) as hire_seq 2 from employees 3 order by salary; EMPLOYEE_ID ----------132 136 128 127 135 131 140 144 FIRST_NAME -------------------TJ Hazel Steven James Ki James Joshua Peter JOB_ID ---------ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK ST_CLERK HIRE_DATE SALARY HIRE_SEQ --------- ---------- ---------10-APR-07 2100 85 06-FEB-08 2200 102 08-MAR-08 2200 104 14-JAN-07 2400 78 12-DEC-07 2400 95 16-FEB-05 2500 28 06-APR-06 2500 65 09-JUL-06 2500 71 For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 14
  15. 15. Performance Tuning: How to write correctly SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------Plan hash value: 1701542182 --------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 107 | 3424 | 5 (40)| 00:00:01 | | 1 | SORT ORDER BY | | 107 | 3424 | 5 (40)| 00:00:01 | | 2 | WINDOW SORT | | 107 | 3424 | 5 (40)| 00:00:01 | | 3 | TABLE ACCESS FULL| EMPLOYEES | 107 | 3424 | 3 (0)| 00:00:01 | --------------------------------------------------------------------------------10 rows selected. For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 15
  16. 16. Performance Tuning: How to write correctly Not using bind variable SQL> host [oracle@LAB1 ~]$ vi script1.sql [oracle@LAB1 ~]$ more script1.sql set timing on begin for i in 1 .. 100000 loop execute immediate 'insert into t (x,y) values ( ' || i || ', ''x'') '; end loop; end; / [oracle@LAB1 ~]$ For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 16
  17. 17. Performance Tuning: How to write correctly Using Bind variable [oracle@LAB1 ~]$ more script2.sql set timing on begin for i in 1 .. 100000 loop execute immediate 'insert into t (x,y) values ( :i, ''x'')' using i; end loop; end; / [oracle@LAB1 ~]$ For Oracle employees and authorized partners only. Do not distribute to third parties. © 2012 Oracle Corporation – Proprietary and Confidential 17
  18. 18. @rovaque oracledbacr.blogspot.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 18
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×