当前位置: 首页 > news >正文

Gather Statistics AUTO_INVALIDATE 减少db的 library cache lock

这个参数可以用于解决gather statistics 导致的library cache lock

Oracle 最高效hard parse的办法:

gather statistics 后不会标记失效,不执行不会无效,执行一次不会无效,执行一次才开始计时,计时结束也不会标记无效。再次执行才会再次触发无效,hard parse,执行。

1. 第一次执行不会失效,也不会立即再次parse

2.第一次执行后开始计算过期,过期时间不超过最大 a (pseudo-)random value up to _optimizer_invalidation_period sec from the time of this parse. 大于过期时间后也不会mark 成invlidate

3.再次执行才会标记无效并再次hard parse.

Applies To

All Users

Summary

Starting with Oracle10g, the DBMS_STATS package offers the AUTO_INVALIDATE option for the NO_INVALIDATE parameter of its GATHER_xxx_STATS and DELETE_xxx_STATS procedures. This parameter allows the user to specify when to invalidate dependent cursors i.e. cursors cached in the library cache area of the shared pool which reference a table, index, column or fixed object whose statistics are modified by the procedure call.

According to the documentation the values NO_INVALIDATE can take are:

  • TRUE: does not invalidate the dependent cursors
  • FALSE: invalidates the dependent cursors immediately
  • AUTO_INVALIDATE (default): have Oracle decide when to invalidate dependent cursors

This document describes details of how AUTO_INVALIDATE works.

This article may be of interest to DBAs interested in the behaviour of DBMS_STATS with respect to cursor invalidations.

Solution

When statistics are modified by DBMS_STATS, new cursors not yet cached in the shared pool will use them to get execution plans. Existing cursors cached in the shared pool cannot update their execution plans however. Instead, such cursors are invalidated and new versions (children cursors) are created which get execution plans based on the updated statistics. This involves a hard-parse operation which is more expensive in resource consumption than a soft-parse which simply reuses a cached cursor.
An important question to consider is: when does this invalidation occur in time after the statistics have been modified ?

Cursor Invalidations on Gathering Statistics prior to Oracle10g

In releases prior to Oracle10g gathering statistics using DBMS_STATS resulted in immediate invalidations of dependent cached cursors, unless NO_INVALIDATE was set to TRUE. An invalidation of a cached cursor means it has to be hard-parsed the next time it is executed. If large numbers of such cursors had to be invalidated and immediately re-executed e.g. due to DBMS_STATS being used on a popular object during a time period of heavy user workload, this would result in a hard-parse spike which could have serious effects on performance including high CPU usage, heavy library cache and shared pool latch contention with subsequent slowdown in application user response times.

Setting NO_INVALIDATE to TRUE would prevent such spikes but this meant that cursors would not notice the updated object statistics and would continue using existing execution plans until hard-parsed. Such a hard parse could happen on a cursor reload (i.e. on the next execution following the cursor being automatically flushed out of the shared pool due to inactivity and heavy usage of other cursors) or after a manual flushing of the shared pool (which could itself also result in hard-parse spikes as most of the flushed-out cursors would need to do a hard parse on their next execution.)

Cursor Invalidations with Oracle10g and AUTO_INVALIDATE

With the AUTO_INVALIDATE option the goal is to spread out the cursor invalidations over a time period long enough for hard-parses not to cause noticeable spikes.

In this way a cached cursor depending on an object whose statistics have been modified by DBMS_STATS will be invalidated as follows:

  • when DBMS_STATS modifies statistics for an object, all current cached cursors depending on this object are marked for rolling invalidation. Let's call this time T0.
  • the next time a session executes a cursor marked for rolling invalidation, it sets a timestamp. This timestamp can take a (pseudo-)random value up to _optimizer_invalidation_period sec from the time of this parse. The reason it does this is to randomly distribute the actual invalidation so as to avoid multiple parses occurring at the same time as much as possible.The default for this parameter is 18000 sec i.e. 5 hours. Let's call the time of this parse T1 and the timestamp value Tmax. On this (first) execution and parse we reuse the existing cursor i.e. we do not hard-parse and do not use the modified statistics to generate a new plan (it is a soft parse.)
  • on every subsequent execution and parse of this cursor (which is now marked for rolling invalidation and timestamped) we check whether the current time T2 exceeds the timestamp Tmax. If not, we reuse the existing cursor again, as happened on the first (soft) parse at time T1. If Tmax has been exceeded, we invalidate the cached cursor and create a new version of it (a new child cursor) which uses the new statistics of the object to generate its execution plan. The new child is marked ROLL_INVALID_MISMATCH in V$SQL_SHARED_CURSOR to explain why we could not share the previous child.

