诊断 ’library cache: mutex X’ 等待 (Doc ID 2331144.1)
Troubleshooting 'library cache: mutex X' Waits. (Doc ID 1357946.1)
Oracle Database Cloud Schema Service - 版本 N/A 和更高版本 Oracle Database Exadata Cloud Machine - 版本 N/A 和更高版本 Oracle Database Exadata Express Cloud Service - 版本 N/A 和更高版本 Oracle Cloud Infrastructure - Database Service - 版本 N/A 和更高版本 Oracle Database Backup Service - 版本 N/A 和更高版本 本文档所含信息适用于所有平台
本文档旨在说明 library cache: mutex X 等待诊断思路。
该机制是用于保护内存结构,在 library cache 中有许多内存结构需要 library cache: mutex X 的保护。
library cache 用来保存解析过的 cursor 相关的内存结构。
等待 library cache: mutex X 与之前版本的 latch:library cache 等待相同。library cache: mutex X 可以被很多因素引起,例如:(包括应用问题,执行计划不能共享导致的高版本的游标等),本质上都是某个进程持有 library cache: mutex X 太长时间,导致后续的进程必须等待该资源。如果在 library cache 的 latch 或者 mutex 上有等待,说明解析时有很大的压力,解析 SQL 的时间变长(由于 library cache 的 latch 或者 mutex 的等待)会使整个数据库的性能下降。
由于引起 library cache: mutex X 的原因多种多样,因此找到引起问题的根本原因很重要,才能使用正确的解决方案。
12c 以后该等待又被细分为如下:
* library cache: mutex X – 用于保护 handle。
* library cache: bucket mutex X – 用于保护 library cache 中的 hash buckets。
* library cache: dependency mutex X – 用于保护依赖。
使用如下脚本生成问题发生时的半小时到一小时区间的 AWR 和 ADDM 报告:
SQL> @$ORACLE_HOME/rdbms/admin/awrrpt.sql
SQL> @$ORACLE_HOME/rdbms/admin/addmrpt.sql
参考:
Document 1680075.1 How to Generate and Check an ADDM report Document 1903158.1 How to Collect Standard Diagnostic Information Using AWR Reports for Performance Issues
(a) Non-Rac
sqlplus "/ as sysdba"
oradebug setmypid
oradebug unlimit
oradebug dump systemstate 266
wait 90 seconds
oradebug dump systemstate 266
wait 90 seconds
oradebug dump systemstate 266
quit
(b) RAC
$ sqlplus '/ as sysdba'
oradebug setmypid
oradebug unlimit
oradebug setinst all
oradebug -g all hanganalyze 4
oradebug -g all dump systemstate 266
quit
$ sqlplus
SQL> oradebug setospid <p.spid from above>
oradebug dump errorstack 3
<< wait 1min>>
oradebug dump errorstack 3
<< wait 1min>>
oradebug dump errorstack 3
exit
得到的 trace 可以用来定位已知问题。 systemstate dump 和 errorstack 不容易理解,可以创建 SR 请技术支持工程师协助分析。
select s.sid, t.sql_text
from v$session s, v$sql t
where s.event like '%mutex%'
and t.sql_id = s.sql_id
Document 459694.1 Procwatcher: Script to Monitor and Examine Oracle DB and Clusterware Processes
image-20240412174548786
image-20240412174559935
之后点击“SQL ordered by Parse Calls”和“SQL ordered by Version Count”
image-20240412174611922
定位解析比较高的 SQL:
注意比较高的解析比例的 SQL,理想情况下解析和执行的比例应该很低,如果该比例很高说明应用中没有很好的使用游标,游标解析并且打开之后应该保持打开状态,与开发人员确认如何保持游标打开,避免下次执行该 SQL 时重复解析。
下一步检查 SQL 高版本:
通过如上的列表中找到 SQL 版本较高的 SQL,可以通过 V$SQL_SHARED_CURSOR
检查引起 SQL 高版本的原因。
Document 438755.1 Formated V$SQL_SHARED_CURSOR Report by SQLID or Hash ValueDocument 296377.1 Troubleshooting: High Version Count Issues
image-20240412174654241
该信息表明每秒会有26.3次的硬解析,这表明硬解析很高。需要检查应用是否正确使用了绑定变量,此外可以参考如下文档的“Over Parsing”部分。
Document 33089.1 TROUBLESHOOTING: Possible Causes of Poor SQL Performance
对于 SQL AREA 的重新加载也要进行检查:
image-20240412174703178
如果在 SQL AREA 上的重新加载次数很高,那么需要检查游标是否被有效共享(重新加载的次数是指被缓存在 shared pool 中,但是使用时已经不在 shared pool 中)。如果游标已经有效共享,那么需要确认 shared pool 和 sga_target 是否足够大,如果 shared pool 有压力而没有足够的空间,那么有些缓存的游标会被从 shared pool 中清除。如果游标共享不充分,shared pool 会被这些不能被重用的游标占满,从而把那些可以重用的游标挤出 shared pool,进而引起在这些 SQL 重新执行时需要重新加载。游标共享充分,但由于 shared pool 空间过小也会引起可重用的游标被清除从而引发硬解析。不过最常见的情况还是游标无法共享。请参照如下文档调整 shared pool:
Document 62143.1 Understanding and Tuning the Shared Pool
Document 727400.1 WAITEVENT: "library cache: mutex X"
Document 1169017.1 ANNOUNCEMENT: Deprecating the cursor_sharing = ‘SIMILAR’ setting
Document 2051456.1 Troubleshooting Databases Hang Due to Heavy Contention for 'library cache: mutex X' Waits (Oracle 11.2 and Later)
NOTE:1291879.1 - Oracle Database Patch Set Update 11.2.0.2.2 Known Issues NOTE:1169017.1 - ANNOUNCEMENT: Deprecating the Cursor_Sharing = 'SIMILAR' Setting NOTE:296377.1 - Troubleshooting: High Version Count Issues NOTE:33089.1 - * TROUBLESHOOTING: Possible Causes of Poor SQL Performance NOTE:459694.1 - Procwatcher: Script to Monitor and Examine Oracle DB and Clusterware Processes
NOTE:62143.1 - Troubleshooting: Understanding and Tuning the Shared Pool NOTE:727400.1 - WAITEVENT: "library cache: mutex X" NOTE:438755.1 - High SQL Version Counts - Script to determine reason(s) NOTE:2051456.1 - Troubleshooting Databases Hang Due to Heavy Contention for 'library cache: mutex X' Waits (Oracle 11.2 and Later)