Anda di halaman 1dari 10

Buffer Management

for Database Systems

Ramakrishnan&Gehrke: Chap. 9.4


Chou & DeWitt, VLDB’85, 127-141
O’Neil et al., SIGMOD’93, 297-306
Johnson & Shasha, VLDB’94, 439-450

Database Systems Implementation, Bongki Moon 1

Buffer Management in a DBMS


Page Requests from Higher Levels

BUFFER POOL

disk page

free frame

MAIN MEMORY

DISK choice of frame dictated


DB by replacement policy

 Data must be in RAM for DBMS to operate on it!


 Need a frame#-to-pageid mapping. For example, hashing.
Database Systems Implementation, Bongki Moon 2

1
When a Page is Requested...

 If requested page is not in pool:


– Choose a frame (buffer page) with pin_count==0 for replacement.
– If the frame is dirty, write it to disk.
– Read the requested disk page into the chosen frame.
 Pin the page and return its address.
– To prevent a page in use from being evicted.
– pin_count++
 It may be that pin_count > 1, because a buffer page may be
requested many times by different processes or Xacts.
 When requestor is done with the page,
– it must unpin the page (pin_count--),
– indicate whether the page has been modified (dirty bit).
 CC & Recovery may entail additional I/O.

Database Systems Implementation, Bongki Moon 3

Buffer Replacement Policy


 Frame is chosen for replacement by a replacement policy:
– LRU, Clock (LRU approx), MRU, etc.
 Policy can have big impact on # of I/O’s; depends on the
access pattern.
 Sequential flooding: Nasty situation caused by LRU +
repeated (circular) sequential scans.
– For example, assume # buffer frames=3, and disk pages requested
are 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5.
– When cold start, what is the # of page faults if LRU or MRU is used?
– # buffer frames < # pages requested means each page request
causes a page fault (i.e., I/O). MRU can be much better in this
situation.
– The circular sequential scan is a very common database operation.

Database Systems Implementation, Bongki Moon 4

2
Stochastic vs. Non-Stochastic

 Stochastic replacement: LRU, MRU, FIFO, etc.


– None of them are appropriate for DBMS.
 Non-Stochastic replacement algorithms
– Reiter’s Domain separation (DS) [1976]
– Kaplan’s New algorithm [1980]
– Sacco & Schkolnick’s Hot Set model [1982]
– Chou & DeWitt’s DBMIN [1985]
 Improved stochastic algorithms
– O’Neil’s LRU-K algorithm [1993] and Johnson’s 2Q [1994]

Database Systems Implementation, Bongki Moon 5

Hot Set Model

 Hot set: a set of pages over which there is a looping


behavior.
– (E.g.) A nested-loop join often requires circular sequential scans.
 Hot point: number of buffers where discontinuities are
observed in buffer# vs. page faults plots.
– For example, pages requested: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 …
– The hot set size is 5.
– To determine the hot point for LRU and MRU, compute #page
faults with a varying number of buffer pages (3, 4, 5, 6).
 Admission control: A new query is allowed only if its hot
set size does not exceed the available buffer space.

Database Systems Implementation, Bongki Moon 6

3
DBMIN Algorithm

 Common database operations yield a composition of a set


of simple, regular and predictable reference patterns.
– (e.g.) circular sequential for nested-loop joins, clustered sequential
for merge joins.
 Elaborate model based on query behaviors
– Buffers are allocated and managed on per-file basis.
– Each file is given a buffer pool to hold its locality set.
– DB2 allows buffers to be partitioned. Each table can be bound to one of
these pools.
 DBMIN algorithm uses different replacement policies for
different reference patterns.
– Query locality set model (QLSM) classifies page reference patterns.
– QLSM patterns: SS, CS, LS, IR, CR, SH, LH
Database Systems Implementation, Bongki Moon 7

QLSM: Examples

 Looping sequential (LS)


– Operations: nested-loop join
– Locality set is the entire file (inner relation). MRU is best.
 Clustered random (CR)
– Operations: an inner relation with non-clustered, non-unique
index, an outer relation is clustered with non-unique keys.
– Locality set size is the number of records in the largest cluster.
– FIFO or LRU replacement.
 Straight hierarchical (SH)
– Operations: tree traversal from root to a leaf (B+-tree).
– Locality set size is one.

Database Systems Implementation, Bongki Moon 8

4
DBMIN Replacement

 Determine a replacement policy and locality set size on per-


file basis.
 On a file open, its locality set is set empty.
 On a file close, buffers in the LS are returned to the global
free list.
 On a page request,
– If it’s in the locality set, update statistics (for replacement policy).
– If it’s in the global free list, add to the LS. If the LS gets bigger than
max, a page is returned to the global free.
– If it’s not in memory, allocate a buffer from the global free list or LS.
 Admission control: A new query is allowed only if buffers
are enough.

Database Systems Implementation, Bongki Moon 9

Oracle: Multiple Buffer Pools

 Oracle 8 introduced multiple buffer pools.


– Segregation by usage reduces I/O.
– Each pool uses LRU replacement (LRU & LRUW lists).
 KEEP pool holds hot blocks (such as indexes).
 RECYCLE pool holds transient data (such as fully
scanned tables).
– Prevents objects from consuming space in cache.
 DEFAULT pool stores the rest of objects.