From the above descriptions, it follows that:

  • a cursor which is never parsed again after being marked for rolling invalidation will not beinvalidated and may eventually be flushed out of the shared pool if memory becomes scarce
  • a cursor which is only parsed once after being marked for rolling invalidation will not be invalidated(it will only be timestamped) and again may be eventually flushed out if memory in the shared pool becomes scarce ---这个标记后还是不会变为失效状态。
  • cursors which are regularly reused will become invalidatedon the next parse that happens after the timestamp Tmax has been exceeded(只有invalidate 才会再次执行时hard parse)

It should be clear that the above method is efficient in that it incurs the overhead of invalidations only for frequently reused cursors.

Exception:parallel SQL are immediately invalidated in order to ensure consistency between execution plans of slaves and Query Coordinator across multiple RAC instances. This is not a problem as parallel SQL are usually heavy and therefore hard-parse resources are insignificant to their total resource usage.

Testcase for Cursor Invalidations using AUTO_INVALIDATE

Here is a short testcase demonstrating the above from a 10.2.0.3 database. It creates a simple table, runs a simple query on it, then gathers statistics and monitors what happens to the cached cursor for the query as time passes and the query is re-executed.

First connect to the database as SYSDBA using SQL*Plus and set it up to show current time, detailed timestamps and reduce the invalidation parameter to 5 minutes from the default of 5 hours:

SQL> set time on pages 1000
15:48:41 SQL> alter session set nls_date_format='dd/mm/yyyy hh24:mi:ss';

Session altered.

15:48:41 SQL> alter system set "_optimizer_invalidation_period"=300;

System altered.

Then create a small table and gather statistics on it:

15:48:41 SQL> create table X as select * from dba_tables;

Table created.

15:48:41 SQL> exec dbms_stats.gather_table_stats(null,'X');

PL/SQL procedure successfully completed.

15:48:42 SQL> select last_analyzed from dba_tables where table_name='X';

LAST_ANALYZED
-------------------
13/03/2008 15:48:42

Now run our example query and check its cursor:

15:48:42SQL> select count(*) from X;

COUNT(*)
----------
1590

15:48:42 SQL> select sql_id from v$sql where sql_text='select count(*) from X';

SQL_ID
-------------
95rckg79jgshh

15:48:42 SQL> select * from v$sql_shared_cursor where sql_id='95rckg79jgshh';

SQL_ID ADDRESS CHILD_AD CHILD_NUMBER U S O O S L S E B P I S T A B D L T
------------- -------- -------- ------------ - - - - - - - - - - - - - - - - - -
R I I R L I O S M U T N F A I T D L D B P C S R P T M B M R O P M F L
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95rckg79jgshh 3076145C 33EFD654 0 N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N


15:48:42 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBER PARSE_CALLSEXECUTIONSFIRST_LOAD_TIME LAST_LOAD_TIME
------------ ---------------------------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 112008-03-13/15:12:47 2008-03-13/15:48:42
13/03/200815:48:42

We can see a single child cursor parsed & executed once at 15:18:42. Let's execute it once more and compare:

15:48:53SQL> select count(*) from X;

COUNT(*)
----------
1590

15:48:55 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBER PARSE_CALLSEXECUTIONSFIRST_LOAD_TIME LAST_LOAD_TIME
------------ ---------------------------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 222008-03-13/15:12:47 2008-03-13/15:48:42
13/03/200815:48:53

We can see now it has 2 executions (and parses) and last execution time has been updated to 15:48:53.
Let us gather statistics on the table using the default which will be AUTO_INVALIDATE:

15:48:55 SQL> exec dbms_stats.gather_table_stats(null,'X');

PL/SQL procedure successfully completed.

15:49:08 SQL> select last_analyzed from dba_tables where table_name='X';

LAST_ANALYZED
-------------------
13/03/2008 15:49:07

15:49:08 SQL> select * from v$sql_shared_cursor where sql_id='95rckg79jgshh';

SQL_ID ADDRESS CHILD_AD CHILD_NUMBER U S O O S L S E B P I S T A B D L T
------------- -------- -------- ------------ - - - - - - - - - - - - - - - - - -
R I I R L I O S M U T N F A I T D L D B P C S R P T M B M R O P M F L
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95rckg79jgshh 3076145C 33EFD654 0 N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N


