Oracle 記憶體架構是效能調校的基石。理解 SGA (System Global Area)、PGA (Program Global Area) 與 UGA (User Global Area) 的職責、分配機制與交互關係,是 DBA 與開發人員的必修課。
| 組件 | 功能 | 關鍵參數 |
|---|---|---|
| Database Buffer Cache | 緩存資料塊(資料表、索引) | DB_CACHE_SIZE, DB_ |
| Shared Pool | Library Cache (SQL/PLAN)、Data Dictionary Cache | SHARED_POOL_SIZE |
| Redo Log Buffer | 緩存 Redo Entry,定期刷入線上重做日誌 | LOG_BUFFER |
| Large Pool | 備份/還原、共用伺服器 UGA、平行查詢訊息緩衝 | LARGE_POOL_SIZE |
| Java Pool | Java 程式碼、Session 狀態 | JAVA_POOL_SIZE |
| Streams Pool | Streams 複寫、Advanced Queuing | STREAMS_POOL_SIZE |
| In-Memory Area | Columnar 格式資料(12c+) | INMEMORY_SIZE |
-- 查看 SGA 總覽
SHOW SGA;
-- 動態調整(無需重啟)
ALTER SYSTEM SET db_cache_size = 500M;
ALTER SYSTEM SET shared_pool_size = 300M;
ALTER SYSTEM SET sga_max_size = 2G; -- 上限
| 工作區 | 用途 | 相關參數 |
|---|---|---|
| Sort Area | ORDER BY、GROUP BY、索引建立排序 | SORT_AREA_SIZE (手動模式) |
| Hash Area | Hash Join、Hash Aggregation | HASH_AREA_SIZE |
| Bitmap Merge | Bitmap 索引合併 | BITMAP_MERGE_AREA_SIZE |
| Create Bitmap | Bitmap 索引建立 | CREATE_BITMAP_AREA_SIZE |
-- 啟用自動管理
ALTER SYSTEM SET pga_aggregate_target = 1G SCOPE=BOTH;
ALTER SYSTEM SET workarea_size_policy = AUTO; -- 預設即為 AUTO
-- 查看 PGA 使用情況
SELECT * FROM v$pgastat;
SELECT * FROM v$process_memory WHERE pid = ;
UGA 儲存 Session 級狀態:Package 變數、Cursor 狀態、PL/SQL 執行上下文等。其儲存位置取決於連線模式:
| 連線模式 | UGA 位置 | 原因 |
|---|---|---|
| 專用伺服器 | PGA 中 | 每 Session 一進程,UGA 隨進程私有 |
| 共用伺服器 | SGA 中 (Large Pool) | 多 Session 共用進程,UGA 必須共享可見 |
-- 專用伺服器:UGA 在 PGA
SELECT name, value FROM v$sesstat s
JOIN v$statname n ON s.statistic# = n.statistic#
WHERE s.sid = SYS_CONTEXT('USERENV','SID')
AND n.name LIKE '%uga%';
-- 共用伺服器:UGA 在 SGA (Large Pool)
SELECT pool, name, bytes FROM v$sgastat WHERE pool = 'large pool';
┌─────────────────────────────────────────────────────────────┐ │ Oracle Instance │ │ ┌─────────────────────────────────────────────────────┐ │ │ │ SGA (共享) │ │ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌──────────┐ │ │ │ │ │Buffer │ │Shared │ │Redo Log │ │Large │ │ │ │ │ │Cache │ │Pool │ │Buffer │ │Pool(UGA*)│ │ │ │ │ └─────────┘ └─────────┘ └─────────┘ └──────────┘ │ │ │ └─────────────────────────────────────────────────────┘ │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Server Proc │ │ Server Proc │ │ Server Proc │ ... │ │ │ (PGA) │ │ (PGA) │ │ (PGA) │ │ │ │ ┌─────────┐ │ │ ┌─────────┐ │ │ ┌─────────┐ │ │ │ │ │ Sort/ │ │ │ │ Sort/ │ │ │ │ Sort/ │ │ │ │ │ │ Hash │ │ │ │ Hash │ │ │ │ Hash │ │ │ │ │ │ UGA* │ │ │ │ UGA* │ │ │ │ UGA* │ │ │ │ │ └─────────┘ │ │ └─────────┘ │ │ └─────────┘ │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘ * 專用伺服器模式:UGA 在 PGA;共用伺服器模式:UGA 在 SGA Large Pool
V$SYSSTAT 的 db block gets / consistent gets / physical reads)RELOADS / INVALIDATIONSMEMORY_TARGET 統一管理 SGA+PGA,簡化調校SGA、PGA、UGA 三大記憶體結構各司其職,又緊密協作。掌握其架構差異、動態調整方式與連線模式對 UGA 定位的影響,能幫助你從記憶體層面精準診斷效能瓶頸,制定最優化的資源配置策略。