Oracle Tanel Poder Rowcache 与 Latch
Oracle Tanel Poder Rowcache 与 Latch
来源:Tanel Poder / tanelpoder.com 适用版本:Oracle Database 9i+ 文档版本:v1.0 / 2026-07-22
1. 概述
Tanel Poder 对 Rowcache 与 Latch 有深入分析[1]。
详细见:Oracle-Maclean-Latch与Mutex深入。
2. Rowcache 基础
2.1 作用
- 数据字典缓存
- Shared Pool 一部分
- 解析时查询
2.2 内容
- dc_users
- dc_objects
- dc_tables
- dc_columns
- dc_indexes
- dc_segments
- dc_rollback_segments
2.3 Tanel 观点
- Rowcache 关键
- 解析依赖
- 性能影响
3. Rowcache 视图
3.1 v$rowcache
SELECT cache#, type, parameter, count, usage, fixed, gets, getmisses, scans, scanmisses
FROM v$rowcache
ORDER BY gets DESC;
3.2 关键字段
- count:缓存条目数
- usage:使用中
- gets:请求次数
- getmisses:未命中
- scans:扫描
- scanmisses:扫描未命中
3.3 Tanel 分析
- getmisses/get 高:未命中多
- 评估 Shared Pool
4. 命中率
4.1 计算
SELECT
SUM(gets) AS total_gets,
SUM(getmisses) AS total_misses,
ROUND((1 - SUM(getmisses)/SUM(gets))*100, 2) AS hit_rate
FROM v$rowcache;
4.2 标准
- > 95%:好
- < 90%:评估
- 增加 Shared Pool
4.3 Tanel 建议
- 监控命中率
- 评估 Shared Pool
- 优化
5. Rowcache 争用
5.1 等待事件
- row cache lock
- latch: row cache objects
5.2 原因
- 频繁 DDL
- 共享池不足
- 解析多
5.3 诊断
SELECT event, count(*) FROM v$active_session_history
WHERE event LIKE '%row cache%'
GROUP BY event;
5.4 Tanel 分析
- row cache lock:字典锁
- latch:内存锁
- 根因
6. Latch 基础
6.1 作用
- 内存结构保护
- 短期锁
- 串行化
6.2 工作流程
1. 请求 Latch
2. Spin(CPU 循环)
3. 失败 → Sleep
4. 唤醒 → 重试
5. 获取 → 工作 → 释放
6.3 Tanel 观点
- Latch 争用 = 性能问题
- 理解机制
- 优化
7. Latch 类型
7.1 cache buffers chains
- Buffer Cache 哈希链
- 热点块
7.2 cache buffers lru chain
- LRU 链
- Buffer 替换
7.3 library cache
- Library Cache
- SQL/PLSQL
7.4 shared pool
- Shared Pool
- 内存分配
7.5 row cache objects
- Rowcache
- 字典缓存
8. Latch 诊断
8.1 v$latch
SELECT name, gets, misses, spin_gets, sleep1, wait_time
FROM v$latch
ORDER BY misses DESC
FETCH FIRST 10 ROWS ONLY;
8.2 v$latch_children
SELECT child#, gets, misses, spin_gets
FROM v$latch_children
WHERE name = '&latch_name'
ORDER BY misses DESC FETCH FIRST 10 ROWS ONLY;
8.3 v$latchholder
SELECT * FROM v$latchholder;
8.4 Tanel 分析
- misses 多:争用
- sleep 多:严重
- 找热块
9. Tanel 工具:latchprof
9.1 用途
- Latch 详细分析
- 函数级别
- 深入
9.2 用法
@latchprof mode,level sid,name sleeps 1000 10
9.3 输出
- Latch 地址
- 持有函数
- 调用栈
9.4 Tanel 优势
- 函数级别
- 深入根因
- 高级
10. 热点块查找
10.1 Latch → 块
-- Latch 地址
SELECT hladdr FROM x$bh
GROUP BY hladdr ORDER BY COUNT(*) DESC FETCH FIRST 5 ROWS ONLY;
-- 块 → 对象
SELECT d.owner, d.object_name, d.object_type
FROM dba_extents d, x$bh x
WHERE d.file_id = x.file#
AND x.dbabuf BETWEEN d.block_id AND d.block_id + d.blocks - 1
AND x.hladdr = '&latch_addr';
10.2 Tanel 方法
- Latch → 地址 → 块 → 对象
- 系统性
11. 优化
11.1 cache buffers chains
- 反向索引
- 分区表
- 减少热点
- _DB_BLOCK_HASH_BUCKETS(隐含)
11.2 library cache
- 绑定变量
- 共享池大小
- 减少硬解析
11.3 shared pool
- 共享池足够
- 减少解析
- 评估
11.4 row cache
- 减少 DDL
- 共享池
- 评估
12. Mutex
12.1 替代
- 替代部分 Latch
- 更轻量
- 10g+
12.2 优势
- 内存小
- 速度快
- 减少 Latch
12.3 v$mutex_sleep
SELECT mutex_type, location, sleeps, wait_time
FROM v$mutex_sleep
ORDER BY sleeps DESC;
12.4 Tanel 工具:mutexprof
@mutexprof mode,level sid,name 1000 10
13. 案例:Library Cache 争用
13.1 现象
- latch: library cache
- 应用慢
13.2 Tanel 诊断
1. latchprof 分析
2. 查找根因
3. 绑定变量
4. 共享池
13.3 优化
- 绑定变量
- 共享池
- 评估
14. 案例:Rowcache 争用
14.1 现象
- row cache lock
- DDL 频繁
14.2 Tanel 诊断
1. v$rowcache
2. 查找热对象
3. 减少 DDL
4. 优化
15. Tanel 方法论
15.1 步骤
1. 等待事件
2. v$rowcache/v$latch
3. latchprof/mutexprof
4. 根因
5. 优化
15.2 工具
- TPT 脚本
- latchprof
- mutexprof
- snapper
15.3 原则
- 工具辅助
- 数据驱动
- 深入内部
16. 最佳实践
- 绑定变量:必用
- 共享池:足够
- 监控:Rowcache/Latch
- latchprof:深入
- mutexprof:Mutex
- 热点块:消除
- DDL:减少
- 统计:命中率
- 测试:验证
- 原理:理解
17. 参考资料
[1] Tanel Poder, “Rowcache and Latch”, https://tanelpoder.com [2] TPT Scripts, https://github.com/tanelpoder/tpt_oracle