15:49:08 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBER PARSE_CALLS EXECUTIONS FIRST_LOAD_TIME LAST_LOAD_TIME
------------ ----------- ---------- ------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 2 2 2008-03-13/15:12:47 2008-03-13/15:48:42
13/03/2008 15:48:53

Nothing has changed on the cursor although we know it has been marked behind the scenes for rolling invalidation. Now let us wait for longer than the _optimizer_invalidation_period e.g. 6 minutes, then check the cursor again:

15:55:24 SQL> select * from v$sql_shared_cursor where sql_id='95rckg79jgshh';

SQL_ID ADDRESS CHILD_AD CHILD_NUMBER U S O O S L S E B P I S T A B D L T
------------- -------- -------- ------------ - - - - - - - - - - - - - - - - - -
R I I R L I O S M U T N F A I T D L D B P C S R P T M B M R O P M F L
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95rckg79jgshh 3076145C 33EFD654 0 N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N


15:55:24 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBER PARSE_CALLS EXECUTIONS FIRST_LOAD_TIME LAST_LOAD_TIME
------------ ----------- ---------- ------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 2 2 2008-03-13/15:12:47 2008-03-13/15:48:42
13/03/2008 15:48:53

Now let us see what happens when we execute it again. This will do the first parse after statistics were gathered and the cursor was marked for rolling invalidation:

15:55:35SQL> select count(*) from X;

COUNT(*)
----------
1590

15:55:37 SQL> select * from v$sql_shared_cursor where sql_id='95rckg79jgshh';

SQL_ID ADDRESS CHILD_AD CHILD_NUMBER U S O O S L S E B P I S T A B D L T
------------- -------- -------- ------------ - - - - - - - - - - - - - - - - - -
R I I R L I O S M U T N F A I T D L D B P C S R P T M B M R O P M F L
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95rckg79jgshh 3076145C 33EFD654 0 N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N


15:55:37 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBER PARSE_CALLSEXECUTIONSFIRST_LOAD_TIME LAST_LOAD_TIME
------------ ---------------------------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 332008-03-13/15:12:47 2008-03-13/15:48:42
13/03/200815:55:35

We can see that the same cursor has been reused, even though we waited for longer than the invalidation period parameter. This shows that the time period where the cursor will be invalidated does not begin until this first parse after statistics are modified has happened. This parse is what sets the Tmax timestamp.

Let us wait another 6 minutes, during this time we expect the timestamp Tmax to run out:

16:01:00 SQL> select * from v$sql_shared_cursor where sql_id='95rckg79jgshh';

SQL_ID ADDRESS CHILD_AD CHILD_NUMBER U S O O S L S E B P I S T A B D L T
------------- -------- -------- ------------ - - - - - - - - - - - - - - - - - -
R I I R L I O S M U T N F A I T D L D B P C S R P T M B M R O P M F L
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95rckg79jgshh 3076145C 33EFD654 0 N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N


16:01:01 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBER PARSE_CALLS EXECUTIONS FIRST_LOAD_TIME LAST_LOAD_TIME
------------ ----------- ---------- ------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 3 3 2008-03-13/15:12:47 2008-03-13/15:48:42
13/03/2008 15:55:35

Nothing happens to the cursor until we execute (and parse) it again after Tmax has passed:

16:01:08SQL> select count(*) from X;

COUNT(*)
----------
1590

16:01:09 SQL> select * from v$sql_shared_cursor where sql_id='95rckg79jgshh';

SQL_ID ADDRESS CHILD_ADCHILD_NUMBERU S O O S L S E B P I S T A B D L T
------------- -------- --------------------- - - - - - - - - - - - - - - - - -
R I I R L I O S M U T N F A I T D L D B P C S R P T M B MRO P M F L
- - - - - - - - - - - - - - - - - - - - - - - - - - - - --- - - - -
95rckg79jgshh 3076145C 33EFD6540N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N NNN N N N N

95rckg79jgshh 3076145C 3078DA101N N N N N N N N N N N N N N N N N N
N N N N N N N N N N N N N N N N N N N N N N N N N N N N NYN N N N N


16:01:10 SQL> select child_number,parse_calls,executions,first_load_time,
last_load_time,last_active_time from v$sql where sql_id='95rckg79jgshh';

