![Page 1: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/1.jpg)
Buffer-pool aware Query Optimization
Ravishankar RamamurthyDavid DeWitt
University of Wisconsin, Madison
![Page 2: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/2.jpg)
2
managing main memory
• Main memories are increasing• Prices declining at about 100x per decade• Advent of 64-bit machines • 1 TB of main memory feasible
• Use a BIGGER buffer pool• Caching does not automatically guarantee improved
performance
![Page 3: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/3.jpg)
3
• Optimizer uses “worst-case” estimates
• Selection query on a single table• Optimizers would choose an unclustered index only for
highly selective queries (~0.1%)• Even if all required pages are cached, optimizer would
still pick a table scan
problem
![Page 4: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/4.jpg)
4
goal
• Buffer-pool aware query optimizer• Benefits ?• Architecture ?
• Focus• Single table queries• Foreign-key joins
![Page 5: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/5.jpg)
5
single table queries
• Prototype query engine• SHORE (320 MB buffer pool, 16 KB pages)• TPC-H 1 GB database
• Selection predicate on Lineitem table (0.5%)• Unclustered index available for evaluating predicate
![Page 6: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/6.jpg)
6
Index scan vs. Scan
0
5
10
15
20
0 0.3 0.5 0.7 0.8 0.9 1
fraction of required pages cached (f)
Tim
e (s
)
Index Scan (s)
Table Scan (s)
![Page 7: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/7.jpg)
7
join queries
• Join Query between Lineitem and Orders • range predicate on l_receiptdate • unclustered index on l_receiptdate
• Index alternatives• Covering Indexes (Cov1, Cov2)• Join Index on (l_orderkey, o_orderkey)
• Stores (RID1, RID2) pair of joining tuples
![Page 8: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/8.jpg)
8
JINDEX plan
FETCH
(Lineitem)
B-Tree
Range Scan
(l_receiptdate)
PROBE
Join Index
FILTER
FETCH
(Orders)
![Page 9: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/9.jpg)
9
effect of buffer pool
0
5
10
15
20
25
Fraction of required pages cached (f1, f2)
Tim
e (s
ec)
JINDEX plan
COV1 join COV2
![Page 10: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/10.jpg)
10
benefits
• Similar tradeoff for other combinations• Index nested loops vs. Sort Merge Join
• Relative costs of plans• Caching can cause a big difference• Optimizer could miss plans that have much better
performance
![Page 11: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/11.jpg)
11
what is needed ?
• Optimizer needs improved cost functions
• Given a selection (join) predicate• What fraction of pages (f) containing tuples that satisfy
the predicate is in memory.• Cost of Index plan = N * (1 – f) * io_cost
• Not altering search space
![Page 12: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/12.jpg)
12
challenges
• Parameter f• function of query and buffer pool state
• Simple page count per relation will not suffice• Different queries require different subsets of pages
![Page 13: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/13.jpg)
13
• Assume interface• bool isCached(RID)
• selection (join) predicate• Optimizer computes RIDs of tuples that satisfy the
predicate• Use isCached() to calculate f.
solution ?
![Page 14: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/14.jpg)
14
candidates
• Index Pre-execution• Accurate technique• High overheads
• Sampling techniques• “close-enough” accuracy• Low overheads
![Page 15: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/15.jpg)
15
index pre-execution
• Compute RID lists during query optimization• “pre-execute” predicates on indexes
• Selection Predicates• Unclustered index on required attribute.• Evaluate predicate only on index pages.• Use List of RIDs and IsCached() to calculate f.
![Page 16: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/16.jpg)
16
selection predicates
• Lineitem table (1 GB TPC-H)• Range Predicate on l_shipdate column
• Shore B-Tree on l_shipdate column
Tuple Count Cold (s) Hot (s)1000 0.11 0.025000 0.38 0.10
10000 0.74 0.2150000 3.13 1.14
• overhead can be15-20% of scan time
![Page 17: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/17.jpg)
17
observations
• Index pre-execution• Accurate but not practical
• Optimizer should not miss important cases• Large fraction of required pages are in memory• How important is accuracy ?
![Page 18: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/18.jpg)
18
relaxing accuracy
• Close-enough (~5%) estimates can suffice• Can sampling help ?
Index scan vs. Scan
0
5
10
15
20
0 0.3 0.5 0.7 0.8 0.9 1
fraction of required pages cached (f)
Tim
e (
s)
Index Scan (s)
Table Scan (s)
![Page 19: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/19.jpg)
19
sampling
• Select * from R where R.value = 1
11111111110000000010000000010011111111110000000111111001001111111111110
11111111111000000010000000010011111111110000000111111000001111111111110
factual = 30/40 = 0.75
festimated = 3/4 = 0.7511111111110000000010000000010011111111110000000111111001001111111111110
11111111110000000010000000010011111111110000000111111001001111111111110
![Page 20: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/20.jpg)
20
sampling
• Index pre-execution• Used to gather RID lists that satisfy predicates
• Alternative• Use random samples of RIDs instead• Pre-compute samples and cache in main memory• Avoids I/Os during query optimization
![Page 21: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/21.jpg)
21
selection predicates
• Pre-computation • Samples on base table (table A)• Reservoir sampling using table scan
• Sa stores (Atuple, RID-A) pair
• Using the samples• Evaluate predicate on Sa
• Use RID-A samples and isCached() interface to calculate festimated
![Page 22: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/22.jpg)
22
experiments
• Simulate buffer pool configurations• Pre-fetch appropriate ranges
• calculate factual
• calculate festimated using sampling
• Evaluation Metric• Mean of ABS (factual - festimated)
• ERR1 (all configurations)
• ERR2 (configurations having factual > 0.75)
![Page 23: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/23.jpg)
23
selection predicate
• Selection predicate on Lineitem table• l_shipdate between (1994-01-01,1994-01-11)
Sample Size ERR1 ERR2
6000 7.04% 4.60%
12000 5.91% 3.50%
30000 4.13% 2.57%
60000 4.06% 2.39%
![Page 24: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/24.jpg)
24
join predicates
• Foreign key join between A and B• A.a is foreign key pointing to B.b.• Index pre-execution not feasible • Sampling techniques
• Pre-computation for joins• Assume Sab is pre-computed
• Sab = Sa Join B
• stores (RID-A, Atuple, Btuple, RID-B)
![Page 25: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/25.jpg)
25
using the samples
• Join Query between A.a and B.b• Range predicate on table A
• Required• What fraction of pages of B that satisfies the join predicate is
cached (f)• Cost of Index nested loops join with B as “inner”
• Approach• Evaluate predicate on Sab• Project RID-B samples that satisfy predicate • Use RID-B samples and isCached() to calculate festimated
![Page 26: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/26.jpg)
26
join predicate
• Join between Lineitem and Order• Predicates on l_receiptdate and l_shipmode
Sample Size ERR1 ERR2
6000 11.68% 7.98%
12000 9.29% 5.99%
30000 5.88% 3.43%
60000 4.35% 3.09%
![Page 27: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/27.jpg)
27
overheads
• Sampling Overheads• No I/Os (compared to index pre-execution)• CPU overheads ~20 ms (2 GHz machine)
• Space Overheads• 1% sample (base table + foreign key relationships) • 25 MB for entire TPC-H database (1 GB)
![Page 28: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/28.jpg)
28
not enough samples
• Unclustered Index vs. Table Scan• Evaluate selection predicate on Sample• RID sample not sufficient• Avoid changing plans if “confidence” is low
• Infer “highly-selective” predicates• Choose index plan
![Page 29: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/29.jpg)
29
highly selective predicates
• Thresholds in predicate selectivity (s)• s < T1 ( Use Index Plan)• s > T2 ( Use Table Scan)
• Probability of “Error” is low• T1 = 0.1%, T2 = 1%• Correct with 99% probability if sample size is 1800
![Page 30: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/30.jpg)
30
extensions
• Multi-way foreign key joins• Join Synopses + RIDs
• Nested Queries• De-correlation vs. Nested Iteration
• Compiled queries• Use “choose” operator
![Page 31: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/31.jpg)
31
summary
• Large Buffer pools (~ 1 TB)• Significant fraction of “required” pages can be cached
• Optimizer needs to be aware of buffer pool contents• Can result in significant improvements
![Page 32: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/32.jpg)
32
Misc slides
• Transient buffer pool
• Pre-execution for joins
![Page 33: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/33.jpg)
33
transient buffer pool
• Buffer pool contents could change before query execution
• Use Choose operator• P1 – Plan picked by traditional optimizer• P2 – Plan picked by buffer pool aware optimizer• Execution plan is Choose (P1, P2)
![Page 34: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/34.jpg)
34
pre-execution for joins
• Foreign-key join between A.a and B.b
RID-A FETCH A.a PROBE
INDEX (B.b)
RID-B
• Use Index on Key value (B.b)• Use Join Index
PROBE
JOIN INDEX
RID-B
![Page 35: Buffer-pool aware Query Optimization Ravishankar Ramamurthy David DeWitt University of Wisconsin, Madison](https://reader035.vdocuments.site/reader035/viewer/2022062409/5697bf7a1a28abf838c82e46/html5/thumbnails/35.jpg)
35
join predicates
RID Count JINDEX (cold) JINDEX (hot)100 0.57 0.01500 2.52 0.04
1000 4.67 0.08
RID Count INDEX on Key (cold) INDEX on Key (hot)100 1.40 0.01500 5.40 0.05
1000 9.64 0.11