buffer management & tuning cs5226 week 6. outline buffer management concepts & algorithms...

88
Buffer Management & Tuning CS5226 Week 6

Upload: kenneth-malin

Post on 31-Mar-2015

251 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Management & Tuning

CS5226 Week 6

Page 2: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Outline

• Buffer management concepts & algorithms

• Buffer tuning

Page 3: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Moore’s Law being proved...

1969 2001 Factor main memory 200 KB 200 MB 103

cache 20 KB 20 MB 103

cache pages 20 5000 <103

disk size 7.5 MB 20 GB 3*103 disk/memory size 40 100 -2.5 transfer rate 150 KB/s 15 MB/s 102 random access 50 ms 5 ms 10 scanning full disk 130 s 1300 s -10

Page 4: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Memory System

CPU

Registers

L1 Cache

CPU Die

L2 Cache

Main Memory

Harddisk

Page 5: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Memory Hierarchy

L1 Cache L2 Cache Main memoryBlock size 16--32 bytes 32--64 bytes 4--16 KBSize 16--64 KB 256KB—

8MBHit time 1 Clock Cycle 1—4 Clock

Cycles10—40 ClockCycles

Backing Store L2 Cache Main memory DiskBlockreplacement

Random Random Replacementstrategies

Miss penalty 4-20 clockcycles

40-200 clockcycles

~6M clockcycles

Page 6: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Time = Seek Time +Rotational Delay +Transfer Time +Other

Page 7: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Rule of Random I/O: ExpensiveThumb Sequential I/O: Much less

• Ex: 1 KB Block» Random I/O: 20 ms.» Sequential I/O: 1 ms.

Page 8: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Improving Access Time of Secondary Storage

• Organization of data on disk• Disk scheduling algorithms

– e.g., elevator algorithm

• Multiple disks• Mirrored disks• Prefetching and large-scale buffering

Page 9: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

DB Buffer vs OS Virtual Memory

• DBMS– More semantics to pages

• Pages are not all equal– More semantics to access patterns

• Queries are not all equal• Facilitates prefetching

– More concurrency on pages (sharing and correctness)– Pinning, forced writes

• Typical OS replacement policies not uniformly good – LRU, MRU, FIFO, LIFO, clock

Page 10: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Basic Concepts

• Data must be in RAM for DBMS to operate on it!• Table of <frame#, pageId> pairs is maintained.

DB

MAIN MEMORY

DISK

disk page

free frame

Page Requests from Higher Levels

BUFFER POOL

Choice of frame dictatedby replacement policy

Page 11: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

• Two variables maintained for each frame in buffer pool– Pin count

• Number of times page in frame has been requested but not released

• Number of current users of the page• Set to 0 initially

– Dirty bit • Indicates if page has been modified since it was brought

into the buffer pool from disk• Turned off initially

Basic Concepts

Page 12: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Why Pin a Page?

• Page is in use by a query/transaction• Log/recovery protocol enforced ordering• Page is hot and will be needed soon, e.g., root of

index trees.

Page 13: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

When a Page is Requested ...

• If requested page is not in pool:– Choose a frame for replacement

• If a free frame is not available, then choose a frame with pin count = 0

– All requestors of the page in frame have unpinned or released it

– If dirty bit is on, write page to disk– Read requested page into chosen frame– Pin the page (increment the pin count)

• A page in pool may be requested many times

– Return address of page to requestor

Page 14: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Management: Parameters

• What are the design parameters that distinguish one BM from another?– Buffer allocation: subdividing the pool

• Who owns a subdivision?– Global? Per query? Per relation?

• How many pages to allocate? (working set)

– Replacement policy• Which page to kick out when out of space?

– Load control• Determine how much load to handle

Page 15: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Replacement Policies

• Frame is chosen for replacement by a replacement policy:– Least-recently-used (LRU)– Most-recently-used (MRU)– First-In-First-Out (FIFO)– Clock / Circular order

• Policy can have big impact on number of I/Os– Depends on the access pattern.

Page 16: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Replacement Policies

• Least-recently-used (LRU)– Buffers not used for a long time are less likely to

be accessed– Rule: Throw out the block that has not been read

or written for the longest time.• Maintain a table to indicate the last time the block in each

