chapter 15.7 buffer management id: 219 name: qun yu class: cs257 219 spring 2009 instructor: dr....

21
Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

Post on 22-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

Chapter 15.7Buffer Management

ID: 219Name: Qun YuClass: CS257 219 Spring 2009Instructor: Dr. T.Y.Lin

Page 2: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

What does a buffer manager do?

Assume there are M of main-memory buffers needed for the operators on relations to store needed data.

In practice: 1) rarely allocated in advance2) the value of M may vary depending on system

conditions Therefore, buffer manager is used to allow processes

to get the memory they need, while minimizing the delay and unclassifiable requests.

Page 3: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

Buffer manager

Buffers

RequestsRead/Writes

 

                 

Figure 1: The role of the buffer manager : responds to requests for main-memory access to disk blocks

The role of the buffer manager

Page 4: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 15.7.1 Buffer Management Architecture

Two broad architectures for a buffer manager:

1) The buffer manager controls main memory directly. • Relational DBMS

2) The buffer manager allocates buffers in virtual memory, allowing the OS to decide how to use buffers. • “main-memory” DBMS • “object-oriented” DBMS

Page 5: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

Buffer Pool

Key setting for the Buffer manager to be efficient:

The buffer manager should limit the number of buffers in use so that they fit in the available main memory, i.e. Don’t exceed available space.

The number of buffers is a parameter set when the DBMS is initialized.

No matter which architecture of buffering is used, we simply assume that there is a fixed-size buffer pool, a set of buffers available to queries and other database actions.

Page 6: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

Data must be in RAM for DBMS to operate on it! Buffer Manager hides the fact that not all data is in RAM.

DB

MAIN MEMORY

DISK

disk page

free frame

Page Requests from Higher Levels

BUFFER POOL

choice of frame dictatedby replacement policy

Buffer Pool

Page 7: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 15.7.2 Buffer Management Strategies

Buffer-replacement strategies:

When a buffer is needed for a newly requested block and the buffer pool is full, what block to throw out the buffer pool?

Page 8: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 Buffer-replacement strategy -- LRU

Least-Recently Used (LRU):

To throw out the block that has not been read or written for the longest time.

• Requires more maintenance but it is effective. • Update the time table for every access.• Least-Recently Used blocks are usually less likely to

be accessed sooner than other blocks.

Page 9: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 Buffer-replacement strategy -- FIFO

First-In-First-Out (FIFO):

The buffer that has been occupied the longest by the same block is emptied and used for the new block.

• Requires less maintenance but it can make more mistakes.• Keep only the loading time• The oldest block doesn’t mean it is less likely to be

accessed. Example: the root block of a B-tree index

Page 10: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 Buffer-replacement strategy – “Clock”

The “Clock” Algorithm (“Second Chance”)

Think of the 8 buffers as arranged in a circle, shown as Figure 3

 

Flag 0 and 1:

buffers with a 0 flag are ok to sent their contents back to disk, i.e. ok to be replaced

buffers with a 1 flag are not ok to be replaced

Page 11: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 Buffer-replacement strategy – “Clock”

 

0

0

1

1

1

0

0

0

Start point to search a 0 flag

the buffer with a 0 flag will be replaced

The flag will be set to 0

By next time the hand reaches it, if the content of this buffer is not accessed, i.e. flag=0, this buffer will be replaced.That’s “Second Chance”.

 

Figure 3: the clock algorithm

Page 12: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 Buffer-replacement strategy -- Clock

 

a buffer’s flag set to 1 when:a block is read into a buffer

the contents of the buffer is accessed

a buffer’s flag set to 0 when:the buffer manager needs a buffer for a new block, it looks for the first 0 it can find, rotating clockwise. If it passes 1’s, it sets them to 0.

Page 13: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

  System Control helps Buffer-replacement strategy

 

System Control

The query processor or other components of a DBMS can give advice to the buffer manager in order to avoid some of the mistakes that would occur with a strict policy such as LRU,FIFO or Clock.

For example:

A “pinned” block means it can’t be moved to disk without first modifying certain other blocks that point to it.

In FIFO, use “pinned” to force root of a B-tree to remain in memory at all times.

Page 14: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

15.7.3 The Relationship Between Physical Operator Selection and Buffer Management

 

Problem:

Physical Operator expected certain number of buffers M for execution.

However, the buffer manager may not be able to guarantee these M buffers are available.

Page 15: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

15.7.3 The Relationship Between Physical Operator Selection and Buffer Management

 

Questions:

Can the algorithm adapt to changes of M, the number of main-memory buffers available?

When available buffers are less than M, and some blocks have to be put in disk instead of in memory.

How the buffer-replacement strategy impact the performance (i.e. the number of additional I/O’s)?

Page 16: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

Example

 

FOR each chunk of M-1 blocks of S DO BEGIN

read these blocks into main-memory buffers;

organize their tuples into a search structure whose

search key is the common attributes of R and S;

FOR each block b of R DO BEGIN

read b into main memory;

FOR each tuple t of b DO BEGIN

find the tuples of S in main memory that

join with t ;

output the join of t with each of these tuples;

END ;

END ;

END ;

 

Figure 15.8: The nested-loop join algorithm

Page 17: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

Example

 

The outer loop number (M-1) depends on the average number of buffers are available at each iteration.

The outer loop use M-1 buffers and 1 is reserved for a block of R, the relation of the inner loop.

If we pin the M-1 blocks we use for S on one iteration of the outer loop, we shall not lose their buffers during the round.

Also, more buffers may become available and then we could keep more than one block of R in memory.

Will these extra buffers improve the running time?

Page 18: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

Example

 

CASE1: NO

Buffer-replacement strategy: LRUBuffers for R: kWe read each block of R in order into buffers.By end of the iteration of the outer loop, the last k blocks of R are in buffers.However, next iteration will start from the beginning of R again. Therefore, the k buffers for R will need to be replaced.

Page 19: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

Example

 

CASE 2: YES

Buffer-replacement strategy: LRUBuffers for R: kWe read the blocks of R in an order that alternates: firstlast and then lastfirst.In this way, we save k disk I/Os on each iteration of the outer loop except the first iteration.

Page 20: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

 

Other Algorithms and M buffers

 

Other Algorithms also are impact by M and the buffer-replacement strategy. Sort-based algorithm

If M shrinks, we can change the size of a sublist.

Unexpected result: too many sublists to allocate each sublist a buffer. Hash-based algorithm

If M shrinks, we can reduce the number of buckets, as long as the buckets still can fit in M buffers.

Page 21: Chapter 15.7 Buffer Management ID: 219 Name: Qun Yu Class: CS257 219 Spring 2009 Instructor: Dr. T.Y.Lin

THANK YOU !