CHILD_NUMBERPARSE_CALLSEXECUTIONSFIRST_LOAD_TIME LAST_LOAD_TIME
---------------------------------------------------- -------------------
LAST_ACTIVE_TIME
-------------------
0 3 3 2008-03-13/15:12:47 2008-03-13/15:48:42
13/03/2008 15:55:35

1112008-03-13/15:12:47 2008-03-13/16:01:09
13/03/200816:01:08

Here we now see that a new child cursor has been created and has 1 execution while the old cursor remains with its previous 3 executions. This shows that the original cursor was invalidated and a hard parse done. In V$SQL_SHARED_CURSOR the new child has a column set to Y (marked in bold), this corresponds to ROLL_INVALID_MISMATCH and indicates that the child had to be created because the original cursor could not be reused due to having been invalidated with rolling invalidation.

This completes the testcase.

http://www.jsqmd.com/news/763454/

相关文章:

  • 2026年山西精准获客与GEO生成式引擎优化深度横评指南 - 企业名录优选推荐
  • ThingsBoard MQTT上传数据避坑指南:连接失败、JSON格式错误、时间戳处理全解析
  • 量子-经典混合神经网络硬件资源评估与优化
  • 2026年山西精准获客、太原短视频代运营与晋中手机号定向完全指南 - 企业名录优选推荐
  • 孩子厌学逃学干预哪家专业?九州金榜一站式青少年心理与家庭教育解决方案 - 品牌企业推荐师(官方)
  • 开发者软技能文档库:提升技术协作与职业竞争力的实践指南
  • 让 AI 不再按过期文档写代码:AgentLockDoc 开源了
  • 深入PX4 Bootloader:从源码编译到自定义配置,打造你的专属飞控启动器
  • 2026年山西精准获客与短视频代运营完全指南:手机号定向推广、GEO优化、本地门店引流一体化解决方案 - 企业名录优选推荐
  • 从“捡回来”到玩转:ESP-01刷机后,如何用串口助手74880波特率查看启动日志与芯片信息
  • 交互式视频超分辨率技术:关键帧与智能传播
  • 上海庭院设计景观公司排行:5家靠谱公司深度盘点 - 真知灼见33
  • 【ISO/SAE 21434合规加速器】:Docker 27轻量化27步法——通过ASAM OpenSCENARIO V2.3认证的最小可信运行时构建指南
  • 九江黄金回收实测:福正美到手价比同行高8%的秘密 - 福正美黄金回收
  • 2026年内蒙环境检测哪家好?如何破解水质检测与废气检测难题 - 深度智识库
  • 专业视觉设计神器 Photoshop 2026 (PS)破解版下载安装教程
  • 2026年选毛刷厂家,掌握这三点绝不出错 - 品牌企业推荐师(官方)
  • 2026年5月新发布:山东地区精密管、精密钢管、合金无缝钢管优质厂商推荐,认准聊城市国顺钢管制造有限公司 - 2026年企业推荐榜
  • 在Ubuntu 22.04上,用Python脚本打通ROS2 Humble与科大讯飞SDK的简易语音控制方案
  • 【2026年最新600套毕设项目分享】速达物流信息查询微信小程序(30231)
  • 在 Node.js 服务中无缝接入 Taotoken 实现稳定的大模型调用
  • 用GBM预测信用卡逾期?手把手教你从数据清洗到模型上线的完整Pipeline(附Python代码)
  • 2026昆明婚纱摄影综合实力排名|4家口碑机构深度测评 备婚不踩坑 - 江湖评测
  • FramePack终极指南:免费AI视频生成神器,6GB显存制作60秒舞蹈大片
  • 广州优质白蚁防治公司推荐(越秀区/天河区/荔湾区/海珠区/白云区/番禺区上门除白蚁) - 品牌推荐大师
  • 别再让用户等!Unity WebGL加载速度提升指南:ASTC vs ETC2图片压缩格式怎么选?
  • 2026年想要选靠谱的电缆故障检测服务商,有哪些实用参考标准? - 品牌企业推荐师(官方)
  • 从文本到代码:arrowgram 双向转换工具的设计原理与实战应用
  • Downkyi终极指南:3步掌握B站视频下载神器,永久保存你的珍贵内容
  • 5月6日成都地区华岐产镀锌钢管(Q235B;内径DN15-200mm)今日价格 - 四川盛世钢联营销中心