buffer has been accessed.• Each database access makes an entry in table.

– Expensive ?

Page 17: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Replacement Policies

• First-In-First-Out (FIFO)– Rule: Empty buffer that has been occupied the

longest by the same block• Maintain a table to indicate the time a block is loaded into

the buffer.• Make an entry in table each time a block is read from

disk– Less maintenance than LRU– No need to modify table when block is accessed

Page 18: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Replacement Policies

• Clock Algorithm– Buffers arranged in a circle

• Each buffer associated with a Flag (0 or 1)• Flag set to 1 when

– A block is read into a buffer – Contents of a buffer is accessed

– A “hand” points to one of the buffers• Rotate clockwise to find a buffer with Flag=0• If it passes a buffer with Flag=1, set it to 0

01

0

11

0

1

0

Page 19: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Replacement Policies

• Clock Algorithm (cont’d)– Rule: Throw out a block from buffer if it remains

unaccessed when the hand• makes a complete rotation to set its flag to 0, and • another complete rotation to find the buffer with its 0

unchanged

01

0

11

0

1

0

Page 20: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

LRU-K

• Self-tuning– Approach the behavior of buffering algorithms in which pages sets with

known access frequencies are manually assigned to different buffer pools of specifically tuned sizes

– Does not rely on external hints about workload characteristics

– Adapts in real time to changing patterns of access

– Provably optimal

• Reference– E. O’Neil, P. O’Neil, G. Weikum: The LRU-K Page Replacement

Algorithm for Database Disk Buffering, SIGMOD 1993, pp. 297-306

Page 21: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Motivation

• GUESS when the page will be referenced again.– Problems with LRU?

Page 22: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Motivation

• GUESS when the page will be referenced again.– Problems with LRU?

• Makes decision based on too little info• Cannot tell between frequent/infrequent refs on time of last

reference• System spends resources to keep useless stuff around

Page 23: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Example 1

• CUSTOMER has 20k tuples• Clustered B+-tree on CUST_ID, 20 b/key• 4K page, 4000 bytes useful space• 100 leaf pages• Many users (random access)• References L1,R1,L2,R2,L3,R3,…• Probability to ref Li is .005, to ref Ri is .00005

Page 24: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

LRU-K Basic Concepts

• Ideas: Take into account history – last K references

(Classic LRU: K = 1 (LRU-1))

(keeps track of history, and try to predict)

Page 25: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Basic concepts

Parameters:• Pages N={1,2,…,n}

• Reference string r1,r2,…, rt, …

• rt = p for page p at time t

• bp = probability that rt+1=p

• Time between references of p: Ip = 1/bp

Page 26: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Algorithm

• Backward K-distance bt(p,K):– #refs from t back to the Kth most recent references to p

• bt(p,K) = INF if Kth ref doesn’t exist• Algorithm:

– Drop page p with max Backward K-distance bt(p,K)

• Ambiguous when infinite (use subsidiary policy, e.g., LRU)

• LRU-2 is better than LRU-1 – Why?

Page 27: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Problem 1

• Early page replacement– Page bt(p,K) is infinite, so drop

– What if it is a rare but “bursty” case?– What if there are Correlated References

• Intra-transaction, e.g., read tuple, followed by update

• Transaction Retry

• Intra-process, i.e., a process references page via 2 transactions, e.g., update RIDs 1-10, commit, update 11-20, commit, …

• Inter-process, i.e., two processes reference the same page independently

Page 28: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Example

• For example, assume (I) – read/update – Algorithm sees p (read)

– Drops it (infinite bt(p,K)) (wrong decision)

– Sees it again (update)– Keeps it around (wrong decision again)

Page 29: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Addressing Correlation

• Correlated Reference Period (CRP)– No penalty or credit for refs within CRP

– Ip: interval from end of one CRP to begin of the next

CRP

Ip

Page 30: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Problem 2

• Reference Retained Information– Algorithm needs to keep info for pages that may not

be resident anymore, e.g., LRU-2• P is referenced and comes in for the first time

• bt(p,2) = INF, p is dropped

• P is referenced again• If no info on p is retained, p may be dropped again

Page 31: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Solution to Problem 2

• Retained Information Period (RIP)– Period after which we drop information about page p– Upper bound: max Backward K-distance of all pages

