【DB笔试面试632】在Oracle中,如何锁住统计信息?
- 2019 年 10 月 10 日
- 筆記
题目部分
在Oracle中,如何锁住统计信息?
♣
答案部分
Oracle会自动收集表的统计信息,大部分情况下,这种行为是有利的。当不需要对某个表做收集的时候,可以采用锁定统计信息的方法,把不需要收集的表排除在外,这样可以使得此表上的统计信息不变,如下所示:
EXEC DBMS_STATS.LOCK_TABLE_STATS(); --表 EXEC DBMS_STATS.LOCK_PARTITION_STATS(); --分区表 EXEC DBMS_STATS.LOCK_SCHEMA_STATS(); --锁定用户统计信息 EXEC DBMS_STATS.UNLOCK_SCHEMA_STATS();--解锁用户统计信息
如果在锁定条件下收集统计信息,那么会出现如下报错:
ERROR at line 1: ORA-20005: object statistics are locked (stattype = ALL) ORA-06512: at "SYS.DBMS_STATS", line 20337 ORA-06512: at "SYS.DBMS_STATS", line 20360 ORA-06512: at line 1
可以使用如下的SQL查询到统计信息被锁的表或索引:
SELECT D.OWNER, D.INDEX_NAME, D.TABLE_OWNER, D.TABLE_NAME, D.PARTITION_NAME, D.SUBPARTITION_NAME, D.OBJECT_TYPE FROM DBA_IND_STATISTICS D WHERE STATTYPE_LOCKED = 'ALL' UNION ALL SELECT '', '', D.OWNER, D.TABLE_NAME, D.PARTITION_NAME, D.SUBPARTITION_NAME, D.OBJECT_TYPE FROM DBA_TAB_STATISTICS D WHERE STATTYPE_LOCKED = 'ALL' ;
真题1、You have statistics collected for some selected tables. Your requirement is that the statistics for the tables and all dependent indexes must not be overwritten by further statistics collection until a certain point of time. How would you achieve this?
A、Lock statistics for the tables.
B、Change STALE_PERCENT to zero for the tables.
C、Set the TIMED_ST A TISTICS parameter to TRUE.
D、Set the ST ATISTICS_LEVEL parameter to BASIC.
E、Set the OPTIMIZER_USE_PENDING parameter statistics to TRUE.
答案:A。
若要保持统计信息不被覆盖,则必须利用DBMS_STATS包将统计信息锁住。
本文选自《Oracle程序员面试笔试宝典》,作者:小麦苗