Database Systems Implementation, Bongki Moon 10

5
Oracle: LRU List and Write List

 Write (LRUW) list holds dirty pages.


 LRU list holds free pages, pinned pages and dirty pages
not yet moved to the Write list.
 Upon a cache miss, find a free buffer page
– Searches the LRU list until a free page is found or a certain
threshold limit of pages are searched.
– If a dirty page is encountered, move it to the Write list.
– If the threshold is hit, signal the DBW0 background process to
flush some dirty pages to disk.
 MRU policy for full table scans
– Blocks read from a table are placed at the LRU end (instead of
MRU end) of the LRU list.
– Control by the CACHE clause when creating a table.
Database Systems Implementation, Bongki Moon 11

LRU-K Algorithm

 LRU-K is an improved stochastic algorithm [1993].


 What’s wrong with LRU?
– Do not consider frequency of references.
– (E.g.) random accesses through a (dense) B-tree index:
Page size is 4KB, 100 buffer pages in the pool.
Table: 20K records of 2KB each, 2 records/page, 10,000 pages.
B-tree: 20 bytes/entry, 200 entries/node, 100 leaf nodes, depth is 2, root
is pinned.
Reference will alternate: B T B T B T …
Access probability: an index page (1/200), a data page (1/20000)
– Nonetheless, LRU will keep 50 index pages and 50 data pages in
memory!! Is this the best case or worst case for LRU?
 Idea: Use inter-arrival time instead of recentness.

Database Systems Implementation, Bongki Moon 12

6
LRU-K: Time-out Correlation

 Correlated references may be misleading


– E.g., intra-transaction (read and update a page), transaction-
retry (abort and redo), intra-process.
– Wrong if use these pairs to estimate inter-arrival time.
 Successive accesses by the same process within a time-
out period are considered correlated.
 Inter-arrival time is from the end of a correlated
reference period to the beginning of the next one.
 Aka. Factoring out Locality.

Database Systems Implementation, Bongki Moon 13

LRU-K: Reference Retention

 Reference retention problem


– We need to keep track of inter-arrival time even for pages not in
memory. Otherwise, the inter-arrival time for a newly loaded
page will always be set to infinity and selected as a replacement
victim.
– If we store the inter-arrival time in the page header, I/O will
increase too much. Why?
 Maintain reference information for a retained information
period after the most recent access.
 Jim Gray’s Five Minute Rule as a guideline.
– Gray & Graefe, SIGMOD Record, 26(4), 1997

Database Systems Implementation, Bongki Moon 14

7
LRU-K: Buffer Replacement

 When a page p referenced is in memory, update data


structures:
– HIST(p,i) : the time of the i-th most recent references, discounting
correlated references (1 ≤ i ≤ K).
– LAST(p) : the time of the most recent reference, regardless of
correlation.
 Maintain the information for all pages with Backward K-
distance smaller than the retained information period.
– Backward k-distance bt(p,K) = current_time - Hist(p,K).
 When a page referenced is not in memory, a buffer page p
is selected as a replacement victim if
– current_time - LAST(p) > Correlated Reference Period
– HIST(p,K) is minimal (i.e., oldest), or bt(p,K) is maximal.
Database Systems Implementation, Bongki Moon 15

2Q Algorithm [VLDB’1994]

 2Q: a low-overhead version of LRU-2


– Maintain 2 queues (Am and A1in) for cached pages, plus another
queue (A1out) to remember recently evicted pages.
– Correlated references are common.
 A page is referenced many times for a short period of time.
 When a new page is accessed, it should be retained for a period of time
to satisfy the correlated references (i.e., RIP).
– Admit a newly accessed page to the A1in queue.
 When it is evicted, it is given a second chance by remembering its id in
A1out, because there may be correlated references coming up soon.
– A page is admitted to the main queue Am, only when it is revived
from the A1out queue.
 Because this page has been accessed at least twice but not by correlated
references.

Database Systems Implementation, Bongki Moon 16

8
2Q Buffer Replacement

 On accessing a page P
If (P is in Am) then move P to the head of Am
else if (P is in A1out) then reclaim(P); add P to head(Am)
else if (P is in A1in) then do nothing (for a correlated reference)
else reclaim(P); add P to the head of A1in (P is a new page accessed)
 Reclaim(P)
if (there is ANY free slot) then put P into the slot
else if (A1in is full)
remove the tail of A1in; add its id to the head of A1out
if (A1out is full) remove the tail of A1out
put P into the page slot freed from A1in
else remove the tail of Am; put P into the page slot freed from Am

Database Systems Implementation, Bongki Moon 17

More Notes on 2Q Buffers

 2+1 Queues
– Am is the main LRU queue.
– A1in is a FIFO queue that stores newly referenced pages.
– A1out is a FIFO queue that stores page ids recently evicted
from A1in.
 Partitioning of Buffer Pool
– A given buffer pool is divided into 2 queues of pages and 1
queue of page ids.
– The ratio between Am and A1in is a major tuning parameter.

Database Systems Implementation, Bongki Moon 18

9
Experimental Evaluation

Database Systems Implementation, Bongki Moon 19

10

Anda mungkin juga menyukai