[每日一题] 11gOCP 1z0-052 :2013-09-6 DBMS_STATS.SET_TABLE_PREFS()使用...............................A37...

转载请注明出处:http://blog.youkuaiyun.com/guoyjoe/article/details/11358457



正确答案:D

Oracle 11g新特性之统计信息收集

在以前的数据库版本中,当收集到新的优化器统计信息时会自动发布。在11g中,这仍然是默认的动作,但你多了一个选择,

你可以将最新的统计信息挂起,直到人为地发布它们。DBMS_STATS.GET_PREFS函数运行你检查“PUBLISH”属性,查看统计信息

是否已经自动发布。默认返回的值为TRUE,意味着已经自动发布了,而FALSE表示它还处于挂起状态,等待发布。

检查当前数据库的PUBLISH”属性:

gyj@OCM> col get_prefs for a50
gyj@OCM> SELECT DBMS_STATS.get_prefs('PUBLISH','SH','CUSTOMERS') get_prefs FROM dual;

GET_PREFS
--------------------------------------------------
TRUE

使用存储过程DBMS_STATS.SET_TABLE_PREFS可以重新设置“PUBLISH”属性,设为FALSE表示它还处于挂起状态,等待发布:

gyj@OCM> BEGIN
  2  DBMS_STATS.SET_TABLE_PREFS('SH', 'CUSTOMERS', 'PUBLISH', 'false');
  3  END;
  4  /

PL/SQL procedure successfully completed.

gyj@OCM> SELECT DBMS_STATS.get_prefs('PUBLISH','SH','CUSTOMERS') get_prefs FROM dual;

GET_PREFS
--------------------------------------------------
FALSE

参考官方文档:

SET_TABLE_PREFS Procedure

This procedure isused to set the statistics preferences of the specified table in the specifiedschema.

Syntax

DBMS_STATS.SET_TABLE_PREFS (
 ownname IN VARCHAR2,
 tabname IN VARCHAR2,
 pname IN VARCHAR2,
 pvalue IN VARCHAR2);

Parameters

Table 141-93SET_TABLE_PREFS Procedure Parameters

Parameter

Description

ownname

Owner name

tabname

Table name

pname

Preference name. The default value for following preferences can be set:

· CASCADE

· DEGREE

· ESTIMATE_PERCENT

· METHOD_OPT

· NO_INVALIDATE

· GRANULARITY

· PUBLISH

· INCREMENTAL

· STALE_PERCENT

.

CASCADE- Determines whether or not index statistics are collected as part of gathering table statistics.

.

DEGREE- Determines degree of parallelism used for gathering statistics.

.

ESTIMATE_PERCENT- Determines the percentage of rows to estimate. The valid range is [0.000001,100]. Use the constantDBMS_STATS.AUTO_SAMPLE_SIZEto have Oracle determine the appropriate sample size for good statistics. This is the default.

.

METHOD_OPT- Controls column statistics collection and histogram creation. It accepts either of the following options, or both in combination:

· FOR ALL [INDEXED | HIDDEN] COLUMNS[size_clause]

· FOR COLUMNS [size clause] column [size_clause] [,column [size_clause]...]

size_clauseis defined assize_clause := SIZE {integer | REPEAT | AUTO | SKEWONLY}

columnis defined ascolumn := column_name | extension name | extension


- integer: Number of histogram buckets. Must be in the range [1,254].
- REPEAT: Collects histograms only on the columns that already have histograms
- AUTO: Oracle determines the columns to collect histograms based on data distribution and the workload of the columns.
- SKEWONLY: Oracle determines the columns to collect histograms based on the data distribution of the columns.
-column_name: Name of a column
-extension :Can be either a column group in the format of(column_name, colume_name [, ...])or an expression

The default isFOR ALL COLUMNS SIZE AUTO.

.

NO_INVALIDATE- The value controls the invalidation of dependent cursors of the tables for which statistics are being gathered. Does not invalidate the dependent cursors if set toTRUE. The procedure invalidates the dependent cursors immediately if set toFALSE. UseDBMS_STATS.AUTO_INVALIDATEto have Oracle decide when to invalidate dependent cursors. This is the default.

.

GRANULARITY- Determines granularity of statistics to collect (only pertinent if the table is partitioned).

'ALL'- Gathers all (subpartition, partition, and global) statistics

'AUTO'- Determines the granularity based on the partitioning type. This is the default value.

'DEFAULT'- Gathers global and partition-level statistics. This option is obsolete, and while currently supported, it is included in the documentation for legacy reasons only. You should use the 'GLOBAL AND PARTITION' for this functionality. Note that the default value is now 'AUTO'.

'GLOBAL'- Gathers global statistics

'GLOBAL AND PARTITION' - Gathers the global and partition level statistics. No subpartition level statistics are gathered even if it is a composite partitioned object.

'PARTITION'- Gathers partition-level statistics