we want to ensure to be memory resident

Page 32: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Data Structures for LRU-K

• HIST(p) – history control block of page p = Time of K most recent references to p - correlated

• LAST(p) – time of most recent ref to page p, correlated references OK

• Maintained for all pages p: bt(p,K) < RIP

Page 33: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

LRU-K Algorithm

If p is in the buffer { // update history of pif (t – LAST(p)) > CRP { // uncorrelated ref

// close correlated period and start newfor i = K-1 to 1

move HIST(p,i) into slot HIST(p,i+1)HIST(p,1) = t

}LAST(p) = t

}

Page 34: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

LRU-K Algorithm (Cont)

else { // select replacement victimmin = tfor all pages q in buffer {

if (t – LAST(p) > CRP // eligible for replacementand HIST(q,K) < min { // max Backward-Kvictim = qmin = HIST(q,K)

}if victim dirty, write back before dropping

Page 35: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

LRU-K Algorithm (Cont)

Fetch p into the victim’s bufferif no HIST(p) exists {

allocate HIST(p) for i = 2 to K HIST(p,i) = 0

} else {for i = 2 to K HIST(p,i) = HIST(p,i-1)

}HIST(p,1) = t // last non-correlated referenceLAST(p) = t // last reference

}

Page 36: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Example 2

• R has 1M tuples• A bunch of processes ref 5000 (0.5%) tuples• A few batch processes do sequential scans

Page 37: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Stochastic OS Replacement Policies

• Least recently used (LRU)• Most recently used (MRU)• First in first out (FIFO)• Last in first out (LIFO)• …• None takes into account DBMS access patterns

Page 38: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Domain Separation

Domain Buffer Pool

B-tree

Data

Hash index

STEAL

Page 39: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Domain Separation (2)

• Pages are classified into domains• LRU within domain• Domains are static• Pages belong to domains regardless of usage

– E.g. sequential scan versus nested loop

• No differentiation in importance of domains– E.g. Index page more useful than data page

• Does not prevent over-utilization of memory by multiple users; no notion of users/queries

• Need orthogonal load control

Page 40: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Group LRU

Domain Buffer Pool

B-tree

Data

Hash index

STEAL

Free list

Page 41: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Group LRU (2)

• Like Domain Separation• Prioritize domains• Steal buffer in order of priority• No convincing evidence that this is better than

LRU!

Page 42: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

“New” Algorithm in INGRES

• Each relation needs a working set– Buffer pool is subdivided and allocated on a per-relation basis– Each active relation is assigned a resident set which is initially

empty– The resident sets are linked in a priority list; unlikely reused

relations are near the top– Ordering of relation is pre-determined, and may be adjusted

subsequently– Search from top of the list– With each relation, use MRU

Page 43: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

“New” Algorithm

• Pros– A new approach that tracks the locality of a query

through relations

• Cons– MRU is not always good– How to determine priority (especially in multi-user

context)?– Costly search of list under high loads

Page 44: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hot-Set Model

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

• Hot set in memory implies efficient query processing

• #page faults vs size of buffers – points of discontinuities called hot points

Page 45: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hot Set (2)#

page

fau

lts

# buffers

Hot point(discontinuity in curve)

LRU

# pa

ge f

aults

# buffers

No hot point!

MRU

Page 46: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hot Set Model

• Key ideas– Give query |hot set| pages– Allow <=1 deficient query to execute– Hot set size computed by query optimizer (provides more

accurate reference pattern)– Use LRU within each partition

• Problems– LRU not always best and allocate more memory– Over-allocates pages for some phases of query

Page 47: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

DBMIN

• Based on “Query Locality Set Model”– DBMS supports a limited set of operations– Reference patterns exhibited are regular and predictable– Complex patterns can be decomposed into simple ones

• Reference pattern classification– Sequential – Random– Hierarchical

• Reference– Hong-Tai Chou, David J. DeWitt: An Evaluation of Buffer

Management Strategies for Relational Database Systems. VLDB 1985: 127-141

Page 48: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

DBMS Reference Patterns

• Straight sequential (SS)• Clustered sequential (CS)• Looping sequential (LS)• Independent random (IR)• Clustered random (CR)• Straight hierarchical (SH)• Hierarchical with straight sequential (H/SS)• Hierarchical with clustered sequential (H/CS)• Looping hierarchical (LH)

