Oracle Performance Tuning 11g2 (7-1)

yuntui發表於2016-11-03

7 Configuring and Using Memory

This chapter explains how to allocate memory to Oracle Database memory caches, and how to use those caches. Proper sizing and effective use of the Oracle Database memory caches greatly improves database performance. Oracle recommends using automatic memory management to manage the memory on your system. However, you can choose to manually adjust the memory pools on your system, as described in this chapter.

這節解釋怎樣去分配記憶體到資料庫記憶體快取,以及如何使用這些快取。正確的調整大小和高效使用快取將會極大的提升系統效能。建議使用自動記憶體管理。但是你可以像本節中介紹的去手動調整記憶體池。

This chapter contains the following sections:

  • Understanding Memory Allocation Issues                 理解記憶體分配問題

  • Configuring and Using the Buffer Cache                 配置和使用快取

  • Configuring and Using the Shared Pool and Large Pool   配置使用共享池和大池

  • Configuring and Using the Redo Log Buffer              配置使用redo

  • PGA Memory Management                                  PGA記憶體管理

  • Managing the Server and Client Result Caches           管理服務端和客戶端的result cache

7.1 Understanding Memory Allocation Issues

Oracle Database stores information in memory caches and on disk. Memory access is much faster than disk access. Disk access (physical I/O) take a significant amount of time, compared with memory access, typically in the order of 10 milliseconds. Physical I/O also increases the CPU resources required, because of the path length in device drivers and operating system event schedulers. For this reason, it is more efficient for data requests of frequently accessed objects to be perform by memory, rather than also requiring disk access.

A performance goal is to reduce the physical I/O overhead as much as possible, either by making it more likely that the required data is in memory, or by making the process of retrieving the required data more efficient.

oracle將資訊儲存在記憶體快取和磁碟上。訪問記憶體要比訪問磁碟快的多。與訪問記憶體相比,訪問磁碟(IO)會消耗了大量的時間,通常需要在10毫秒左右。物理IO同樣也增加了CPU資源的佔用,因為裝置驅動的路徑長度和作業系統事件排程(這裡不太理解)。基於這個原因,將頻繁訪問的資料放在記憶體中執行將會非常高效,而不是請求磁碟訪問。

This section contains the following topics:

  • Oracle Memory Caches                 記憶體快取

  •           AMM管理記憶體

  • Automatic Shared Memory Management   ASMM管理記憶體

  • Dynamically Changing Cache Sizes     動態的改變快取大小

  •            應用考慮

  • Operating System Memory Use          作業系統記憶體使用

  • Iteration During Configuration       迭代配置

7.1.1 Oracle Memory Caches

The main Oracle Database memory caches that affect performance are:  影響效能的快取有這個:

  • Shared pool

  • Large pool

  • Java pool

  • Buffer cache

  • Streams pool size

  • Log buffer

  • Process-private memory, such as memory used for sorting and hash joins

7.1.2 Automatic Memory Management

Oracle strongly recommends the use of automatic memory management to manage the memory on your system. Automatic memory management enables Oracle Database to automatically manage and tune the instance memory. Automatic memory management can be configured using a target memory size initialization parameter (MEMORY_TARGET) and a maximum memory size initialization parameter (MEMORY_MAX_TARGET). The database tunes to the target memory size, redistributing memory as needed between the system global area (SGA) and the instance program global area (instance PGA). Before setting any memory pool sizes, consider using the automatic memory management feature of Oracle Database. If you must configure memory allocations, consider using the Memory Advisor for managing memory.

oracle強烈的建議你使用AMM管理記憶體(自動記憶體管理)。AMM允許資料庫自動管理和調優例項記憶體。AMM透過memory_target,memory_max_target引數進行配置。資料庫調優目標記憶體大小,在SGAPGA間按需進行記憶體分佈(透過粒度及MMAP技術實現的)。在設定任何記憶體部件大小之前,先考慮使用AMM特性(即讓資料庫自動去調整,而不是自己去設定)。假如你必須要配置記憶體部件大小,那就考慮使用記憶體顧問,以便確定需要實際需要的記憶體大小。

 

7.1.3 Automatic Shared Memory Management

