2/3/21 1 1 February 3, 2021 Database System Internals CSE 444 - Winter 2021 Query Optimization (part 1) 1 We know how to compute the cost of a plan Next: Find a good plan automatically? This is the role of the query optimizer CSE 444 - Winter 2021 2 February 3, 2021 Query Optimization Overview 2 Query Optimization Overview Parse & Rewrite Query Select Logical Plan Select Physical Plan Query Execution Disk SQL query Query optimization Logical plan Physical plan 3 February 3, 2021 CSE 444 - Winter 2021 3 What We Already Know… Supplier(sno,sname,scity,sstate) Part(pno,pname,psize,pcolor) Supply(sno,pno,price) For each SQL query…. SELECT S.sname FROM Supplier S, Supply U WHERE S.scity='Seattle' AND S.sstate='WA’ AND S.sno = U.sno AND U.pno = 2 There exist many logical query plans… 4 February 3, 2021 CSE 444 - Winter 2021 4
8
Embed
Database System Internals Query Optimization (part 1)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
2/3/21
1
1February 3, 2021
Database System Internals
CSE 444 - Winter 2021
Query Optimization (part 1)
1
We know how to compute the cost of a plan
Next: Find a good plan automatically?
This is the role of the query optimizer
CSE 444 - Winter 2021 2February 3, 2021
Query Optimization Overview
2
Query Optimization Overview
Parse & Rewrite Query
Select Logical Plan
Select Physical Plan
Query Execution
Disk
SQL query
Queryoptimization
Logicalplan
Physicalplan
3
February 3, 2021 CSE 444 - Winter 2021
3
What We Already Know…Supplier(sno,sname,scity,sstate)
Part(pno,pname,psize,pcolor)Supply(sno,pno,price)
For each SQL query….
SELECT S.snameFROM Supplier S, Supply UWHERE S.scity='Seattle' AND S.sstate='WA’
AND S.sno = U.snoAND U.pno = 2
There exist many logical query plans…
4February 3, 2021 CSE 444 - Winter 2021
4
2/3/21
2
CSE 444 - Winter 2021
Example Query: Logical Plan 1
Supplier Supply
sno = sno
𝜎sscity=‘Seattle’ ⋀ state=‘WA’ ⋀ pno=2
𝛑sname
5February 3, 2021
5
Example Query: Logical Plan 2
CSE 444 - Winter 2021 6
Supplier Supply
sno = sno
𝜎sscity=‘Seattle’ ⋀ sstate=‘WA’
𝛑sname
𝜎pno=2
February 3, 2021
6
What We Also Know
§For each logical plan…
§There exist many physical plans
CSE 444 - Winter 2021 7February 3, 2021
7
Example Query: Physical Plan 1
CSE 444 - Winter 2021 8
Supplier Supply
sno = sno
𝜎scity=‘Seattle’ ⋀ sstate=‘WA’ ⋀ pno=2
𝛑sname
(File scan) (File scan)
(Nested loop)
(On the fly)
(On the fly)
February 3, 2021
8
2/3/21
3
Example Query: Physical Plan 2
CSE 444 - Winter 2021 9
Supplier Supply
sno = sno
𝜎scity=‘Seattle’ ⋀ sstate=‘WA’ ⋀ pno=2
𝛑sname
(File scan) (Index scan)
(Index nested loop)
(On the fly)
(On the fly)
February 3, 2021
9CSE 444 - Winter 2021 10
Query Optimizer Overview
§ Input: A logical query plan§Output: A good physical query plan
February 3, 2021
10
CSE 444 - Winter 2021 11
Query Optimizer Overview
§ Input: A logical query plan§Output: A good physical query plan§Basic query optimization algorithm
• Enumerate alternative plans (logical and physical)• Compute estimated cost of each plan
• Compute number of I/Os• Optionally take into account other resources
• Choose plan with lowest cost• This is called cost-based optimization
February 3, 2021
11
Two Types of Optimizers
§Rule-based (heuristic) optimizers:• Apply greedily rules that always improve plan
• Typically: push selections down• Very limited: no longer used today
§Cost-based optimizers:• Use a cost model to estimate the cost of each plan• Select the “cheapest” plan• We focus on cost-based optimizers
CSE 444 - Winter 2021 12February 3, 2021
12
2/3/21
4
13
Observations
§No magic “best” plan: depends on the data
§ In order to make the right choice• Need to have statistics over the data• The B’s, the T’s, the V’s• Commonly: histograms over base data
• In SimpleDB as well… lab 5.
CSE 444 - Winter 2021February 3, 2021
13
Key Decisions for Implementation
Search Space
Optimization rules
Optimization algorithm
14CSE 444 - Winter 2021February 3, 2021
14
Key Decisions for Implementation
Search SpaceWhat form of plans do we consider?
Optimization rules
Optimization algorithm
15CSE 444 - Winter 2021February 3, 2021
15
Search Space – Type of Plan
16
R3 R1 R2 R4R3 R1
R4
R2
Left-deep plan Bushy plan
CSE 444 - Winter 2021
Linear plan: One input to each join is a relation from diskCan be either left or right input
February 3, 2021
16
2/3/21
5
Key Decisions for Implementation
Search Space
Optimization rulesWhich algebraic laws do we apply?
Optimization algorithm
17CSE 444 - Winter 2021February 3, 2021
1718
Optimization Rules – RA equivalencies
§Selections• Commutative: 𝜎c1(𝜎c2(R)) same as 𝜎c2(𝜎c1(R))• Cascading: 𝜎c1⋀c2(R) same as 𝜎c2(𝜎c1(R))
§Projections• Cascading
§ Joins• Commutative : R ⋈ S same as S ⋈ R • Associative: R ⋈ (S ⋈ T) same as (R ⋈ S) ⋈ T
CSE 444 - Winter 2021February 3, 2021
18
19
Example: Simple Algebraic Laws
§Example: R(A, B, C, D), S(E, F, G)
s F=3 (R ⨝ D=E S) =
s A=5 AND G=9 (R ⨝ D=E S) =
CSE 444 - Winter 2021February 3, 2021
1920
Example: Simple Algebraic Laws
§Example: R(A, B, C, D), S(E, F, G)
s F=3 (R ⨝ D=E S) = R ⨝ D=E s F=3 (S)
s A=5 AND G=9 (R ⨝ D=E S) =
CSE 444 - Winter 2021February 3, 2021
20
2/3/21
6
21
Example: Simple Algebraic Laws
§Example: R(A, B, C, D), S(E, F, G)
s F=3 (R ⨝ D=E S) = R ⨝ D=E s F=3 (S)
s A=5 AND G=9 (R ⨝ D=E S) = s A=5 (R) ⨝ D=E sG=9(S)
CSE 444 - Winter 2021February 3, 2021
21
Commutativity, Associativity, Distributivity
22
R ∪ S = S ∪ R, R ∪ (S ∪ T) = (R ∪ S) ∪ TR ⨝ S = S ⨝ R, R ⨝ (S ⨝ T) = (R ⨝ S) ⨝ T
R ⨝ (S ∪ T) = (R ⨝ S) ∪ (R ⨝ T)
CSE 444 - Winter 2021February 3, 2021
22
Laws Involving Selection
23
s C AND C’(R) = s C(s C’(R)) = s C(R) ∩ s C’(R)s C OR C’(R) = s C(R) ∪ s C’(R)s C (R ⨝ S) = s C (R) ⨝ S
s C (R – S) = s C (R) – Ss C (R ∪ S) = s C (R) ∪ s C (S)s C (R ⨝ S) = s C (R) ⨝ S