Page 49: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns

• Straight sequential (SS)– File scan without repetition

• E.g., selection on an unordered relation

– #pages?– Replacement algorithm?

R1R2R3R4R5R6

Table R

Page 50: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns

• Straight sequential (SS)– File scan without repetition

• E.g., selection on an unordered relation

– #pages? 1– Replacement algorithm?

R1R2R3R4R5R6

Table R

Page 51: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns

• Straight sequential (SS)– File scan without repetition

• E.g., selection on an unordered relation

– #pages? 1– Replacement algorithm?

Replaced with next one

R1R2R3R4R5R6

Table R

Page 52: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns (Cont)

• Clustered sequential (CS)– Like inner S for merge-join (sequential

with backup)– Local rescan in SS– Join condition: R.a = S.a

– #Pages?– Replacement algo?

444778

44477

4

Page 53: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns (Cont)

• Clustered sequential (CS)– Like inner S for merge-join (sequential

with backup)– Local rescan in SS– Join condition: R.a = S.a

– #Pages? #pages in largest cluster– Replacement algo?

444778

44477

4

Page 54: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns (Cont)

• Clustered sequential (CS)– Like inner S for merge-join (sequential

with backup)– Local rescan in SS– Join condition: R.a = S.a

– #Pages? #pages in largest cluster– Replacement algo? FIFO/LRU

444778

44477

4

Page 55: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns (Cont)

• Looping sequential (LS)– Sequential reference be repeated

several times • e.g., Like inner S for nested-loop-join

– #Pages?– Replacement algo?

444778

44477

4

Page 56: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns (Cont)

• Looping sequential (LS)– Sequential reference be repeated

several times • e.g., Like inner S for nested-loop-join

– #Pages? As many as possible– Replacement algo?

444778

44477

4

Page 57: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Sequential Patterns (Cont)

• Looping sequential (LS)– Sequential reference be repeated

several times • e.g., Like inner S for nested-loop-join

– #Pages? As many as possible– Replacement algo? MRU

444778

44477

4

Page 58: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Random Pattterns

• Independent Random (IR)– Genuinely random accesses

• e.g., non-clustered index scan

R1R2R3R4R5R6

Page 59: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Random Pattterns

• Independent Random (IR)– Genuinely random accesses

• e.g., non-clustered index scan

– One page (assuming low prob. of reaccesses)

R1R2R3R4R5R6

Page 60: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Random Pattterns

• Independent Random (IR)– Genuinely random accesses

• e.g., non-clustered index scan

– One page (assuming low prob. of reaccesses)

– Any replacement algorithm!

R1R2R3R4R5R6

Page 61: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Random Pattterns

• Clustered Random (CR)– Random accesses which

demonstrate locality• e.g., join with inner, non-clustered, non-

unique index on join columnR1R2R3R4R5R6

S2S3S4S5S6

S1

Page 62: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Random Pattterns

• Clustered Random (CR)– Random accesses which

demonstrate locality• e.g., join with inner, non-clustered, non-

unique index on join column

– #records in largest cluster

R1R2R3R4R5R6

S2S3S4S5S6

S1

Page 63: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Random Pattterns

• Clustered Random (CR)– Random accesses which

demonstrate locality• e.g., join with inner, non-clustered, non-

unique index on join column

– #records in largest cluster– As in CS

R1R2R3R4R5R6

S2S3S4S5S6

S1

Page 64: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hierarchical Pattterns

• Straight Hierarchical (SH)– Access index pages ONCE (retrieve

a single tuple)R1R2R3R4R5R6

Page 65: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hierarchical Pattterns

• Straight Hierarchical (SH)– Access index pages ONCE (retrieve

a single tuple)– Like SS

R1R2R3R4R5R6

Page 66: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hierarchical Pattterns

• Straight Hierarchical (SH)– Access index pages ONCE (retrieve

a single tuple)– Followed by straight sequential scan

(H/SS)• Like SS

R1R2R3R4R5R6

Page 67: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hierarchical Pattterns

• Straight Hierarchical (SH)– Access index pages ONCE (retrieve

a single tuple)– Followed by straight sequential scan