Automatic Shared Memory Management simplifies the configuration of the SGA. To use Automatic Shared Memory Management, set the SGA_TARGET initialization parameter to a nonzero value and set the STATISTICS_LEVEL initialization parameter to TYPICAL or ALL. Set the value of the SGA_TARGET parameter to the amount of memory that you intend to dedicate for the SGA. In response to the workload on the system, the automatic SGA management distributes the memory appropriately for the following memory pools:

ASMM簡化了SGA的配置。為了使用ASMM,需要設定sga_target = xxxxstatistics_level = typical|all。將SGA_TARGET設定成你希望的記憶體大小。根據系統壓力的不同,自動的SGA管理下面的記憶體部件:

  • Database buffer cache (default pool)

  • Shared pool

  • Large pool

  • Java pool

  • Streams pool

If these automatically tuned memory pools had been set to nonzero values, those values are used as minimum levels by Automatic Shared Memory Management. You would set minimum values if an application component needs a minimum amount of memory to function properly.

SGA_TARGET is a dynamic parameter that can be changed by accessing the SGA Size Advisor from the Memory Parameters SGA page in Oracle Enterprise Manager, or by querying the V$SGA_TARGET_ADVICE view and using the ALTER SYSTEM command. SGA_TARGET can be set less than or equal to the value of SGA_MAX_SIZE initialization parameter. Changes in the value of SGA_TARGET automatically resize the automatically tuned memory pools.

If you dynamically disable SGA_TARGET by setting its value to 0 at instance startup, Automatic Shared Memory Management will be disabled and the current auto-tuned sizes will be used for each memory pool. If necessary, you can manually resize each memory pool using the DB_CACHE_SIZE, SHARED_POOL_SIZE, LARGE_POOL_SIZE, JAVA_POOL_SIZE, and STREAMS_POOL_SIZE initialization parameters.

假如這些自動最佳化的記憶體池已經被設定成非0值,那麼這些值就被ASMM作為最小需要保留的值。假如你知道你的應用程式需要佔用的最小的部件記憶體,那就設定一個最小值給它。

SGA_TARGET是一個動態引數,可以透過OEMSGA SIZE Advisor來實時的調整其大小,或者查詢 v$sga_target_advice 檢視及使用alter system進行更新。但是sga_target <= sga_max_size引數。當改變SGA_TARGET的時候,SGA中的相關部件也一起隨之改變了

假如你在啟動時將SGA_TARGET=0的話,ASMM將會被禁用掉,並且當前各個部件的記憶體大小將作為其實際大小了(為什麼會這樣子呢?關閉資料庫後,去看看spfile就明白了!oracle將上次系統的各個部件大小都記錄下來,以便下次重啟的時候直接分配各部件到最佳狀態。這就是為什麼當ASMM關閉後其他部件會有正常的大小設定)。如果有必要的話,你可以手動的設定下面記憶體部件的大小:

db_cache_size, shared_pool_size, large_pool_size, java_pool_size, streams_pool_size引數。

The following pools are manually sized components and are not affected by Automatic Shared Memory Management:

下面這些部件是要手工去設定的,不會被ASMM影響到: log buffer, keep, recycle, other non-8k block size, fixed sga

  • Log buffer

  • Other buffer caches (such as KEEP, RECYCLE, and other nondefault block size)

  • Fixed SGA and other internal allocations

To manually size these memory pools, you must set the DB_KEEP_CACHE_SIZE, DB_RECYCLE_CACHE_SIZE, DB_nK_CACHE_SIZE, and LOG_BUFFER initialization parameters. The memory allocated to these pools is deducted from the total available for SGA_TARGET when Automatic Shared Memory Management computes the values of the automatically tuned memory pools.

手動去調整這些記憶體部件,你必須去設定:db_keep_cache_size, db_recycle_cache_size, db_16k_cache_size, log_buffer引數。雖然這些引數不能被SGA_TARGET自動設定,但是這些引數加起來的記憶體是包含在SGA_TARGET中的,所以在設定真正需要的記憶體時,多加上這些記憶體到SGA_TARGET上。

 

7.1.4 Dynamically Changing Cache Sizes

If the system is not using Automatic Memory Management or Automatic Shared Memory Management, you can choose to dynamically reconfigure the sizes of the shared pool, the large pool, the buffer cache, and the process-private memory. The following sections contain details on sizing of caches:

