您好,登錄后才能下訂單哦!
Oracle數據庫包含了如下基本內存組件
System global area (SGA)
The SGA is a group of shared memory structures, known as SGA components, that contain data and control information for one Oracle Database instance. The SGA is shared by all server and background processes. Examples of data stored in the SGA include cached data blocks and shared SQL areas.
Program global area (PGA)
A PGA is a nonshared memory region that contains data and control information exclusively for use by an Oracle process. The PGA is created by Oracle Database when an Oracle process is started.
One PGA exists for each server process and background process. The collection of individual PGAs is the total instance PGA, or instance PGA. Database initialization parameters set the size of the instance PGA, not individual PGAs.
User Global Area (UGA)
The UGA is memory associated with a user session.
Software code areas
Software code areas are portions of memory used to store code that is being run or can be run. Oracle Database code is stored in a software area that is typically at a different location from user programs—a more exclusive or protected location.
內存管理
Oracle依賴于內存相關的初始化參數來控制內存的管理。
內存管理有如下三個選項
Automatic memory management
You specify the target size for instance memory. The database instance automatically tunes to the target memory size, redistributing memory as needed between the SGA and the instance PGA.
Automatic shared memory management
This management mode is partially automated. You set a target size for the SGA and then have the option of setting an aggregate target size for the PGA or managing PGA work areas individually.
Manual memory management
Instead of setting the total memory size, you set many initialization parameters to manage components of the SGA and instance PGA individually.
UGA概覽
UGA是會話內存,用來保存會話變量例如登錄信息,已經數據庫會話需要的其他信息。
當PL/SQL包加載進內存時,UGA中包含了package state,也就是調用PL/SQL時指定的變量值。
PGA概覽
PGA緩沖區,則主要是為了某個用戶進程所服務的。這個內存區不是共享的,只有這個用戶的服務進程本身才能夠訪問它自己的PGA區。做個形象的比喻,SGA就好像是操作系統上的一個共享文件夾,不同用戶可以以此為平臺進行數據方面的交流。而PGA就好像是操作系統上的一個私有文件夾,只有這個文件夾的所有者才能夠進行訪問,其他用戶都不能夠訪問。雖然程序緩存區不向其他用戶的進程開放,但是這個內存區仍然肩負著一些重要的使命,如數據排序、權限控制等等都離不開這個內存區。
PGA組件
私有SQL區包含了綁定變量值和運行時期內存結構信息等數據。每一個運行SQL語句的會話都有一個塊私有SQL區。一個游標的私有SQL區又分為兩個生命周期不同的區:
永久區,包含綁定變量信息。當游標關閉時被釋放。
運行區,當執行結束時釋放。
Cursor
A cursor is a name or handle to a specific private SQL area
SGA包含如下組件
Database Buffer Cache
Redo Log Buffer
Shared Pool
Large Pool
Java Pool
Streams Pool
Fixed SGA
Buffer cache
Buffer Cache是SGA區中專門用于存放從數據文件中讀取的的數據塊拷貝的區域。Oracle進程如果發現需要訪問的數據塊已經在buffer cache中,就直接讀寫內存中的相應區域,而無需讀取數據文件,從而大大提高性能。Buffer cache對于所有oracle進程都是共享的,即能被所有oracle進程訪問。
Buffer的大小和數據塊一樣。
Buffer cache按照類型分為3個池
Default pool
This pool is the location where blocks are normally cached. Unless you manually configure separate pools, the default pool is the only buffer pool.
Keep pool
This pool is intended for blocks that were accessed frequently, but which aged out of the default pool because of lack of space. The goal of the keep buffer pool is to retain objects in memory, thus avoiding I/O operations.
Recycle pool
This pool is intended for blocks that are used infrequently. A recycle pool prevent objects from consuming unnecessary space in the cache.
Oracle還提供了非標準塊大小的buffer cache。如果你建立的表空間指定的塊大小為非數據庫塊大小,那么將使用這些buffer cache來緩存數據塊。
首先Oracle 以每個數據塊的文件號、塊號、類型做hash運算,得到hash值。
對于hash值相同的塊,放在一個Hash Bucket中。
因為buffer的大小畢竟有限,buffer中的數據塊需要根據一定的規則提出內存。
Oracle采用了LRU算法維護一個LRU鏈表,來決定哪些數據塊被淘汰。
通用的淘汰算法如下
Oracle改進了LRU算法,引入了Touch count概念、以及LRU鏈表分為熱端頭和冷端頭。
Touch count:
用來記錄數據塊訪問的頻繁度,此數值在內存中不受保護,多個進程可以同時修改它。這個值并不是精準的表示塊被訪問的次數,只是一種趨勢。3秒內無論多少用戶,訪問多少次塊。此值加1.
當數據塊第一次被放到buffer中,Oracle將其放置在冷端的頭部。
如果buffer已經沒有空閑空間,那么如何淘汰數據塊呢?Oracle從LRU的冷端尾部掃描數據塊,當發現數據塊的Touch count大于等于2時,將數據塊移動到熱端頭部,并將Touch count置為0 。當Oracle發現Touch count小于2時,則淘汰該數據塊。
當數據塊被修改了,我們把這個塊稱之為臟塊。臟塊在寫入磁盤前,是不會被踢出buffer的。
如果LRU中的臟塊比較多,每次申請新的空間時,都要掃描很多臟塊,但是又不能被淘汰。效率很低。
為此Oracle因為了臟LRU鏈表。專門用來記錄臟數據塊。
當塊被修改,并不會馬上從LRU鏈表中移動到LRUW中。只有當Oracle需要淘汰數據塊時,才會去掃描LRU鏈表,此時發現塊為臟塊,將數據塊移動到LRUW鏈表中。
檢查點鏈表
通過上面的描述,我們知道臟塊在LRU和LRUW鏈表中都有。那么當dbwr寫數據時,這兩個鏈表都要掃描。首先效率比較低,并且無法保證先修改的數據塊先被寫入磁盤。
為此Oracle引入了檢查點隊列,該隊列按照數據塊第一次被修改順序將臟塊鏈接到一起。
dbwr寫臟塊時,只需讀取檢查點隊列即可。
并且每個數據塊與記錄了日志條目的位置
redo log buffer
用戶進程將redo entries 拷貝到redo log buffer中。LGWR負責將其寫到磁盤中。
Redo entries contain the information necessary to reconstruct, or redo, changes made to the database by DML or DDL operations. Database recovery applies redo entries to data files to reconstruct lost changes.
共享池
Library Cache:
主要存放shared curosr(SQL)和PLSQL對象(function,procedure,trigger)的信息,以及這些對象所依賴的table,index,view等對象的信息。
Private SQL Areas與Shared SQL Area的關系
數據字典緩存
用來緩存系統數據字典表的內容,與普通表的緩存不同,普通表以塊為單位緩存到buffer cache中。而數據字典緩存以行為單位,緩存到shared pool中的data dictionary cache中。
Server result cache
用來緩存sql或者plsql的執行結果。
大池:
The large pool can provide large memory allocations for the following:
UGA for the shared server and the Oracle XA interface (used where transactions interact with multiple databases)
Message buffers used in the parallel execution of statements
Buffers for Recovery Manager (RMAN) I/O slaves
配置內存
Oracle提供了兩個初始化參數用來配置內存自動管理
MEMORY_TARGET:sga+pga內存之和,Oracle自動分配SGA和PGA的大小。
MEMORY_MAX_TARGET:MEMORY_TARGET可以設置大小的上限。
SGA自動內存管理
設置初始化參數SGA_TARGET為非0值,并且將STATISTICS_LEVEL的值設置為TYPICAL或者ALL.
PGA自動內存管理
PGA_AGGREGATE_TARGET設置為非0值。
如果workarea_size_policy為auto則sort_area_size,hash_area_size等參數設置被忽略,如果workarea_size_policy為manual,則sort_area_size,hash_area_size等參數設置生效。
也可以手工配置其他各個內存池的大小。當配置了內存自動管理時,有配置了具體池的大小,那么該配置為自動內存分配時的最小大小。
查看內存情況
The following views provide information about dynamic resize operations:
V$MEMORY_CURRENT_RESIZE_OPS
displays information about memory resize operations (both automatic and manual) which are currently in progress.
V$MEMORY_DYNAMIC_COMPONENTS
displays information about the current sizes of all dynamically tuned memory components, including the total sizes of the SGA and instance PGA.
V$MEMORY_RESIZE_OPS
displays information about the last 800 completed memory resize operations (both automatic and manual). This does not include in-progress operations.
V$MEMORY_TARGET_ADVICE
displays tuning advice for the MEMORY_TARGET
initialization parameter.
V$SGA_CURRENT_RESIZE_OPS
displays information about SGA resize operations that are currently in progress. An operation can be a grow or a shrink of a dynamic SGA component.
V$SGA_RESIZE_OPS
displays information about the last 800 completed SGA resize operations. This does not include any operations currently in progress.
V$SGA_DYNAMIC_COMPONENTS
displays information about the dynamic components in SGA. This view summarizes information based on all completed SGA resize operations that occurred after startup.
V$SGA_DYNAMIC_FREE_MEMORY
displays information about the amount of SGA memory available for future dynamic SGA resize operations.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。