Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

zen and the art of SQL optimization

5,448 views

Published on

What do zen practice and Oracle SQL optimization have in common? You’d be surprised! This presentation demonstrates how the zen principles of simplicity, focus, and practice can be applied to your technical skills to make you a calm and fearless SQL optimizer. You will be “enlightened” as to how to find ease with your day-to-day SQL tuning activities.

Published in: Technology, Business

zen and the art of SQL optimization

  1. zen and the art of SQL optimization Karen Morton
  2.  
  3.  
  4. Simplicity
  5. “ Make everything as simple as possible but no simpler. ” <ul><li>Albert Einstein </li></ul>
  6.  
  7.  
  8. “ Normally, we do not so much look at things as overlook them.” <ul><li>Alan Watts </li></ul>
  9. simple vs simple-minded
  10. Oracle 9.2.0.6 SELECT to_char( to_date(:B1,‘DD-MON-YYYY’)+1, ‘DD-MON-YYYY’ ) FROM dual;
  11. What about its effect?
  12. 32.9% % of total R
  13. 1,087,770 # of executions
  14. 3,263,033 # of LIOs
  15. v_date_str := to_char( to_date( :B1,‘DD-MON-YYYY’ ) + 1, ‘ DD-MON-YYYY’ ) ;
  16. Focus
  17. ETCIBMVIPNASAFBI
  18. ETC IBM VIP NASA FBI
  19. 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’;
  20. SELECT rowid, load_no, load_status, manifest_no, manifest_date FROM cs_loads WHERE load_no = :B1 ;
  21. Keys to Optimizing SQL
  22. Tune the question, not the query.
  23. What output should the query provide?
  24. What special conditions need to be handled?
  25. “ If you don’t know where you are going, you’ll end up somewhere else.” <ul><li>Yogi Berra </li></ul>
  26. Reduce. Reuse.
  27. Decrease row source sizes quickly
  28. Access objects once and reuse the result set
  29. “ Not enough gets said about the importance of abandoning crap.” - Ira Glass
  30. Practice.
  31. Never stop learning
  32. Stay connected
  33. Think in sets
  34. Test Test Test (then test again)
  35.  
  36. “ Practice, practice, practice. Until it becomes your practice.” <ul><li>Unknown </li></ul>
  37. Going Zen
  38. <ul><li>1 </li></ul>
  39. <ul><li>SELECT invoice_id, distribution_line_number </li></ul><ul><li>FROM ap.ap_invoice_distributions_all ap2 </li></ul><ul><li>WHERE ap2.project_id > 0 </li></ul><ul><li>AND ap2.pa_addition_flag = 'T' </li></ul><ul><li>MINUS </li></ul><ul><li>SELECT invoice_id, distribution_line_number </li></ul><ul><li>FROM ap.ap_invoice_distributions_all ap, </li></ul><ul><li>pa.pa_cost_distribution_lines_all pa </li></ul><ul><li>WHERE ap.project_id > 0 </li></ul><ul><li>AND ap.pa_addition_flag = 'T' </li></ul><ul><li>AND pa.system_reference2 IS NOT NULL </li></ul><ul><li>AND TO_NUMBER(pa.system_reference2) = ap.invoice_id </li></ul><ul><li>AND TO_NUMBER(pa.system_reference3) = ap.distribution_line_number </li></ul><ul><li>ORDER BY invoice_id; </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:20.09 60,941 57,116 6 </li></ul>
  40. <ul><li>SELECT invoice_id, distribution_line_number </li></ul><ul><li>FROM (SELECT invoice_id, distribution_line_number, </li></ul><ul><li>system_reference2, system_reference3 </li></ul><ul><li>FROM ap.ap_invoice_distributions_all ap, </li></ul><ul><li>(SELECT TO_NUMBER(system_reference2) system_reference2, </li></ul><ul><li>TO_NUMBER(system_reference3) system_reference3 </li></ul><ul><li>FROM pa.pa_cost_distribution_lines_all </li></ul><ul><li>WHERE system_reference2 IS NOT NULL) </li></ul><ul><li>WHERE system_reference2 (+) = ap.invoice_id </li></ul><ul><li>AND system_reference3 (+) = ap.distribution_line_number </li></ul><ul><li>AND ap.project_id > 0 AND ap.pa_addition_flag = 'T' ) </li></ul><ul><li>WHERE system_reference2 IS NULL AND system_reference3 IS NULL </li></ul><ul><li>ORDER BY invoice_id; </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:03.09 1,824 453 6 </li></ul>
  41. <ul><li>2 </li></ul>
  42. <ul><li>SELECT t.* </li></ul><ul><li>FROM RMS.tasks t, RMS.status s </li></ul><ul><li>WHERE t.status_id = s.id </li></ul><ul><li>AND (EXISTS ( SELECT null FROM RMS.tasks ta, RMS.resources r </li></ul><ul><li> WHERE t.id = ta.parent_task_id AND ta.project_code = :p </li></ul><ul><li> AND ta.task_type_code = 'A' AND ta.resource_id = r.id </li></ul><ul><li> AND r.reporting_id = :r ) </li></ul><ul><li> OR (t.project_code = :p AND t.task_type_code = 'T') ); </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:09.50 287,330 992 3 </li></ul>
  43. <ul><li>SELECT t.* FROM RMS.tasks t, RMS.status s </li></ul><ul><li>WHERE t.status_id = s.id AND EXISTS </li></ul><ul><li> (SELECT null FROM RMS.tasks ta,RMS.resources r </li></ul><ul><li> WHERE t.id = ta.parent_task_id AND ta.project_code = :p </li></ul><ul><li>AND ta.task_type_code = 'A' AND ta.resource_id = r.id </li></ul><ul><li>AND r.reporting_id = :r ) </li></ul><ul><li>UNION </li></ul><ul><li>SELECT t.* FROM RMS.tasks t, RMS.status s </li></ul><ul><li>WHERE t.status_id = s.id AND t.id IN </li></ul><ul><li>(SELECT t.parent_task_id FROM RMS.tasks t, RMS.status s </li></ul><ul><li> WHERE t.status_id = s.id AND </li></ul><ul><li>EXISTS (SELECT null FROM RMS.tasks ta, RMS.resources r </li></ul><ul><li> WHERE t.id = ta.parent_task_id AND ta.project_code = :p </li></ul><ul><li> AND ta.task_type_code = 'A' AND ta.resource_id = r.id </li></ul><ul><li> AND r.reporting_id = :r ); </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:00.01 128 0 3 </li></ul>
  44. <ul><li>3 </li></ul>
  45. <ul><li>SELECT J.emplid, J.empl_rcd, J.effdt, J.effseq </li></ul><ul><li>FROM PS_JOB J </li></ul><ul><li>WHERE J.effdt = (SELECT MAX(effdt) FROM PS_JOB </li></ul><ul><li> WHERE emplid = J.emplid </li></ul><ul><li> AND empl_rcd = J.empl_rcd </li></ul><ul><li> AND effdt <= SYSDATE ) </li></ul><ul><li>AND J. effseq = (SELECT MAX(effseq) FROM PS_JOB </li></ul><ul><li> WHERE emplid = J.emplid </li></ul><ul><li> AND empl_rcd = J.empl_rcd </li></ul><ul><li> AND effdt = J.effdt ) ; </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:08.04 337,908 0 30,107 </li></ul>
  46. <ul><li>SELECT J.emplid, J.empl_rcd, J.effdt, J.effseq </li></ul><ul><li>FROM </li></ul><ul><li>(SELECT J.emplid, J.empl_rcd, J.effdt, J.effseq, MAX(J.effdt) </li></ul><ul><li> OVER (PARTITION BY J.emplid, J.empl_rcd ) max_effdt, </li></ul><ul><li>LAST_VALUE(J.effseq) OVER (PARTITION BY J.emplid, J.empl_rcd </li></ul><ul><li>ORDER BY J.efdt, J.effseq ROWS BETWEEN CURRENT ROW AND </li></ul><ul><li> UNBOUNDED FOLLOWING ) max_effseq </li></ul><ul><li> FROM PS_JOB J </li></ul><ul><li>WHERE J.effdt <= SYSDATE ) J </li></ul><ul><li>WHERE J.effdt = J.max_effdt </li></ul><ul><li>AND J.effseq = J.max_effseq; </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:00.44 1,009 0 30,107 </li></ul>
  47. <ul><li>4 </li></ul>
  48. <ul><li>SELECT utrsotp_desc </li></ul><ul><li>FROM utrsotp, ucbsvco </li></ul><ul><li>WHERE ucbsvco_sotp_code = utrsotp_code </li></ul><ul><li>AND ucbsvco_dispatch_date = </li></ul><ul><li>(SELECT MAX(ucbsvco_dispatch_date) </li></ul><ul><li>FROM ucbsvco </li></ul><ul><li>WHERE ucbsvco_cust_code = 1320908 </li></ul><ul><li>AND ucbsvco_prem_code = '507601' </li></ul><ul><li>AND ucbsvco_stus_code = 'C' </li></ul><ul><li>AND ucbsvco_dispatch_ind IS NOT NULL) </li></ul><ul><li>AND ucbsvco_stus_code = 'C' </li></ul><ul><li>AND ucbsvco_dispatch_ind IS NOT NULL </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:20.16 106,693 0 1 </li></ul>
  49. <ul><li>SELECT utrsotp_desc </li></ul><ul><li>FROM utrsotp, </li></ul><ul><li>(SELECT ucbsvco_sotp_code, ucbsvco_dispatch_date, </li></ul><ul><li>MAX(ucbsvco_dispatch_date) OVER(PARTITION BY </li></ul><ul><li> ucbsvco_cust_code, ucbsvco_prem_code) max_dispatch_dt </li></ul><ul><li>FROM ucbsvco </li></ul><ul><li>WHERE ucbsvco_cust_code = 1320908 </li></ul><ul><li>AND ucbsvco_prem_code = '507601' </li></ul><ul><li>AND ucbsvco_stus_code = 'C' </li></ul><ul><li>AND ucbsvco_dispatch_ind IS NOT NULL) svco </li></ul><ul><li>WHERE svco.ucbsvco_dispatch_date = svco.max_dispatch_dt </li></ul><ul><li>AND svco.ucbsvco_sotp_code = utrsotp_code </li></ul><ul><li>Elapsed Time LIO Blocks PIO Blocks Total Rows </li></ul><ul><li>------------ ---------- ---------- ---------- </li></ul><ul><li> 00:00:00.18 15 0 1 </li></ul>
  50.  
  51. Simplicity is powerful…
  52. … but it is neither simple nor easy to achieve.
  53. It is obtained through the careful reduction of the nonessential.
  54. While simplicity is the goal…
  55. … it is possible to be “too simple.”
  56. Your job is to find the balance most appropriate to your situation.
  57. Thank You
  58. http://www.method-r.com http://karenmorton.blogspot.com [email_address]

×