'SUBPARTITION'- Gathers subpartition-level statistics.

.

PUBLISH- Determines whether or not newly gathered statistics will be published once the gather job has completed. Prior to Oracle Database 11g, Release 1 (11.1), once a statistic gathering job completed the new statistics were automatically published into the dictionary tables. The user now has the ability to gather statistics but not publish them immediately. This allows the DBA to test the new statistics before publishing them.

.

INCREMENTAL- Determines whether or not the global statistics of a partitioned table will be maintained without doing a full table scan. With partitioned tables it is very common to load new data into a new partition. As new partitions are added and data loaded, the global table statistics need to be kept up to date. Oracle will update the global table statistics by scanning only the partitions that have been changed instead of the entire table if the following conditions hold:

· INCREMENTALvalue for the partitioned table is set toTRUE;

· PUBLISHvalue for the partitioned table is set toTRUE;

· User specifiesAUTO_SAMPLE_SIZEforESTIMATE_PERCENTandAUTOforGRANULARITYwhen gathering statistics on the table.

If theINCREMENTALvalue for the partitioned table was set toFALSE(default value), a full table scan is used to maintain the global statistics which is a much more resource intensive and time-consuming operation for large tables.

.

STALE_PERCENT- Determines the percentage of rows in a table that have to change before the statistics on that table are deemed stale and should be regathered. The default value is 10%.

pvalue

Preference value. IfNULLis specified, it will set the Oracle default value.

Exceptions

ORA-20000: Object doesnot exist or insufficient privileges

ORA-20001: Invalid orillegal input values

Usage Notes

·To run this procedure, you need to connect as owner of the tableor should have theANALYZEANYsystemprivilege.

·All arguments are of typeVARCHAR2and values are enclosed in quotes, even when they representnumbers.

Examples

DBMS_STATS.SET_TABLE_PREFS('SH', 'SALES', 'CASCADE', 'DBMS_STATS.AUTO_CASCADE');
DBMS_STATS.SET_TABLE_PREFS('SH', 'SALES','ESTIMATE_PERCENT','9');
DBMS_STATS.SET_TABLE_PREFS('SH', 'SALES', 'DEGREE','99');
 

QQ:252803295

技术交流: DSI&Core Search Ⅰ 群:127149411(2000人技术群:未满) DSI&Core Search Ⅱ 群:177089463(1000人技术群:未满) DSI&Core Search Ⅲ 群:284596437(500人技术群:未满) DSI&Core Search Ⅳ 群:192136702(500人技术群:未满) DSI&Core Search Ⅴ 群:285030382(500人闲聊群:未满)

MAIL:dbathink@hotmail.com

BLOG:http://blog.youkuaiyun.com/guoyjoe

WEIBO:http://weibo.com/guoyJoe0218

ITPUB:http://www.itpub.net/space-uid-28460966.html

OCM: http://education.oracle.com/education/otn/YGuo.HTM


