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

query op miza on
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Query ¡Op)miza)on ¡

1 ¡

slide-2
SLIDE 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 ¡
  • ne ¡way ¡to ¡run ¡a ¡query. ¡

2 ¡

slide-3
SLIDE 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 ¡

slide-4
SLIDE 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 ¡

slide-5
SLIDE 5

Example ¡

  • SELECT ¡First, ¡Last ¡

FROM ¡Students ¡NATURAL ¡JOIN ¡Enrolled ¡ WHERE ¡CRN=12345 ¡

Student Enrolled

5 ¡

slide-6
SLIDE 6

Example ¡

  • SELECT ¡First, ¡Last ¡

FROM ¡Students ¡NATURAL ¡JOIN ¡Enrolled ¡ WHERE ¡CRN=12345 ¡

Student Enrolled Student Enrolled

Canonical ¡Form ¡

6 ¡

slide-7
SLIDE 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 ¡

slide-8
SLIDE 8

8 ¡

slide-9
SLIDE 9

9 ¡

slide-10
SLIDE 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 ¡

slide-11
SLIDE 11

Example ¡

  • Prove ¡ ¡

) 2 ( ) 1 ( ) 2 1 (

?

R R R R

P P P

σ σ σ ∪ = ∪

11 ¡

slide-12
SLIDE 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 ¡

slide-13
SLIDE 13

How ¡does ¡a ¡join ¡work? ¡

  • Three ¡main ¡algorithms: ¡

– Nested ¡loop ¡join ¡ – Sort-­‑merge ¡join ¡ – Hash ¡join ¡

13 ¡

slide-14
SLIDE 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 ¡

slide-15
SLIDE 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 ¡

slide-16
SLIDE 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 ¡

slide-17
SLIDE 17

Equivalence ¡of ¡expressions ¡

  • Natural ¡joins: ¡

– commuta)ve ¡ – associa)ve ¡ ¡

  • How ¡many ¡different ¡join ¡orderings ¡are ¡there ¡

for ¡n ¡rela)ons? ¡ R S S R ◃ ▹ ◃ ▹ = ) ( ) ( T S R T S R ◃ ▹ ◃ ▹ ◃ ▹ ◃ ▹ =

17 ¡

slide-18
SLIDE 18

Equivalence ¡of ¡expressions ¡

  • Natural ¡joins: ¡

– commuta)ve ¡ – associa)ve ¡ ¡

  • How ¡many ¡different ¡join ¡orderings ¡are ¡there ¡

for ¡n ¡rela)ons? ¡

– Catalan ¡number ¡= ¡O(4^n) ¡

R S S R ◃ ▹ ◃ ▹ = ) ( ) ( T S R T S R ◃ ▹ ◃ ▹ ◃ ▹ ◃ ▹ =

18 ¡

slide-19
SLIDE 19

Why ¡care? ¡

19 ¡

slide-20
SLIDE 20

Picking ¡good ¡join ¡orders ¡

  • Query ¡op)mizer ¡generates ¡a ¡few ¡poten)al ¡
  • rders ¡

– 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 ¡

slide-21
SLIDE 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 ¡

slide-22
SLIDE 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 ¡

slide-23
SLIDE 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 ¡