Download presentation
Presentation is loading. Please wait.
Published byJanis Parrish Modified over 6 years ago
1
Faloutsos/Pavlo C. Faloutsos – A. Pavlo Lecture#13: Query Evaluation
CMU /615 Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#13: Query Evaluation
2
Today's Class Intro to Operator Evaluation Typical Query Optimizer
Faloutsos/Pavlo CMU /615 Today's Class Intro to Operator Evaluation Typical Query Optimizer Projection/Aggregation: Sort vs. Hash Faloutsos/Pavlo CMU SCS /615
3
Example Database STUDENT ENROLLED COURSE sid name login age gpa 53666
Faloutsos/Pavlo CMU /615 Example Database STUDENT ENROLLED sid name login age gpa 53666 Kayne 39 4.0 53688 Bieber 22 3.9 53655 Tupac 26 3.5 sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 COURSE cid name 15-415 Database Applications 15-721 Database Systems 15-826 Data Mining 15-823 Advanced Topics in Databases Faloutsos/Pavlo CMU SCS /615
4
pname,cid (sgrade=‘A’ (student⋈enrolled))
Query Plan Example SELECT name, cid FROM student, enrolled WHERE student.sid = enrolled.sid AND enrolled.grade = ‘A’ Relational Algebra: pname,cid (sgrade=‘A’ (student⋈enrolled)) Faloutsos/Pavlo CMU SCS /615
5
Query Plan Example p s ⨝ SELECT name, cid FROM student, enrolled
WHERE student.sid = enrolled.sid AND enrolled.grade = ‘A’ student enrolled s ⨝ p name, cid grade=‘A’ sid=sid Faloutsos/Pavlo CMU SCS /615
6
Query Plan Example p s ⨝ SELECT name, cid FROM student, enrolled
WHERE student.sid = enrolled.sid AND enrolled.grade = ‘A’ “On-the-fly” student enrolled s ⨝ p name, cid “On-the-fly” grade=‘A’ Hash Join sid=sid File Scan File Scan Faloutsos/Pavlo CMU SCS /615
7
Query Plan Example p s ⨝ SELECT name, cid FROM student, enrolled
WHERE student.sid = enrolled.sid AND enrolled.grade = ‘A’ student enrolled s ⨝ p The output of each operator is the input to the next operator. Each operator iterates over its input and performs some task. Faloutsos/Pavlo CMU SCS /615
8
After Midterm: How the DBMS finds the best plan.
Operator Evaluation Several algorithms are available for different relational operators. Each has its own performance trade-offs. The goal of the query optimizer is to choose the one that has the lowest “cost”. After Midterm: How the DBMS finds the best plan. Faloutsos/Pavlo CMU SCS /615
9
Operator Execution Strategies
Indexing Iteration (= seq. scanning) Partitioning (sorting and hashing) Faloutsos/Pavlo CMU SCS /615
10
Operator Algorithms Selection: file scan; index scan
Projection: hashing; sorting Join: looping; hashing; sorting Group By: hashing; sorting Order By: sorting Faloutsos/Pavlo CMU SCS /615
11
Operator Algorithms Selection: file scan; index scan
Next Lecture Selection: file scan; index scan Projection: hashing; sorting Join: looping; hashing; sorting Group By: hashing; sorting Order By: sorting Today Next Lecture Today Today Faloutsos/Pavlo CMU SCS /615
12
Today's Class Intro to Operator Evaluation Typical Query Optimizer
Faloutsos/Pavlo CMU /615 Today's Class Intro to Operator Evaluation Typical Query Optimizer Projection/Aggregation: Sort vs. Hash Faloutsos/Pavlo CMU SCS /615
13
Query Optimization Bring query in internal form (eg., parse tree)
… into “canonical form” (syntactic q-opt) Generate alternative plans. Estimate cost for each plan. Pick the best one. Faloutsos/Pavlo CMU SCS /615
14
Query Plan Example p s ⨝ SELECT name, cid FROM student, enrolled
WHERE student.sid = enrolled.sid AND enrolled.grade = ‘A’ student enrolled s ⨝ p name, cid grade=‘A’ sid=sid Faloutsos/Pavlo CMU SCS /615
15
Query Plan Example s ⨝ p s ⨝ p s ⨝ p SELECT name, cid
FROM student, enrolled WHERE student.sid = enrolled.sid AND enrolled.grade = ‘A’ student enrolled s ⨝ p sid=sid grade=‘A’ name, cid student enrolled s ⨝ p sid=sid grade=‘A’ name, cid sid, cid student enrolled s ⨝ p sid=sid grade=‘A’ name, cid Faloutsos/Pavlo CMU SCS /615
16
Today's Class Intro to Operator Evaluation Typical Query Optimizer
Faloutsos/Pavlo CMU /615 Today's Class Intro to Operator Evaluation Typical Query Optimizer Projection/Aggregation: Sort vs. Hash Faloutsos/Pavlo CMU SCS /615
17
Duplicate Elimination
What does it do, in English? How to execute it? SELECT DISTINCT cid FROM enrolled WHERE grade IN (‘B’,’C’) pDISTINCT cid (sgrade=‘B’ ∨ grade=‘C’ (enrolled)) Not technically correct because RA doesn’t have “DISTINCT” Faloutsos/Pavlo CMU SCS /615
18
Duplicate Elimination
SELECT DISTINCT cid FROM enrolled WHERE grade IN (‘B’,’C’) enrolled s p grade=‘B’ ∨ grade=‘C’ DISTINCT cid Two Choices: Sorting Hashing Faloutsos/Pavlo CMU SCS /615
19
Sorting Projection p s X Filter Remove Columns Sort Eliminate Dupes
enrolled s p grade=‘B’ ∨ grade=‘C’ DISTINCT cid sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 sid cid grade 53666 15-415 C 53688 15-826 B 15-721 53655 cid 15-415 15-826 15-721 cid 15-415 15-721 15-826 X Filter Remove Columns Sort Eliminate Dupes Faloutsos/Pavlo CMU SCS /615
20
Alternative to Sorting: Hashing!
What if we don’t need the order of the sorted data? Forming groups in GROUP BY Removing duplicates in DISTINCT Hashing does this! And may be cheaper than sorting! (why?) But what if table doesn’t fit in memory? Faloutsos/Pavlo CMU SCS /615
21
Hashing Projection Populate an ephemeral hash table as we iterate over a table. For each record, check whether there is already an entry in the hash table: DISTINCT: Discard duplicate. GROUP BY: Perform aggregate computation. Two phase approach. Faloutsos/Pavlo CMU SCS /615
22
Phase 1: Partition Use a hash function h1 to split tuples into partitions on disk. We know that all matches live in the same partition. Partitions are “spilled” to disk via output buffers. Assume that we have B buffers. Faloutsos/Pavlo CMU SCS /615
23
Phase 1: Partition p s h1 ⋮ Filter Remove Columns Hash enrolled
grade=‘B’ ∨ grade=‘C’ DISTINCT cid sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 B-1 partitions 15-415 ⋮ 15-826 15-721 sid cid grade 53666 15-415 C 53688 15-826 B 15-721 53655 cid 15-415 15-826 15-721 h1 Filter Remove Columns Hash Faloutsos/Pavlo CMU SCS /615
24
Phase 2: ReHash For each partition on disk:
Read it into memory and build an in-memory hash table based on a hash function h2 Then go through each bucket of this hash table to bring together matching tuples This assumes that each partition fits in memory. Faloutsos/Pavlo CMU SCS /615
25
Phase 2: ReHash p s h2 h2 h2 ⋮ Hash Table Eliminate Dupes enrolled
grade=‘B’ ∨ grade=‘C’ DISTINCT cid sid cid grade 53666 15-415 C 53688 15-721 A 15-826 B 53655 15-415 ⋮ 15-826 15-721 h2 key value XXX 15-415 YYY 15-826 ZZZ 15-721 cid 15-415 15-826 15-721 h2 Partitions From Phase 1 h2 Hash Table Eliminate Dupes Faloutsos/Pavlo CMU SCS /615
26
Analysis How big of a table can we hash using this approach?
B-1 “spill partitions” in Phase 1 Each should be no more than B blocks big Faloutsos/Pavlo CMU SCS /615
27
Analysis How big of a table can we hash using this approach?
B-1 “spill partitions” in Phase 1 Each should be no more than B blocks big Answer: B∙(B-1). A table of N blocks needs about sqrt(N) buffers What assumption do we make? Faloutsos/Pavlo CMU SCS /615
28
Analysis How big of a table can we hash using this approach?
B-1 “spill partitions” in Phase 1 Each should be no more than B blocks big Answer: B∙(B-1). A table of N blocks needs about sqrt(N) buffers Assumes hash distributes records evenly! Use a “fudge factor” f >1 for that: we need B ~ sqrt( f ∙N) Faloutsos/Pavlo CMU SCS /615
29
Analysis Have a bigger table? Recursive partitioning!
In the ReHash phase, if a partition i is bigger than B, then recurse. Pretend that i is a table we need to hash, run the Partitioning phase on i, and then the ReHash phase on each of its (sub)partitions Faloutsos/Pavlo CMU SCS /615
30
Recursive Partitioning
Hash the overflowing bucket again h2 ⋮ h1 h1’ Hash Faloutsos/Pavlo CMU SCS /615
31
Hashing vs. Sorting Which one needs more buffers? Faloutsos/Pavlo
CMU SCS /615
32
Hashing vs. Sorting Recall: We can hash a table of size N blocks in sqrt(N) space How big of a table can we sort in 2 passes? Get N/B sorted runs after Pass 0 Can merge all runs in Pass 1 if N/B ≤ B-1 Thus, we (roughly) require: N ≤ B2 We can sort a table of size N blocks in about space sqrt(N) Same as hashing! Faloutsos/Pavlo CMU SCS /615
33
Hashing vs. Sorting Choice of sorting vs. hashing is subtle and depends on optimizations done in each case Already discussed optimizations for sorting: Chunk I/O into large blocks to amortize seek+RD costs Double-buffering to overlap CPU and I/O Faloutsos/Pavlo CMU SCS /615
34
Hashing vs. Sorting Choice of sorting vs. hashing is subtle and depends on optimizations done in each case Another optimization when using sorting for aggregation: “Early aggregation” of records in sorted runs Let’s look at some optimizations for hashing next… Faloutsos/Pavlo CMU SCS /615
35
Hashing: We Can Do Better!
Combine the summarization into the hashing process - How? Faloutsos/Pavlo CMU SCS /615
36
Hashing: We Can Do Better!
During the ReHash phase, store pairs of the form <GroupKey, RunningVal> When we want to insert a new tuple into the hash table: If we find a matching GroupKey, just update the RunningVal appropriately Else insert a new <GroupKey, RunningVal> Faloutsos/Pavlo CMU SCS /615
37
Hashing Aggregation h2 h2 h2 ⋮ Hash Table Final Result
SELECT cid, AVG(s.gpa) FROM student AS s, enrolled AS e WHERE s.sid = e.sid GROUP BY cid Running Totals 15-415 ⋮ 15-826 15-721 h2 key value XXX <15-415, 2.99> YYY <15-826,3.76> ZZZ <15-721,3.44> cid AVG(gpa) 15-415 3.66 15-826 3.333 15-721 2.89 h2 Partitions From Phase 1 h2 Hash Table Final Result Faloutsos/Pavlo CMU SCS /615
38
Hashing Aggregation What’s the benefit?
How many entries will we have to handle? Number of distinct values of GroupKeys columns Not the number of tuples! Also probably “narrower” than the tuples Faloutsos/Pavlo CMU SCS /615
39
So, Hashing is Better…right?
Any caveats? Sorting is better on non-uniform data. Sorting is better when result needs to be sorted. Hashing vs. sorting: Commercial systems use either or both Faloutsos/Pavlo CMU SCS /615
40
Summary Query processing architecture:
Query optimizer translates SQL to a query plan Query executor “interprets” the plan Hashing is a useful alternative to sorting for duplicate elimination / group-by Both are valuable techniques for a DBMS Faloutsos/Pavlo CMU SCS /615
41
Next Class How to actually use indexes. Join algorithms.
More query optimization. Faloutsos/Pavlo CMU SCS /615
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.