(H/SS)• Like SS

– Followed by clustered scan (H/CS)• Like CS

R1R2R3R4R5R6

Page 68: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hierarchical Pattterns

• Looping Hierarchical (LH)– Repeatedly traverse an index, e.g., when inner

index in join is repeatedly accessed

Page 69: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Hierarchical Pattterns

• Looping Hierarchical (LH)– Repeatedly traverse an index, e.g., when inner

index in join is repeatedly accessed– Size is height of tree– LIFO need to keep the root

Page 70: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

DBMIN

• A buffer management algorithm based on QLSM– Buffers allocated on a per-file instance basis

• Active instances of same file have different BPs

• Those are independently managed

• May share a same buffered page through global table

– Each file instance has its locality set (lset) of pages– Manage each lset by the access pattern for that file– Each page in buffer belongs to at most 1 lset– Global, shared table of buffers too

Page 71: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

What’s Implemented in DBMS?

• DB2 & Sybase ASE– Named pools to be bound to tables or indexes– Each pool can be configured to use clock replacement or LRU

(ASE)– Client can indicate pages to replace

• Oracle– A table can be bound to 1 to 2 pools, one with higher priority to

be kept

• Others– Global pools with simple policies

Page 72: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Summary

Algorithms• LRU, MRU, LIFO, …• Domain separation

(assign pages to domain)• Group LRU (prioritize

domains)• NEW (resident set per

relation)• Hot set (per query)• DBMIN (locality set per

file instance)

DBMS reference patterns• Sequential

– Straight Sequential– Clustered Sequential– Looping Sequential

• Random– Independent Random– Clustered Random

• Hierarchical– Straight Hierarchical– With Straight Sequential– With Clustered Sequential– Looping Hierarchical

Page 73: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Tuning

• DBMS buffer tuning (Oracle 9i)

Page 74: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Database Buffers

Application buffers

DBMS buffers

OS buffers

•An application can have its own in-memory buffers (e.g., variables in the program; cursors);

•A logical read/write will be issued to the DBMS if the data needs to be read/written to the DBMS;

•A physical read/write is issued by the DBMS using its systematic page replacement algorithm. And such a request is passed to the OS.

•OS may initiate IO operations to support the virtual memory the DBMS buffer is built on.

Page 75: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Database Buffer Size

• Buffer too small, then hit ratio too small

hit ratio = (logical acc. - physical acc.) / (logical acc.)

• Buffer too large, wasteful at the expense of others

• Recommended strategy: monitor hit ratio and increase buffer size until hit ratio flattens out. If there is still paging, then buy memory.

LOG DATADATA

RAM

Paging Disk

DATABASE PROCESSES

DATABASEBUFFER

Page 76: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Overall Cache Hit Ratio