假如系統沒有使用ASMM或者AMM管理,你可以動態的重新配置這些shared pool,large pool,buffer cache, process-private部件的大小。下面的章節討論了調整大小的一些細節:

  • Configuring and Using the Buffer Cache

  • Configuring and Using the Shared Pool and Large Pool

  • Configuring and Using the Redo Log Buffer

The size of these memory caches is configurable using initialization configuration parameters, such as DB_CACHE_SIZE, JAVA_POOL_SIZE, LARGE_POOL_SIZE, LOG_BUFFER, and SHARED_POOL_SIZE. The values for these parameters are also dynamically configurable using the ALTER SYSTEM statement except for the log buffer pool and process-private memory, which are static after startup.

Memory for the shared pool, large pool, java pool, and buffer cache is allocated in units of granules. The granule size is 4MB if the SGA size is less than 1GB. If the SGA size is greater than 1GB, the granule size changes to 16MB. The granule size is calculated and fixed when the instance starts up. The size does not change during the lifetime of the instance.

The granule size that is currently being used for SGA can be viewed in the view V$SGA_DYNAMIC_COMPONENTS. The same granule size is used for all dynamic components in the SGA.

You can expand the total SGA size to a value equal to the SGA_MAX_SIZE parameter. If the SGA_MAX_SIZE is not set, you can decrease the size of one cache and reallocate that memory to another cache if necessary. SGA_MAX_SIZE defaults to the aggregate setting of all the components.

這些部件記憶體大小是由:db_cache_size,java_pool_size,large_pool_size,log_buffer,shared_pool_size引數來配置的。這些引數可以直接透過alter system語句來配置,但是log buffer poolprocess-private記憶體是靜態引數,必須重新啟動才能應用。

shared pool,large pool, java pool buffer cache的記憶體分配是按照粒度為單位進行分配的。如果SGA分配的大小小於1G的話,那麼這個粒度就是4M。假如SGA大於1G,那麼粒度就是16M。這個粒度的值是在例項啟動時計算出來的,這個值在系統執行期內是不會改變的。

