Latch是簡單的、低層次的序列化技術,用以保護SGA中的共享數據結構,比如並發用戶列表和buffer cache裡的blocks信息。一個服務器進程或後台進程在開始操作或尋找一個共享數據結構之前必須獲得對應的latch,在完成以後釋放latch。不必對latch本身進行優化,如果latch存在競爭,表明SGA的一部分正在經歷不正常的資源使用。
1)Latch的作用:
A、序列化訪問:保護SGA中的共享數據結構;保護共享內存的分配。
B、序列化執行:避免同時執行某些關鍵代碼;避免互相干擾。
2)Latch請求的兩種類型:
A、willing-to-wait:請求的進程經過短時間的等待後再次發出請求,直到獲得latch
B、immediate:如果沒有獲得latch,請求的進程不等待,而是繼續處理其他指令。
2、檢查Latch競爭
檢查latch free是不是主要的wait event:
Select * from v$system_event order by time_waited;
檢查latch的使用情況:
Select * from v$latch:
與willing-to-wait請求有關的列:gets、misses、sleeps、wait_time、cwait_time、spin_gets
與immediate請求有關的列:immediate_gets、immediate_misses
Gets: number of successful willing-to-wait requests for a latch;
Misses: number of times an initial wiling-to-wait request was unsuccessful;
Sleeps: number of times a process waited after an initial willing-to-wait request;
Wait_time: number of milliseconds waited after willing-to-wait request;
Cwait_time: a measure of the cumulative wait time including the time spent spinning and sleeping,the overhead of context switches due to OS time slicing and page faults and interrupts;
Spin_gets: gets that misses first try but succeed after spinning.
Immediate_gets: number of successful immediate requests for each latch;
Immediate_misss: number of unsuccessful immediate requests for each latch;
一般無需調整latch,但是下列的措施是有用的:
A、對處於競爭中的latch做進一步的調查
B、如果競爭主要存在於shared pool和library cache中,可以考慮調整應用
C、如果進一步的調查顯示需要調整shared pool和buffer cache,就進行調整
Select * from v$latch where name like ‘%shared pool%’ or name like ‘%library cache%’;
如果競爭是在shared pool或library cache上,表示下列集中情況:
A、不能共享的sql,應檢查他們是否相似,考慮以變量代替sql中的常量:
Select sql_text from v$sqlarea where executions=1 order by upper(sql_text);
B、共享sql被重新編譯,考慮library cache的大小是否需要調整:
SELECT sql_text,parse_calls,executions FROM v$sqlarea where parse_calls>5;
C、library cache不夠大。