My idea about the organization about block buffer cache

来源:互联网 发布:电子音乐贺卡制作软件 编辑:程序博客网 时间:2024/05/20 21:20

Organization of the Database Buffer Cache
    The buffers in the cache are organized in two lists: the write list and the least
recently used (LRU) list. The write list holds dirty buffers, which contain data that
has been modified but has not yet been written to disk. The LRU list holds free
buffers, pinned buffers, and dirty buffers that have not yet been moved to the write
list. Free buffers do not contain any useful data and are available for use. Pinned
buffers are currently being accessed.

    When an Oracle process accesses a buffer, the process moves the buffer to the most
recently used (MRU) end of the LRU list. As more buffers are continually moved to
the MRU end of the LRU list, dirty buffers age toward the LRU end of the LRU list.

    The first time an Oracle user process requires a particular piece of data, it searches
for the data in the database buffer cache. If the process finds the data already in the
cache (a cache hit), it can read the data directly from memory. If the process cannot
find the data in the cache (a cache miss), it must copy the data block from a datafile
on disk into a buffer in the cache before accessing the data. Accessing data through
a cache hit is faster than data access through a cache miss.

    Before reading a data block into the cache, the process must first find a free buffer.
The process searches the LRU list, starting at the least recently used end of the list.
The process searches either until it finds a free buffer or until it has searched the
threshold limit of buffers.

    If the user process finds a dirty buffer as it searches the LRU list, it moves that
buffer to the write list and continues to search.When the process finds a free buffer,
it reads the data block from disk into the buffer and moves the buffer to the MRU
end of the LRU list.

    If an Oracle user process searches the threshold limit of buffers without finding a
free buffer, the process stops searching the LRU list and signals the DBW0
background process to write some of the dirty buffers to disk.

*****************************************************************************************
My summary:

    The first time an Oracle user process requires a particular piece of data, it searches
for the data in the database buffer cache(begin with the MRU end of the list i think):

if (found the buffer including the data block need) then
  user process moves the block to the MRU end of the LRU list;  --cause a cache hits
else  --cause a cache misses
  user process find the data block in the data files;
  user process search from the LRU end of LRU list for a free buffer:
 
  for buffers in every buffers searched
  (
    if (found a free buffer) then
      the user process put the data blocks into the free buffer, put the buffer to the MRU end of LRU list;               
    elseif (found a dirty buffer) then
      put the found dirty buffer to the write list,continue search the free buffer;
    elseif (not found or reach the threshold limit of the buffers) then
      send a signal to DBWn , DBWn write some dirty buffers to the data files;
    end if
  )
end if
*****************************************************************************************

The LRU Algorithm and Full Table Scans
    When the user process is performing a full table scan, it reads the blocks of the table
into buffers and puts them on the LRU end (instead of the MRU end) of the LRU
list. This is because a fully scanned table usually is needed only briefly, so the blocks
should be moved out quickly to leave more frequently used blocks in the cache.

    You can control this default behavior of blocks involved in table scans on a
table-by-table basis. To specify that blocks of the table are to be placed at the MRU
end of the list during a full table scan, use the CACHE clause when creating or
altering a table or cluster. You can specify this behavior for small lookup tables or
large static historical tables to avoid I/O on subsequent accesses of the table.


    从这里我们可以看出:当FTS的时候,一般情况data block放在LRU end of the LRU list,这是因为用于FTS的block一般来说再次使用的频率比较低;然而FTS出来的data block如果使用的频率很高的话,我们需要把它放到MRU end of LRU list,就可以在修改表的子句,即alter table table_name cache;

原创粉丝点击