440 likes | 1.17k Views
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: rarely allocated in advance
E N D
Chapter 15.7Buffer 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: • rarely allocated in advance • 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.
The role of the buffer manager Requests Read/Writes Buffers Buffer manager Figure 1: The role of the buffer manager : responds to requests for main-memory access to disk blocks
15.7.1 Buffer Management Architecture • Two broad architectures for a buffer manager: • The buffer manager controls main memory directly. • Relational DBMS • The buffer manager allocates buffers in virtual memory, allowing the OS to decide how to use buffers. • “main-memory” DBMS • “object-oriented” DBMS
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-sizebuffer pool, a set of buffers available to queries and other database actions.
DB Buffer Pool Page Requests from Higher Levels • Data must be in RAM for DBMS to operate on it! • Buffer Manager hides the fact that not all data is in RAM. BUFFER POOL disk page free frame MAIN MEMORY DISK choice of frame dictated by replacement policy
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?
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.
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
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
Buffer-replacement strategy – “Clock” 0 0 1 0 0 the buffer with a 0 flag will be replaced Start point to search a 0 flag 0 1 The flag will be set to 0 1 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
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.
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.
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.
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)?
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
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?
Example CASE1: NO • Buffer-replacement strategy: LRU • Buffers for R: k • We 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.
Example CASE 2: YES • Buffer-replacement strategy: LRU • Buffers for R: k • We 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.
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.