Download - zen and the art of SQL optimization
![Page 1: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/1.jpg)
zen and the art ofSQL optimization
Karen Morton
![Page 2: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/2.jpg)
![Page 3: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/3.jpg)
![Page 4: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/4.jpg)
Simplicity
![Page 5: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/5.jpg)
“Make everything as simple as possible but no simpler.
”— Albert Einstein
![Page 6: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/6.jpg)
![Page 7: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/7.jpg)
![Page 8: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/8.jpg)
— Alan Watts
“Normally, we do not so much look at things as overlook them.”
![Page 9: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/9.jpg)
simplevs
simple-minded
![Page 10: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/11.jpg)
What about its effect?
![Page 12: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/12.jpg)
32.9%% of total R
![Page 13: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/13.jpg)
1,087,770# of executions
![Page 14: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/14.jpg)
3,263,033# of LIOs
![Page 15: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/15.jpg)
v_date_str := to_char(
to_date( :B1,‘DD-MON-YYYY’ ) + 1, ‘DD-MON-YYYY’ ) ;
![Page 16: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/16.jpg)
Focus
![Page 17: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/17.jpg)
ETCIBMVIPNASAFBI
![Page 18: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/18.jpg)
ETCIBMVIPNASAFBI
![Page 19: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/21.jpg)
Keys to Optimizing SQL
![Page 22: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/22.jpg)
Tune the question,not the query.
![Page 23: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/23.jpg)
What output shouldthe query provide?
![Page 24: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/24.jpg)
What special conditionsneed to be handled?
![Page 25: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/25.jpg)
“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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/26.jpg)
Reduce.Reuse.
![Page 27: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/27.jpg)
Decrease rowsource sizes quickly
![Page 28: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/28.jpg)
Access objects onceand reuse the result set
![Page 29: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/29.jpg)
“Not enough gets said
about the importance
of abandoning crap.” - Ira Glass
![Page 30: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/30.jpg)
Practice.
![Page 31: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/31.jpg)
Never stop learning
![Page 32: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/32.jpg)
Stay connected
![Page 33: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/33.jpg)
Think in sets
![Page 34: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/34.jpg)
TestTestTest
(then test again)
![Page 35: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/35.jpg)
![Page 36: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/36.jpg)
“Practice, practice, practice.Until it becomes your practice.”
— Unknown
![Page 37: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/37.jpg)
GoingZen
![Page 38: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/38.jpg)
1
![Page 39: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/39.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/40.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/41.jpg)
2
![Page 42: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/42.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/43.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/44.jpg)
3
![Page 45: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/45.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/46.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/47.jpg)
4
![Page 48: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/48.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/49.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/50.jpg)
![Page 51: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/51.jpg)
Simplicity is powerful…
![Page 52: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/52.jpg)
…but it is neithersimple nor easy to
achieve.
![Page 53: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/53.jpg)
It is obtained through the careful
reduction of the nonessential.
![Page 54: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/54.jpg)
While simplicityis the goal…
![Page 55: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/55.jpg)
…it is possible tobe “too simple.”
![Page 56: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/56.jpg)
Your job is to findthe balance mostappropriate toyour situation.
![Page 57: zen and the art of SQL optimization](https://reader033.vdocuments.site/reader033/viewer/2022061122/546f9f79af7959bd0a8b45f0/html5/thumbnails/57.jpg)
Thank You