• Cache hit ratio = (# logical read - # physical read) / # logical read

• Ideally, hit ratio > 80%

• Overall buffer cache hit ratio for entire instance

SELECT (P1.value + P2.value - P3.value) / (P1.value + P2.value)FROM v$sysstat P1, v$sysstat P2, v$sysstat P3WHERE P1.name = 'db block gets‘AND P2.name = 'consistent gets‘AND P3.name = 'physical reads'

Page 77: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Session Cache Hit Ratio

• Buffer cache hit ratio for one specific session

SELECT (P1.value + P2.value - P3.value) / (P1.value + P2.value)FROM v$sesstat P1, v$statname N1, v$sesstat P2, v$statname N2, v$sesstat P3, v$statname N3WHERE N1.name = 'db block gets‘AND P1.statistic# = N1.statistic#AND P1.sid = <enter SID of session here>AND N2.name = 'consistent gets‘AND P2.statistic# = N2.statistic#AND P2.sid = P1.sidAND N3.name = 'physical reads‘AND P3.statistic# = N3.statistic#AND P3.sid = P1.sid

Page 78: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Adjust Buffer Cache Size

• Buffer size = db_block_buffers * db_block_size• db_block_size is set at database creation;

cannot tune• Change the db_block_buffers parameter

Page 79: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Should Buffer Cache Be Larger?

• Set db_block_lru_extended_statistics to 1000

• Incurs overhead! Set back to 0 when done

SELECT 250 * TRUNC (rownum / 250) + 1 || ' to ' || 250 * (TRUNC (rownum / 250) + 1) "Interval", SUM (count) "Buffer Cache Hits“FROM v$recent_bucketGROUP BY TRUNC (rownum / 250)

Interval Buffer Cache Hits--------------- -----------------------1 to 250 16083251 to 500 11422501 to 750 683751 to 1000 177

Page 80: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Should Buffer Cache Be Smaller?

• Set db_block_lru_statistics to true

SELECT 1000 * TRUNC (rownum / 1000) + 1 || ' to ' || 1000 * (TRUNC (rownum / 1000) + 1) "Interval", SUM (count) "Buffer Cache Hits“FROM v$current_bucketWHERE rownum > 0GROUP BY TRUNC (rownum / 1000)

Interval Buffer Cache Hits------------ -----------------------1 to 1000 6684151001 to 2000 2817602001 to 3000 1669403001 to 4000 147704001 to 5000 70305001 to 6000 959

Page 81: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

I/O Intensive SQL Statements

• v$sqlarea contains one row for each SQL statement currently in the system global area

• Executions: # times the statement has been executed since entering SGA

• Buffer_gets: total # logical reads by all executions of the statement

• Disk_reads: total # physical reads by all executions of the statement

SELECT executions, buffer_gets, disk_reads, first_load_time, sql_textFROM v$sqlareaORDER BY disk_reads

Page 82: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Swapping of Data Pages

• Monitoring tools: sar or vmstat• If system is swapping

– Remove unnecessary system daemons and applications

– Decrease number of database buffers– Decrease number of UNIX file buffers

Page 83: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Paging of Program Blocks

• Monitoring tools: sar or vmstat • To reduce paging

– Install more memory– Move some programs to another machine– Configure SGA to use less memory

• Compare paging activities during fast versus slow response

Page 84: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

SAR – Monitoring Tool

• vmstat –S 5 8

procs memory page disk faults cpu r b w swap free si so pi po fr de sr f0 s0 s1 s3 in sy cs us sy id 0 0 0 1892 5864 0 0 0 0 0 0 0 0 0 0 0 90 74 24 0 0 99 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 0 46 25 21 0 0 100 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 0 47 20 18 0 0 100 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 2 53 22 20 0 0 100 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 0 87 23 21 0 0 100 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 0 48 41 23 0 0 100 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 0 44 20 18 0 0 100 0 0 0 85356 8372 0 0 0 0 0 0 0 0 0 0 0 51 71 24 0 0 100

1 = swapped out processes

# swap-in,swap-outper sec

# page-in,page-outper sec

Page 85: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Size - Data

Settings:employees(ssnum, name, lat, long, hundreds1,

hundreds2);

clustered index c on employees(lat); (unused)– 10 distinct values of lat and long, 100 distinct values of hundreds1 and

hundreds2

– 20000000 rows (630 Mb);

– Warm Buffer

– Dual Xeon (550MHz,512Kb), 1Gb RAM, Internal RAID controller from Adaptec (80Mb), 4x18Gb drives (10000 RPM), Windows 2000.

Page 86: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Buffer Size - Queries

Queries:– Scan Query

select sum(long) from employees;

– Multipoint queryselect * from employees where lat = ?;

Page 87: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Database Buffer Size

• SQL Server 7 on Windows 2000

• Scan query:– LRU (least recently used) does

badly when table spills to disk as Stonebraker observed 20 years ago.

• Multipoint query:– Throughput increases with buffer

size until all data is accessed from RAM.

Multipoint Query

0

40

80

120

160

0 200 400 600 800 1000

Buffer Size (Mb)

Th

rou

gh

pu

t (Q

uer

ies/

sec)

Scan Query

0

0.02

0.04

0.06

0.08

0.1

0 200 400 600 800 1000

Buffer Size (Mb)

Th

rou

gh

pu

t (Q

ue

rie

s/s

ec

)

Page 88: Buffer Management & Tuning CS5226 Week 6. Outline Buffer management concepts & algorithms Buffer tuning

Summary

• Monitor cache hit ratio• Increase/reduce buffer cache size• Pay attention to I/O intensive SQL statements• Avoid swapping• Check for excessive paging