<think> 我们正在处理的是关于Oracle数据库统计信息锁定导致的ORA-20005和ORA-38029错误的问题。根据引用内容,这些错误通常发生在尝试收集或分析表的统计信息时,因为统计信息被锁定(stattype_locked为'ALL')。 原因分析: 1. 统计信息被锁定:这是为了防止统计信息被自动或手动更新,从而保持执行计划的稳定性。在Oracle 10g及以上版本中,默认会自动收集统计信息,但有时为了稳定执行计划,可能会使用`DBMS_STATS.LOCK_TABLE_STATS`或`DBMS_STATS.LOCK_SCHEMA_STATS`来锁定统计信息。 2. 锁定的影响:一旦锁定,任何尝试更新统计信息的操作(如`DBMS_STATS.GATHER_TABLE_STATS`或`ANALYZE`命令)都会失败,并抛出ORA-20005或ORA-38029错误。 解决办法: 1. 解锁统计信息:使用`DBMS_STATS.UNLOCK_TABLE_STATS`来解锁特定表的统计信息。语法为: ```sql EXEC DBMS_STATS.UNLOCK_TABLE_STATS(ownname => '用户名', tabname => '表名'); ``` 如果是多个表被锁定,可以批量生成解锁语句(如引用[5]所示): ```sql SELECT 'EXEC DBMS_STATS.UNLOCK_TABLE_STATS('''||owner||''','''||table_name||''');' FROM dba_tab_statistics WHERE owner='用户名' AND stattype_locked IS NOT NULL; ``` 注意:在PL/SQL Developer等工具中执行时,需要去掉EXEC并放在BEGIN...END块中。 2. 重新收集统计信息:解锁后,就可以正常收集统计信息了: ```sql EXEC DBMS_STATS.GATHER_TABLE_STATS(ownname => '用户名', tabname => '表名'); ``` 3. 如何避免:除非有特殊需求(如稳定执行计划),否则不要随意锁定统计信息。如果确实需要锁定,应该记录并在需要更新统计信息时解锁。 注意事项: - 权限:执行解锁操作需要相应的权限(通常需要具有DBA权限或该表的权限)。 - 批量操作:如果整个模式(schema)的统计信息被锁定,可以使用`DBMS_STATS.UNLOCK_SCHEMA_STATS`解锁整个模式。 相关问题: 1. 如何查询数据库中所有被锁定的统计信息? 2. 锁定统计信息对数据库性能有什么影响? 3. 在什么情况下需要锁定统计信息? 4. 如何永久避免统计信息被锁定导致的收集失败? 引用说明:我们在回答中参考了引用[1][2][3][4][5]的内容,其中引用[1][2][3][4][5]均提到了ORA-20005/ORA-38029错误与统计信息锁定有关,并提供了查询锁定状态和解锁的方法。 </think> ### 🔍 对象统计信息锁定问题(ORA-20005/ORA-38029)解析 #### 📌 错误本质 当 Oracle 数据库尝试更新表的统计信息时,若检测到统计信息已被锁定,会抛出两种等效错误: - **`ORA-20005: object statistics are locked (stattype = ALL)`** - **`ORA-38029: 对象统计信息已锁定`** 二者均表示 **统计信息处于人工锁定状态**,阻止自动或手动更新操作[^1][^2]。 --- ### ️ **核心原因分析** 1. **人为锁定统计信息** - 通过 `DBMS_STATS.LOCK_TABLE_STATS` 或 `DBMS_STATS.LOCK_SCHEMA_STATS` 显式锁定 - 目的:**稳定执行计划**,防止 Oracle 自动收集统计信息改变优化器行为[^3] 2. **Schema/表迁移操作** - 从其他数据库导入表时,若源库锁定了统计信息,导入后锁定状态会保留[^2] 3. **特殊维护需求** - 对关键业务表手动锁定统计信息,确保执行计划长期稳定 - 例如:避免因数据倾斜导致优化器选择低效执行计划 --- ### 🔧 **解决方案** #### ✅ 步骤 1:确认锁定状态 ```sql -- 查询被锁定的表 SELECT owner, table_name, stattype_locked FROM dba_tab_statistics WHERE stattype_locked = 'ALL'; -- ALL 表示完全锁定 ``` > 输出示例: > ``` > OWNER TABLE_NAME STATTYPE_LOCKED > ------ ----------- ---------------- > SYS TEST_TABLE ALL > ```[^1][^5] #### ✅ 步骤 2:解锁统计信息 ```sql -- 单表解锁 BEGIN DBMS_STATS.UNLOCK_TABLE_STATS( ownname => 'SYS', -- 模式名 tabname => 'TEST_TABLE' -- 表名 ); END; ``` -- **批量解锁**(适用于多表锁定) ```sql -- 生成解锁语句 SELECT 'EXEC DBMS_STATS.UNLOCK_TABLE_STATS('''||owner||''','''||table_name||''');' FROM dba_tab_statistics WHERE stattype_locked = 'ALL'; ``` > 执行生成的语句即可批量解锁[^5] #### ✅ 步骤 3:重新收集统计信息 ```sql BEGIN DBMS_STATS.GATHER_TABLE_STATS( ownname => 'SYS', tabname => 'TEST_TABLE' ); END; ```[^1] --- ### ️ **关键注意事项** 1. **锁定策略需谨慎** - 仅对关键业务表锁定统计信息 - 避免使用 `LOCK_SCHEMA_STATS` 锁定整个模式 - 锁定后需文档记录,避免遗忘[^3] 2. **迁移后的检查** - 从其他环境导入表后,立即检查 `dba_tab_statistics.stattype_locked` 3. **权限要求** - 解锁操作需要 `ANALYZE ANY` 或 `DBA` 权限 --- ### 💡 替代方案:临时禁用自动统计 若需保持执行计划稳定但不想永久锁定: ```sql -- 禁用表自动统计(不锁定) BEGIN DBMS_STATS.SET_TABLE_PREFS( ownname => 'SYS', tabname => 'TEST_TABLE', pname => 'AUTOSTATS_TARGET', pvalue => 'OFF' ); END; ``` --- ### ❓ 相关问题 1. **如何区分 `LOCK_TABLE_STATS` 和 `SET_TABLE_PREFS` 的使用场景?** 2. **统计信息锁定对 SQL 执行计划有何具体影响?** 3. **Oracle 自动统计收集机制如何检测锁定的统计信息?** 4. **批量解锁操作是否存在性能风险?** 5. **统计信息锁定与表锁定的本质区别是什么?** > 参考资料: > [^1]: ORA-20005 统计信息被锁定的原因与解决方案 > [^2]: 表迁移导致统计信息锁定的场景 > [^3]: 锁定统计信息的设计目的与风险 > [^4]: 统计信息锁定的技术实现细节 > [^5]: 批量解锁操作的实践方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值