3 - QueryProcessing - Ch15
3 - QueryProcessing - Ch15
Optimization
Chapter Outline
• Heap files
– random order; insert at end-of-file
• Sorted files
– sorted on <age, sal>
• Clustered B+ tree file
– search key <age, sal>
• Heap file with unclustered B+-tree index
– on search key <age, sal>
• Heap file with unclustered hash index
– on search key <age, sal>
Possible Operations
• Scan
– Fetch all records from disk to buffer pool
• Equality search
– Find all employees with age = 23 and sal = 50
– Fetch page from disk, then locate qualifying record in page
• Range selection
– Find all employees with age > 35
• Insert a record
– identify the page, fetch that page from disk, inset record, write back
to disk (possibly other pages as well)
• Delete a record
– similar to insert
Understanding the Workload
• A workload is a mix of queries and updates
SELECT E.dno
• How selective is the condition? FROM Emp E
– everyone > 40, index not of much WHERE E.age>40
help, scan is as good
– Suppose 10% > 40. Then?
Which attribute(s)?
• Depends on if the index is clustered Clustered/Unclustered?
– otherwise can be more expensive B+ tree/Hash?
than a linear scan
– if clustered, 10% I/O (+ index pages)
Examples of Clustered Indexes
Group-By query What is a good indexing
strategy?
• Use E.age as search key?
– Bad If many tuples have E.age > 10 or if not
clustered…. SELECT E.dno, COUNT (*)
– …using E.age index and sorting the retrieved FROM Emp E
tuples by E.dno may be costly
WHERE E.age>10
GROUP BY E.dno
• Clustered E.dno index may be better
– First group by, then count tuples with age >
10
– good when age > 10 is not too selective Which attribute(s)?
Clustered/Unclustered?
• Note: the first option is good when the B+ tree/Hash?
WHERE condition is highly selective (few
tuples have age > 10), the second is good
when not highly selective
Examples of Clustered Indexes
What is a good indexing
strategy?
Equality queries and duplicates SELECT E.dno
FROM Emp E
• Clustering on E.hobby helps WHERE E.hobby=‘Stamps’
– hobby not a candidate key, several
tuples possible Which attribute(s)?
Clustered/Unclustered?
B+ tree/Hash?
• Does clustering help now?
– (eid = key) SELECT E.dno
– Not much FROM Emp E
– at most one tuple satisfies the WHERE E.eid=50
condition
Indexes with Composite Search Keys
• Composite Search Keys: Search on a Examples of composite key
combination of fields indexes using lexicographic order.
INPUT 1
OUTPUT
INPUT 2
2N (log 2 N + 1)
2,3
3,4
8-page runs
• Not too practical, but useful to 4,5
learn basic concepts for 6,6
external sorting 7,8
9
General External Merge Sort
• Suppose we have more than 3 buffer pages.
• How can we utilize them?
• To sort a file with N pages using B buffer pages:
– Pass 0: use B buffer pages:
• Produce ⌈N/B⌉ sorted runs of B pages each.
– Pass 1, 2, …, etc.: merge B-1 runs to one output page
• keep writing to disk once the output page is full
INPUT 1
INPUT 2
... ... OUTPUT ...
INPUT B-1
Disk Disk
B Main memory buffers
Cost of External Merge Sort
• Number of passes:1 + ⌈logB-1⌈N/B⌉⌉
• Cost = 2N * (# of passes) – why 2 times?
• E.g., with 5 buffer pages, to sort 108 page file:
• Pass 0: sorting 5 pages at a time
– ⌈108/5⌉ = 22 sorted runs of 5 pages each (last run is only 3
pages)
• Pass 1: 4-way merge
– ⌈22/4⌉ = 6 sorted runs of 20 pages each (last run is only 8 pages)
• Pass 2: 4-way merge
– (but 2-way for the last two runs)
– [6/4⌉ = 2 sorted runs, 80 pages and 28 pages
• Pass 3: 2-way merge (only 2 runs remaining)
– Sorted file of 108 pages
Number of Passes of External Sort
High B is good, although CPU cost increases
INPUT 1
INPUT 1'
INPUT 2
OUTPUT
INPUT 2'
OUTPUT'
b
block size
Disk INPUT k
Disk
INPUT k'
• In algebra: R⨝ S
– Common! Must be carefully optimized
– R X S is large; so, R X S followed by a selection is inefficient
3. Hash Join
Algorithms for Joins
1. NESTED LOOP JOINS
M = 1000 pages in R
pR = 100 tuples per page
Simple Nested Loops Join
N = 500 pages in S
R⨝S pS = 80 tuples per page
foreach tuple r in R do
foreach tuple s in S where ri == sj do
add <r, s> to result
• For each tuple in the outer relation R, we scan the entire inner relation S.
– Cost: M + (pR * M) * N = 1000 + 100*1000*500 I/Os.
...
... ...
Input buffer for S
Output buffer
Block Nested Loops Join
• If R does not fit in memory,
– Use one page as an input buffer for scanning the inner S
– one page as the output buffer
– and use all remaining pages to hold ``block’’ of outer R.
– For each matching tuple r in R-block, s in S-page, add <r, s> to result
– Then read next R-block, scan S, etc.
Reserves
Sailors
1. Partition Phase
– partition R and S using the same hash function h
2. Probing Phase
– join tuples from the same partition (same h(..)
value) of R and S
– tuples in different partition of h will never join
– use a “different” hash function h2 for joining
these tuples
• (why different – see next slide first)
S R
Hash-Join Original
Relation OUTPUT Partitions
1
Cost of Hash-Join
• In partitioning phase
– read+write both relns; 2(M+N)
– In matching phase, read both relns; M+N I/Os
– remember – we are not counting final write
• Without grouping:
– In general, requires scanning the relation.
– Given index whose search key includes all attributes in the SELECT
or WHERE clauses, can do index-only scan
• With grouping:
– Sort on group-by attributes
– or, hash on group-by attributes
– can combine sort/hash and aggregate
– can do index-only scan here as well