Data Engineering
Performance Tuning: Query Plan Selection
1
Given a SQL query Q
2
Query Execution Plans
Consider
SELECT id, age, zipcode
FROM Stops, Zips
WHERE Stops.location = Zips.location AND age > 35
Stops and Zips are both laid out as blocks
Q: What are ways in which we could execute this query?
Query Execution Plans: The Logical
SELECT id, age, zipcode
FROM Stops, Zips
WHERE Stops.location = Zips.location AND age > 35
Query Execution Plans: The Physical
SELECT id, age, zipcode
FROM Stops, Zips
WHERE Stops.location = Zips.location AND age > 35
What join algorithm do we use?
Do we use an index on age or not?
Do we wait until the results of the join are ready before we start doing projection? Or can we do it “on-the-fly”?
Given a SQL query Q
6
Logical Operators vs. Physical Operators
7
Side Note: Code-Centric vs. Query-Centric
A Query Plan By Any Other Name…
Streaming
NoSQL Pipelines
ML Computation Graphs
Recap: Why Users Should Worry About Performance Even in Query Centric Systems
Multiple reasons why:
Reason I: revisiting our specification
Reason 2: contract breakdown
Reason 3: many heavy-weight levers still under the control of users
The Declarative Contract
Getting started: Scanning a Relation R
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
Let’s Talk About Joins
12
Join Approach 1: Nested Loop Joins
Simple Approach:
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
R
S
Join Approach 1: Nested Loop Joins
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
1
2
3
4
5
6
15
16
5
6
7
8
1
2
3
4
R
S
4
2
A Brief Recap: Merge Sort
Join Approach 2: Sort-Merge Join
16
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
11
12
13
14
11
12
13
14
5
6
7
8
21
22
23
24
21
22
23
24
1
2
3
4
31
32
33
34
31
32
33
34
5
6
7
8
41
42
43
44
41
42
43
44
11
21
31
41
1
1
2
2
22
3
3
32
4
4
42
23
5
Sort
Merge
Join Approach 2: Sort-Merge Join
17
Join Approach 3: Hash Join
18
1
2
3
4
5
6
7
8
1
2
3
4
5
6
11
12
13
14
1
Start of hashing R phase 1
S
R
19
1
2
3
4
5
6
7
8
1
2
3
4
5
6
11
12
13
14
1
21
Join Approach 3: Hash Join
Join Approach 3: Hash Join
20
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
14
2
21
11
Join Approach 3: Hash Join
21
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
14
3
21
11
Join Approach 3: Hash Join
22
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
14
4
21
11
Join Approach 3: Hash Join
23
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
14
4
21
11
23
Join Approach 3: Hash Join
24
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
8
21
11
22
14
22
End of hashing R phase 1
Join Approach 3: Hash Join
25
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
22
1
32
24
31
23
Start of hashing S phase 1
Join Approach 3: Hash Join
26
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
32
31
23
32
24
34
44
41
6
End of hashing S phase 1
Join Approach 3: Hash Join
27
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
32
31
23
32
24
34
44
41
21
11
31
41
1
Joining red buckets phase 2
Join Approach 3: Hash Join
28
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
32
31
23
32
24
34
44
41
21
11
31
41
1
2
3
4
5
Joining red buckets phase 2
Join Approach 3: Hash Join
29
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
32
31
23
32
24
34
44
41
1
2
3
4
5
13
23
6
Joining dark g. buckets phase 2
Join Approach 3: Hash Join
30
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
32
31
23
32
24
34
44
41
1
2
3
4
5
13
23
6
7
8
Joining dark g. buckets phase 2
Join Approach 3: Hash Join
31
1
2
3
4
5
6
7
8
1
2
3
4
5
6
12
13
21
11
22
14
32
31
23
32
24
34
44
41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Join Approach 3: Hash Join
Similar Variants Exist for Other Binary Operators
33
Other Operators: Filters, Projects, Grouping
34
Overall: Physical Design of Operators is Hard!
35
Recap: Logical Operators vs. Physical Operators
36
OK…
37
Step 1: Converting to a logical query plan
SELECT a1, a2, …, aggs
FROM R1, … Rk
WHERE C
GROUP BY b1, …, bm
HAVING H
38
Usually Joins
Extended RA operator for grouping and aggregation
Some subqueries can be rewritten!
SELECT DISTINCT Stops.location FROM Stops
WHERE Stops.location IN (SELECT Zips.location FROM Zips)
Q: Can we rewrite without using a subquery?
SELECT DISTINCT Stops.location FROM Stops, Zips
WHERE Stops.location = Zips.location
Q: What if we dropped the DISTINCT keyword?
39
Step 2: Rewriting the Logical Plan
SELECT id, age, zipcode
FROM Stops, Zips
WHERE Stops.location = Zips.location AND age > 35
Need to be able to tell that these three plans are equivalent
Step 2: Rewriting the Logical Plan
41
Laws Involving Selection: Examples
42
Laws Involving Selection: Examples
43
Laws Involving Projection
44
How to use these rules
Product (maker, price, pname, category)
Company (name, city, owner, marketcap)
Query plans (RA exps) also depicted as trees
Q: What does this query evaluate to?
Q: Can we push the predicates down?
45
Product
Company
maker = name
price>100 &
city = “berkeley”
pname
How to use these rules
46
Product
Company
maker = name
pname
price>100
city = “berkeley”
Product
Company
maker = name
price>100 &
city = “berkeley”
pname
How to use these rules
47
Product
Company
maker = name
pname
price>100
city = “berkeley”
Q: can we push
projections down?
How to use these rules
48
Product
Company
maker = name
pname
price>100
city = “berkeley”
pname,price,
maker
name,city
Product
Company
maker = name
pname
price>100
city = “berkeley”
OK…
49