Download - zen and the art of SQL optimization

Transcript
Page 1: zen and the art of SQL optimization

zen and the art ofSQL optimization

Karen Morton

Page 2: zen and the art of SQL optimization
Page 3: zen and the art of SQL optimization
Page 4: zen and the art of SQL optimization

Simplicity

Page 5: zen and the art of SQL optimization

“Make everything as simple as possible but no simpler.

”— Albert Einstein

Page 6: zen and the art of SQL optimization
Page 7: zen and the art of SQL optimization
Page 8: zen and the art of SQL optimization

— Alan Watts

“Normally, we do not so much look at things as overlook them.”

Page 9: zen and the art of SQL optimization

simplevs

simple-minded

Page 10: zen and the art of SQL optimization

Oracle 9.2.0.6

SELECT to_char( to_date(:B1,‘DD-MON-YYYY’)+1,

‘DD-MON-YYYY’)

FROM dual;

Page 11: zen and the art of SQL optimization

What about its effect?

Page 12: zen and the art of SQL optimization

32.9%% of total R

Page 13: zen and the art of SQL optimization

1,087,770# of executions

Page 14: zen and the art of SQL optimization

3,263,033# of LIOs

Page 15: zen and the art of SQL optimization

v_date_str := to_char(

to_date( :B1,‘DD-MON-YYYY’ ) + 1, ‘DD-MON-YYYY’ ) ;

Page 16: zen and the art of SQL optimization

Focus

Page 17: zen and the art of SQL optimization

ETCIBMVIPNASAFBI

Page 18: zen and the art of SQL optimization

ETCIBMVIPNASAFBI

Page 19: zen and the art of SQL optimization

SELECT rowid, load_no, load_status,manifest_no, manifest_date

FROM cs_loads WHERE load_no = 552888 ;

VARCHAR2(15)

SELECT rowid, load_no, load_status,manifest_no, manifest_date

FROM cs_loads WHERE load_no = 552888 ;

datatype mismatch causes FULL SCAN

SELECT rowid, load_no, load_status,manifest_no, manifest_date

FROM cs_loads WHERE load_no =‘552888’;

Page 20: zen and the art of SQL optimization

SELECT rowid, load_no, load_status,manifest_no, manifest_date

FROM cs_loads WHERE load_no = :B1 ;

Page 21: zen and the art of SQL optimization

Keys to Optimizing SQL

Page 22: zen and the art of SQL optimization

Tune the question,not the query.

Page 23: zen and the art of SQL optimization

What output shouldthe query provide?

Page 24: zen and the art of SQL optimization

What special conditionsneed to be handled?

Page 25: zen and the art of SQL optimization

“If you don’t know where you are going, you’ll end up somewhere else.” — Yogi Berra

Page 26: zen and the art of SQL optimization

Reduce.Reuse.

Page 27: zen and the art of SQL optimization

Decrease rowsource sizes quickly

Page 28: zen and the art of SQL optimization

Access objects onceand reuse the result set

Page 29: zen and the art of SQL optimization

“Not enough gets said

about the importance

of abandoning crap.” - Ira Glass

Page 30: zen and the art of SQL optimization

Practice.

Page 31: zen and the art of SQL optimization

Never stop learning

Page 32: zen and the art of SQL optimization

Stay connected

Page 33: zen and the art of SQL optimization

Think in sets

Page 34: zen and the art of SQL optimization

TestTestTest

(then test again)

Page 35: zen and the art of SQL optimization
Page 36: zen and the art of SQL optimization

“Practice, practice, practice.Until it becomes your practice.”

— Unknown

Page 37: zen and the art of SQL optimization

GoingZen

Page 38: zen and the art of SQL optimization

1

Page 39: zen and the art of SQL optimization

SELECT invoice_id, distribution_line_number FROM ap.ap_invoice_distributions_all ap2 WHERE ap2.project_id > 0 AND ap2.pa_addition_flag = 'T' MINUS SELECT invoice_id, distribution_line_number FROM ap.ap_invoice_distributions_all ap, pa.pa_cost_distribution_lines_all pa WHERE ap.project_id > 0 AND ap.pa_addition_flag = 'T' AND pa.system_reference2 IS NOT NULL AND TO_NUMBER(pa.system_reference2) = ap.invoice_id AND TO_NUMBER(pa.system_reference3) = ap.distribution_line_number ORDER BY invoice_id;

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:20.09 60,941 57,116 6

Page 40: zen and the art of SQL optimization

SELECT invoice_id, distribution_line_number FROM (SELECT invoice_id, distribution_line_number, system_reference2, system_reference3 FROM ap.ap_invoice_distributions_all ap, (SELECT TO_NUMBER(system_reference2)

system_reference2, TO_NUMBER(system_reference3) system_reference3 FROM pa.pa_cost_distribution_lines_all WHERE system_reference2 IS NOT NULL) WHERE system_reference2 (+) = ap.invoice_id AND system_reference3 (+) = ap.distribution_line_number AND ap.project_id > 0 AND ap.pa_addition_flag = 'T' ) WHERE system_reference2 IS NULL AND system_reference3 IS NULL ORDER BY invoice_id;

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:03.09 1,824 453 6

