【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程式設計師面試筆試寶典》,作者:小麥苗