(注:為什麼需要這個粒度呢? 那考慮一下如果沒有這個粒度會怎樣?如果沒有的話,那麼oracle每次分配記憶體的時候就需要到處去找哪裡有free的記憶體塊,然後再確定其大小,最後還要更新自己的“資料庫字典”以防止其他程式也從這裡分配等等,非常的複雜。

有了粒度之後就好辦了,如果一個“粒度塊”沒有使用,那就把那個4M或者16M的塊拿過來使用,用完之後再還回去,用個簡單的例子說明就是集裝箱的道理,將整個粒度作為一個小的整體,整個SGA就是輪船,輪船裡面都是由集裝箱組成的,至於你要在集裝箱裡裝什麼是自己的事情了。在11G中,oracle使用了MMAP技術,可以在/dev/shm中看到有很多檔案,每個都是以4M或者16M大小的,這個就是記憶體的一個對映,也是粒度的一個直觀展示, mmap的意思就是memory map的意思。在10G中,oracle還是使用傳統的共享記憶體技術,啟動時分配大量的記憶體,然後Oracle又將其邏輯的分成許多粒度塊使用。而在11G中直接透過作業系統的功能就實現了此方法。下面是我從MAN手冊中取的mmap系統呼叫方法,瞭解即可。

clip_image001

    那是不是隻有oracle使用了這個粒度呢? 顯然不是的,生活中到處都有,作業系統也是一樣。假如有16G的記憶體,那作業系統是如何管理呢? ------ 它是將記憶體按4K為一個粒度進行劃分的。那為什麼又是要以4K為單位劃分呢?因為是intel的地址匯流排這麼定的。在32位系統中,注意不是指32位作業系統,而是地址匯流排只有32位的系統中,比如11111111,11111111,11110000,00000000,我隨意的寫了32位數字,最後這12位,我用120來表示,212=4096=4K,那上面32 – 12 = 20位又是做什麼呢?在linux中是按照段頁式管理的,頁就是這4K的單位的粒度,前面20位就代表一個有220=1M個頁面,每個頁面又是4K大小。

    理解了這部分之後,就能知道一個程式最小的記憶體佔用了。比如我寫了一個簡單的printf(“hello world!\n”);程式,那這個程式按理說最多戰勝幾十個位元組,但是實際上作業系統要給它一個頁面的,即4K位元組使用的。同樣如果我想要4M的記憶體,作業系統就給我們1024個頁面(1024*4K = 4M

可以透過 v$sga_dynamic_components 檢視來檢視粒度大小。SGA中所有的部件都使用相同的粒度。 v$sgainfo中也有的。

你可以將總的SGA大小擴充套件到SGA_MAX_SIZE大小。假如SGA_MAX_SIZE沒有設定,如果需要的話,你可以將一個部件的記憶體減少,然後重新分配給其他的部件。SGA_MAX_SIZE預設是所有部件的記憶體總和。

Note:

SGA_MAX_SIZE cannot be dynamically resized.   注意這個SGA_MAX_SIZE是靜態引數,修改完一定要加上scope=spfile,否則無法修改成功

The maximum amount of memory usable by the instance is determined at instance startup by the initialization parameter SGA_MAX_SIZE. You can specify SGA_MAX_SIZE to be larger than the sum of all of the memory components, such as buffer cache and shared pool. Otherwise, SGA_MAX_SIZE defaults to the actual size used by those components. Setting SGA_MAX_SIZE larger than the sum of memory used by all of the components lets you dynamically increase a cache size without needing to decrease the size of another cache.

See Also:

Your operating system's documentation for information about managing dynamic SGA

 

    例項可以使用的最大記憶體是由系統啟動時的SGA_MAX_SIZE引數決定的。你可以將SGA_MAX_SIZE設定的比所有記憶體部件都大一些,例如buffer cacheshared pool否則,SGA_MAX_SIZE預設情況下是所有部件的總和。將SGA_MAX_SIZE設定的大一些的好處是可以讓你自動的增加快取大小而不用去減少其他部件的記憶體

 

7.1.4.1 Viewing Information About Dynamic Resize Operations

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.

          clip_image002

  • 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.

 clip_image003

  • 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.

clip_image005

v$memory_dynmic_components相比,少了SGA Target PGA Target兩列。

  • V$SGA_DYNAMIC_FREE_MEMORY displays information about the amount of SGA memory available for future dynamic SGA resize operations.

clip_image007

可以看到在我的虛擬機器上,memory_max_target一共是1G,目前給SGA的還剩下721420288 / 1024 / 1024 = 688M空間。

clip_image009

See Also:

    • Oracle Database Concepts for more information about dynamic SGA

 

 

7.1.5 Application Considerations

When configuring memory, size the cache appropriately for the application's needs. Conversely, tuning the application's use of the caches can greatly reduce resource requirements. Efficient use of Oracle Database memory caches also reduces the load on related resources such as the latches, the CPU, and the I/O system.

當配置記憶體時,要根據應用的需要去配置合適的記憶體。相反地,除錯應用程式對快取的使用以減少對資源的要求。高效使用資料庫快取同樣可以降低對其他的比如latchCPUIO資源的使用。

For best performance, you should consider the following:

為了更好的效能,你應該考慮以下幾點:

    最佳設計的快取應該能使作業系統和資料庫有效的使用資源

    對資料庫的記憶體設定應該能夠反映出應用程式的需求

  • The cache should be optimally designed to use the operating system and database resources most efficiently.

  • Memory allocations to Oracle Database memory structures should best reflect the needs of the application.

Making changes or additions to an existing application might require resizing Oracle Database memory structures to meet the needs of your modified application.

If your application uses Java, you should investigate whether you need to modify the default configuration for the Java pool. See the Oracle Database Java Developer's Guide for information about Java memory usage.

對一個應用程式進行改變或者增加功能時可能需要重新設定資料庫的記憶體結構,以滿足新的應用程式需求。

假如你的應用程式是用JAVA來寫的,你應該檢視一下你的預設的java pool是否夠用。

 

7.1.6 Operating System Memory Use

For most operating systems, it is important to consider the following:   對於多數的作業系統,必須要考慮以下幾點:

  • Reduce Paging                                 減少換頁

  • Fit the SGA into Main Memory                  固定SGA到主記憶體中

  • Allow Adequate Memory to Individual Users     給予獨立使用者足夠的記憶體

7.1.6.1 Reduce Paging

Paging occurs when an operating system transfers memory-resident pages to disk solely to allow new pages to be loaded into memory. Many operating systems page to accommodate large amounts of information that do not fit into real memory. On most operating systems, paging reduces performance.

Use operating system utilities to examine the operating system, to identify whether there is a lot of paging on your system. If so, then the total system memory may not be large enough to hold everything for which you have allocated memory. Either increase the total memory on your system, or decrease the amount of memory allocated.

換頁是作業系統將記憶體中的頁面轉換到硬碟上,以便留出記憶體頁給新的應用程式。許多要容納大量資訊的應用,它們需要的作業系統頁面太多以至於無法完全放入記憶體。在多數作業系統中,換頁將造成效能效能大幅下降。

使用作業系統提供的工具去檢查是否有換頁發生。如果有換頁發生,說明你的記憶體太小了,無法容納需要的內容。要麼增加伺服器的記憶體,要麼減少對記憶體的使用

 

7.1.6.2 Fit the SGA into Main Memory

Because the purpose of the SGA is to store data in memory for fast access, the SGA should be within main memory. If pages of the SGA are swapped to disk, then the data is no longer quickly accessible. On most operating systems, the disadvantage of paging significantly outweighs the advantage of a large SGA.

Note:

You can use the LOCK_SGA parameter to lock the SGA into physical memory and prevent it from being paged out. The database does not use the MEMORY_TARGET and MEMORY_MAX_TARGET parameters when the LOCK_SGA parameter is enabled.

 

因為SGA的目標是儲存資訊到記憶體中以便快速訪問,因此SGA應該一直在記憶體中。假如SGA的頁面被置換到了交換區,那麼資料將無法迅速的訪問到。在多數作業系統上,與其去發生換頁,還不如將SGA調的小一些(比設定太大的SGA更好些;因為太大了卻導致換頁發生,影響效能,何苦呢)。

注意: 你可以使用LOCK_SGA引數去將SGA鎖定到記憶體中以防止被置換出去。但是使用之後,資料庫將無法使用memory_target,memory_max_target引數了

       那麼在linux是如何做到這點呢?  使用mlockmlockall程式去實現!下面是我在man手冊中開啟的,可以看到DESCRIPTION中第一行寫的:

       mlock()和mlockall()鎖定部分或全部的呼叫程式的虛擬地址空間到RAM中,防止記憶體被置換到交換區中。

    clip_image010

To see how much memory is allocated to the SGA and each of its internal structures, enter the following SQL*Plus statement:

       怎樣檢視SGA的資訊呢? 使用show sga或者在v$sga_dynamic_components中檢視。

        clip_image011

               也還可以這樣做:從v$sgainfo中查詢

clip_image013

        這樣從圖中我們看到granule size 4M,最大的SGA大小是1G, 還剩下721420288/1024/1024 = 688M

 

7.1.6.3 Allow Adequate Memory to Individual Users

When sizing the SGA, ensure that you allow enough memory for the individual server processes and any other programs running on the system.

See Also:

Your operating system hardware and software documentation, and the Oracle documentation specific to your operating system, for more information on tuning operating system memory usage

 

當調整SGA的時候,確保有足夠的記憶體給server程式以及系統上其他程式。(比如作業系統程式,監控程式等等)

 

7.1.7 Iteration During Configuration

Configuring memory allocation involves distributing available memory to Oracle Database memory structures, depending on the needs of the application. The distribution of memory to Oracle Database structures can affect the amount of physical I/O necessary for Oracle Database t operate. Having a good first initial memory configuration also provides an indication of whether the I/O system is effectively configured.

It might be necessary to repeat the steps of memory allocation after the initial pass through the process. Subsequent passes let you make adjustments in earlier steps, based on changes in later steps. For example, decreasing the size of the buffer cache lets you increase the size of another memory structure, such as the shared pool.

考慮記憶體分配涉及到分配合理大小的記憶體到資料庫的記憶體結構上,這是依賴於應用程式的需求的。資料庫記憶體結構的分配會影響IO的操作總數。要一開始就合理的配置記憶體,以及提供一個檢視IO系統是否有效配置的監控應用。

對於記憶體的分配是一個不斷重複迭代除錯的過程,直至滿足應用程式的要求。基於隨後程式的發展,接下來你可以對更早的配置中做一個調整。例如減少buffer cache的大小時,去增加shared pool的大小。

 

 

窗體頂端

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30633755/viewspace-2127715/,如需轉載,請註明出處,否則將追究法律責任。

相關文章