Page 41: zen and the art of SQL optimization

2

Page 42: zen and the art of SQL optimization

SELECT t.* FROM RMS.tasks t, RMS.status s WHERE t.status_id = s.id AND (EXISTS ( SELECT null FROM RMS.tasks ta, RMS.resources r

WHERE t.id = ta.parent_task_id AND ta.project_code = :p AND ta.task_type_code = 'A' AND ta.resource_id = r.id AND r.reporting_id = :r )

OR (t.project_code = :p AND t.task_type_code = 'T') );

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:09.50 287,330 992 3

Page 43: zen and the art of SQL optimization

SELECT t.* FROM RMS.tasks t, RMS.status s WHERE t.status_id = s.id AND EXISTS

(SELECT null FROM RMS.tasks ta,RMS.resources r WHERE t.id = ta.parent_task_id AND ta.project_code = :p

AND ta.task_type_code = 'A' AND ta.resource_id = r.id AND r.reporting_id = :r )

UNIONSELECT t.* FROM RMS.tasks t, RMS.status s WHERE t.status_id = s.id AND t.id IN

(SELECT t.parent_task_id FROM RMS.tasks t, RMS.status s WHERE t.status_id = s.id AND

EXISTS (SELECT null FROM RMS.tasks ta, RMS.resources r WHERE t.id = ta.parent_task_id AND ta.project_code = :p AND ta.task_type_code = 'A' AND ta.resource_id = r.id AND r.reporting_id = :r );

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:00.01 128 0 3

Page 44: zen and the art of SQL optimization

3

Page 45: zen and the art of SQL optimization

SELECT J.emplid, J.empl_rcd, J.effdt, J.effseq FROM PS_JOB J WHERE J.effdt = (SELECT MAX(effdt) FROM PS_JOB WHERE emplid = J.emplid AND empl_rcd = J.empl_rcd AND effdt <= SYSDATE ) AND J. effseq = (SELECT MAX(effseq) FROM PS_JOB WHERE emplid = J.emplid AND empl_rcd = J.empl_rcd AND effdt = J.effdt ) ;

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:08.04 337,908 0 30,107

Page 46: zen and the art of SQL optimization

SELECT J.emplid, J.empl_rcd, J.effdt, J.effseq FROM (SELECT J.emplid, J.empl_rcd, J.effdt, J.effseq, MAX(J.effdt)

OVER (PARTITION BY J.emplid, J.empl_rcd ) max_effdt, LAST_VALUE(J.effseq) OVER (PARTITION BY J.emplid,

J.empl_rcd ORDER BY J.efdt, J.effseq ROWS BETWEEN CURRENT ROW AND

UNBOUNDED FOLLOWING ) max_effseq FROM PS_JOB J WHERE J.effdt <= SYSDATE ) J

WHERE J.effdt = J.max_effdt AND J.effseq = J.max_effseq;

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:00.44 1,009 0 30,107

Page 47: zen and the art of SQL optimization

4

Page 48: zen and the art of SQL optimization

SELECT utrsotp_desc FROM utrsotp, ucbsvco WHERE ucbsvco_sotp_code = utrsotp_code AND ucbsvco_dispatch_date = (SELECT MAX(ucbsvco_dispatch_date) FROM ucbsvco WHERE ucbsvco_cust_code = 1320908 AND ucbsvco_prem_code = '507601' AND ucbsvco_stus_code = 'C' AND ucbsvco_dispatch_ind IS NOT NULL) AND ucbsvco_stus_code = 'C' AND ucbsvco_dispatch_ind IS NOT NULL

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:20.16 106,693 0 1

Page 49: zen and the art of SQL optimization

SELECT utrsotp_desc FROM utrsotp, (SELECT ucbsvco_sotp_code, ucbsvco_dispatch_date, MAX(ucbsvco_dispatch_date) OVER(PARTITION BY

ucbsvco_cust_code, ucbsvco_prem_code) max_dispatch_dt

FROM ucbsvco WHERE ucbsvco_cust_code = 1320908 AND ucbsvco_prem_code = '507601' AND ucbsvco_stus_code = 'C' AND ucbsvco_dispatch_ind IS NOT NULL) svco WHERE svco.ucbsvco_dispatch_date = svco.max_dispatch_dt AND svco.ucbsvco_sotp_code = utrsotp_code

Elapsed Time LIO Blocks PIO Blocks Total Rows------------ ---------- ---------- ---------- 00:00:00.18 15 0 1

Page 50: zen and the art of SQL optimization
Page 51: zen and the art of SQL optimization

Simplicity is powerful…

Page 52: zen and the art of SQL optimization

…but it is neithersimple nor easy to

achieve.

Page 53: zen and the art of SQL optimization

It is obtained through the careful

reduction of the nonessential.

Page 54: zen and the art of SQL optimization

While simplicityis the goal…

Page 55: zen and the art of SQL optimization

…it is possible tobe “too simple.”

Page 56: zen and the art of SQL optimization

Your job is to findthe balance mostappropriate toyour situation.

Page 57: zen and the art of SQL optimization

Thank You

Page 58: zen and the art of SQL optimization

http://www.method-r.com

http://karenmorton.blogspot.com

[email protected]