首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> Oracle 資料庫 >> Oracle 資料庫基礎

Oracle Redo 並行機制深度解析:Shared Strand、Private Strand 與 Latch 優化

     編輯:Oracle 資料庫基礎

Redo 產生與寫入流程

  1. User Process 在 PGA 產生 Redo Entry。
  2. 取得 Redo Copy Latch (多個,CPU_COUNT × 2) 複製到公共區。
  3. 取得 Redo Allocation Latch (單一) 分配 Log Buffer 空間。
  4. 釋放 Allocation Latch → 寫入 Log Buffer → 釋放 Copy Latch。
  5. LGWR 依條件 (Commit、1/3 滿、3 秒、切換) 將 Buffer 寫入 Redo Log File。

高併發 OLTP 下,Redo Allocation Latch 爭用 成為熱點。

Shared Strand (9.2 引入,10g 參數隱含化)

  • 將 Log Buffer 切割為多個 Strand,各自擁有獨立 Allocation Latch。
  • 參數控制:
    _log_parallelism          -- 初始 Strand 數 (10g 隱含)
    _log_parallelism_max      -- 最大 Strand 數
    _log_parallelism_dynamic  -- 是否動態調整 (TRUE/FALSE)
  • 查詢實際 Strand:
SELECT indx, strand_size_kcrfa
FROM   x$kcrfstrand
WHERE  last_buf_kcrfa != '00';

-- 範例輸出:2 個 Shared Strand,各 3.35 MB
-- LOG_BUFFER = 7,028,736 → 7MB/2 ≈ 3.5MB

設定建議

  • CPU ≤ 16:預設 2 Strand 足夠。
  • 每增加 16 CPU 可考慮 +1 Strand,上限 8。
  • _log_parallelism_max 不得超過 CPU_COUNT。
  • 11g 移除 _log_parallelism,完全由 _log_parallelism_max、_log_parallelism_dynamic、CPU_COUNT 決定。

Private Strand (10g 引入,重大突破)

  • 不再從 Log Buffer 切割,而是在 Shared Pool 獨立分配記憶體 (V$SGASTAT 名稱 private strands)。
  • 每個 Private Strand 服務 單一活動交易,交易期間在 Strand 內產生 Redo,Commit/Flush 時批次寫入 Redo Log File。
  • 優勢:
    1. 無需 Redo Copy Latch。
    2. 無需 Shared Strand Allocation Latch。
    3. 批次寫入減少 LGWR 喚醒次數。
    4. 大幅降低 CPU 佔用與 Latch 等待。
  • 查詢 Private Strand:
SELECT * FROM v$sgastat WHERE name LIKE '%strand%';
-- POOL: shared pool  NAME: private strands  BYTES: 2,684,928

SELECT indx, strand_size_kcrfa
FROM   x$kcrfstrand
WHERE  last_buf_kcrfa = '00';  -- Private Strand 標記
-- 大量小 Strand (如 66KB x 多個)

交易使用 Private Strand 判斷

SELECT DECODE(BITAND(ktcxbflg, 4096), 0, 1, 0) AS used_private_strand, COUNT(*)
FROM   x$ktcxb
WHERE  BITAND(ksspaflg, 1) != 0
AND    BITAND(ktcxbflg, 2) != 0
GROUP  BY BITAND(ktcxbflg, 4096);
-- USED_PRIVATE_STRAND  COUNT(*)
-- 1 (使用)             10
-- 0 (未使用)           1

Private Strand 流程

  1. 交易開始 → 申請 Private Strand Allocation Latch (失敗則退回 Shared Strand)。
  2. 在 Private Strand 產生 Redo Entry。
  3. Flush/Commit → 申請 Redo Copy Latch → 批次寫入 Log File → 釋放 Latch → 釋放 Private Strand Latch。

注意:交易一旦進入 Shared Strand 流程,即使後來有 Private Strand 釋出,也不會切換回 Private Strand。

總結:Redo 並行演進脈絡

階段機制Latch 爭用點適用場景
單 Strand單一 Log BufferRedo Allocation Latch (單點熱點)低併發
Shared StrandLog Buffer 多分區多個 Allocation Latch 分散中高併發、10g 前
Private StrandShared Pool 獨立記憶體,交易專用幾乎消除 Allocation/Copy Latch 爭用高併發 OLTP、10g+ 預設啟用
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved