query op miza on
play

Query Op)miza)on 1 Query op)miza)on Given an SQL query, - PowerPoint PPT Presentation

Query Op)miza)on 1 Query op)miza)on Given an SQL query, the query op)mizer tries to figure out the order of opera)ons that will make the


  1. Query ¡Op)miza)on ¡ 1 ¡

  2. Query ¡op)miza)on ¡ • Given ¡an ¡SQL ¡query, ¡the ¡query ¡op)mizer ¡tries ¡ to ¡figure ¡out ¡the ¡order ¡of ¡opera)ons ¡that ¡will ¡ make ¡the ¡query ¡run ¡the ¡fastest. ¡ • Possible ¡because ¡usually ¡there ¡is ¡more ¡than ¡ one ¡way ¡to ¡run ¡a ¡query. ¡ 2 ¡

  3. Why ¡query ¡op)miza)on? ¡ • SQL ¡is ¡declara)ve. ¡ – SQL ¡only ¡says ¡ what ¡to ¡retrieve ¡from ¡the ¡DB, ¡not ¡ the ¡details ¡of ¡ how . ¡ – Unlike ¡most ¡programming ¡languages ¡(though ¡ there ¡are ¡other ¡declara)ve ¡languages). ¡ • Good ¡query ¡op)miza)on ¡can ¡make ¡a ¡big ¡ difference. ¡ 3 ¡

  4. Example ¡ • Students(R#, ¡First, ¡Last) ¡ • Enrolled(R#, ¡CRN) ¡ • SELECT ¡First, ¡Last ¡ FROM ¡Students ¡NATURAL ¡JOIN ¡Enrolled ¡ WHERE ¡CRN=12345 ¡ • π F,L ¡( ¡σ ¡ CRN=12345 ¡(S ¡ ¡ ¡ ¡ ¡E)) ¡ ¡ ◃ ▹ 4 ¡

  5. Example ¡ • SELECT ¡First, ¡Last ¡ FROM ¡Students ¡NATURAL ¡JOIN ¡Enrolled ¡ WHERE ¡CRN=12345 ¡ Student Enrolled 5 ¡

  6. Example ¡ • SELECT ¡First, ¡Last ¡ FROM ¡Students ¡NATURAL ¡JOIN ¡Enrolled ¡ WHERE ¡CRN=12345 ¡ Canonical ¡Form ¡ Student Enrolled Student Enrolled 6 ¡

  7. Canonical ¡Form ¡ • Make ¡all ¡JOINs ¡explicit ¡with ¡WHERE ¡clauses. ¡ – S ¡NatJoin ¡T ¡== ¡S ¡Join ¡T ¡WHERE… ¡ – S ¡Join ¡T ¡ON ¡… ¡== ¡S ¡Join ¡T ¡WHERE… ¡ • Perform ¡selec)ons ¡and ¡projec)ons ¡as ¡early ¡as ¡ possible. ¡ 7 ¡

  8. 8 ¡

  9. 9 ¡

  10. Rela)onal ¡algebra ¡ • How ¡do ¡we ¡know ¡ ¡ π F,L ¡( ¡σ ¡ CRN=12345 ¡(S ¡ ¡ ¡ ¡ ¡E)) ¡ ¡ is ¡equal ¡to ¡ ¡ ¡ ¡ ¡π F,L ¡( ¡S ¡ ¡ ¡ ¡ ¡σ ¡ CRN=12345 ¡(E)) ¡ ¡ ¡? ¡ ¡ • Yay ¡172 ¡proofs! ¡ ¡ 10 ¡

  11. Example ¡ • Prove ¡ ¡ ? ( R 1 R 2 ) ( R 1 ) ( R 2 ) σ ∪ = σ ∪ σ P P P 11 ¡

  12. Back ¡to ¡query ¡op)miza)on ¡ • Projec)ons ¡and ¡selec)ons ¡ – Perform ¡them ¡early ¡(but ¡carefully) ¡to ¡reduce ¡ • number ¡of ¡tuples ¡ • size ¡of ¡tuples ¡(remove ¡ajributes) ¡ – Project ¡out ¡(remove) ¡all ¡ajributes ¡except ¡those ¡ requested ¡or ¡required ¡(e.g., ¡needed ¡for ¡joins) ¡ 12 ¡

  13. How ¡does ¡a ¡join ¡work? ¡ • Three ¡main ¡algorithms: ¡ – Nested ¡loop ¡join ¡ – Sort-­‑merge ¡join ¡ – Hash ¡join ¡ 13 ¡

  14. Nested ¡loop ¡join ¡ ¡For ¡each ¡tuple ¡r ¡in ¡R ¡do ¡ ¡ ¡ ¡ ¡ ¡For ¡each ¡tuple ¡s ¡in ¡S ¡do ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡If ¡r ¡and ¡s ¡sa)sfy ¡the ¡join ¡condi)on ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Then ¡output ¡the ¡tuple ¡<r,s> ¡ 14 ¡

  15. Sort-­‑Merge ¡join ¡ • Assume ¡we ¡want ¡to ¡join ¡R ¡and ¡S ¡on ¡some ¡ ajribute ¡A. ¡ • Sort ¡both ¡R ¡and ¡S ¡by ¡A. ¡ • Perform ¡two ¡linear ¡scans ¡of ¡R ¡and ¡S. ¡ – Works ¡well ¡with ¡no ¡duplicate ¡values ¡of ¡A. ¡ 15 ¡

  16. Hash ¡join ¡ ¡ • Join ¡R ¡and ¡S ¡on ¡A. ¡ • Make ¡a ¡hash ¡table ¡of ¡the ¡smaller ¡rela)on, ¡ mapping ¡A ¡to ¡the ¡appropriate ¡row(s) ¡of ¡R ¡(or ¡ S). ¡ • Scan ¡the ¡larger ¡rela)on ¡to ¡find ¡the ¡relevant ¡ rows ¡using ¡the ¡hash ¡table. ¡ 16 ¡

  17. Equivalence ¡of ¡expressions ¡ • Natural ¡joins: ¡ – commuta)ve ¡ R ▹ ◃ S S ▹ ◃ R = ( R ▹ ◃ S ) ▹ ◃ T R ▹ ◃ ( S ▹ ◃ T ) – associa)ve ¡ = ¡ • How ¡many ¡different ¡join ¡orderings ¡are ¡there ¡ for ¡n ¡rela)ons? ¡ 17 ¡

  18. Equivalence ¡of ¡expressions ¡ • Natural ¡joins: ¡ – commuta)ve ¡ R ▹ ◃ S S ▹ ◃ R = ( R ▹ ◃ S ) ▹ ◃ T R ▹ ◃ ( S ▹ ◃ T ) – associa)ve ¡ = ¡ • How ¡many ¡different ¡join ¡orderings ¡are ¡there ¡ for ¡n ¡rela)ons? ¡ – Catalan ¡number ¡= ¡O(4^n) ¡ 18 ¡

  19. Why ¡care? ¡ 19 ¡

  20. Picking ¡good ¡join ¡orders ¡ • Query ¡op)mizer ¡generates ¡a ¡few ¡poten)al ¡ orders ¡ – Doesn't ¡evaluate ¡all ¡O(4^n) ¡possibili)es. ¡ – Prefers ¡deep ¡trees ¡over ¡bushy ¡trees. ¡ • How ¡many ¡leq-­‑deep ¡trees ¡are ¡there ¡for ¡n ¡rela)ons? ¡ 20 ¡

  21. • Query ¡op)mizer ¡tries ¡to ¡es)mate ¡the ¡cost ¡for ¡ each ¡ query ¡plan , ¡relying ¡on ¡ – Sta)s)cs ¡maintained ¡for ¡rela)ons ¡and ¡indexes ¡ (size ¡of ¡rela)on, ¡size ¡of ¡index, ¡number ¡of ¡dis)nct ¡ values ¡in ¡columns, ¡etc) ¡ – Formulas ¡to ¡es)mate ¡selec)vity ¡of ¡predicates ¡(the ¡ probability ¡that ¡a ¡randomly-­‑selected ¡row ¡will ¡be ¡ true ¡for ¡a ¡predicate) ¡ – Formulas ¡to ¡es)mate ¡CPU ¡and ¡I/O ¡costs ¡of ¡ selec)ons, ¡projec)ons, ¡joins, ¡aggrega)ons, ¡etc. ¡ 21 ¡

  22. Views ¡ • A ¡ view ¡is ¡a ¡stored ¡SQL ¡query ¡that ¡can ¡be ¡used ¡ as ¡if ¡it ¡were ¡a ¡rela)on. ¡ • Only ¡the ¡query ¡itself ¡is ¡stored, ¡not ¡the ¡results. ¡ – Results ¡are ¡re-­‑computed ¡whenever ¡the ¡view ¡is ¡ used. ¡ • Saves ¡typing, ¡but ¡not ¡)me. ¡ • CREATE ¡VIEW ¡name ¡AS ¡ SELECT…FROM…WHERE ¡ 22 ¡

  23. Materialized ¡Views ¡ • A ¡ materialized ¡ view ¡stores ¡the ¡results ¡of ¡the ¡ query ¡rather ¡than ¡the ¡query ¡itself. ¡ • Results ¡are ¡re-­‑computed ¡as ¡needed. ¡ • Saves ¡typing ¡and ¡usually ¡)me, ¡at ¡the ¡cost ¡of ¡ space. ¡ • CREATE ¡MATERIALIZED ¡VIEW ¡name ¡AS ¡ SELECT…FROM…WHERE ¡ – In ¡many ¡RDBMSs, ¡but ¡not ¡SQLite. ¡ 23 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend