Oracle 10g rebuild index,索引原來統計資訊丟失

ljm0211發表於2012-07-04

1、建立測試表及索引

create table t as select * from dba_objects where rownum<=100;
create index ind_t_object_id on t(object_id);

create table tt (id number,name varchar2(
30));
insert into tt values(
1,'aaa');
insert into tt values(
2,'bbb');
insert into tt values(
3,'ccc');

commit;

 

2、檢視建立物件當前的統計資訊

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊為空;
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊存在; rowcnt100
select * from dba_tab_statistics where wner='DBMGR' and table_name='TT';

統計資訊為空;

 

3、收集表的統計資訊

begin
dbms_stats.gather_table_stats(ownname => 'DBMGR',tabname => 'T', cascade => true);
end;

 

4、檢視備份的統計資訊
select * from sys.WRI$_OPTSTAT_tab_HISTORY where obj#=357525 order by savtime desc;

備份正常,備份了一條空的統計資訊;備份記錄中savtime04-JUL-12 04.56.21.319354 PM +08:00

 

select * from sys.WRI$_OPTSTAT_IND_HISTORY where obj#=357526 order by savtime desc;

備份正常,備份了一條rowcnt100analyzetime2012/7/4 16:50:50 的統計資訊;備份記錄中savtime04-JUL-12 04.56.21.495591 PM +08:00

 

5、檢視兩個物件的資訊

select * from dba_objects where wner='DBMGR' and object_name in ('T','IND_T_OBJECT_ID');

createdlast_ddl_time 都是2012/7/4 16:50:50

 

6、檢視建立物件當前的統計資訊

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊:num_rows: 100 last_analyzed2012/7/4 16:56:21
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊:num_rows: 100 last_analyzed2012/7/4 16:56:21

 

7、為了便於觀察,刪除表中1條資料,重新收集統計資訊,檢視統計資訊

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊:num_rows: 99 last_analyzed2012/7/4 17:12:44
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊:num_rows: 99 last_analyzed2012/7/4 17:12:44

 

 

7、執行restore,恢復最初的統計資訊

begin
dbms_stats.restore_table_stats(ownname => 'DBMGR',tabname => 'T', AS_OF_TIMESTAMP => '04-JUL-12 04.56.20.495591 PM +08:00');
end;

 

8、檢視建立物件當前的統計資訊

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊為空; 恢復正確
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊存在; rowcnt100 last_analyzed2012/7/4 16:50:50  恢復正確

 

9、檢視當前備份的索引統計資訊,如圖,都是正確的

 

10、索引當前統計資訊:rowcnt100 last_analyzed2012/7/4 16:50:50;為了便於觀察,再刪除表中10條資料,然後rebuild index

alter index dbmgr.IND_T_OBJECT_ID rebuild online;

我們期望看到新增一條rowcnt100的統計資訊備份,但是實際新增統計資訊如下,都是空的。

 

11、當前的統計資訊情況

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊為空;
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊存在; rowcnt89 last_analyzed2012/7/4 18:20:41rebuild index更新了統計資訊

 

12、嘗試恢復索引統計資訊

begin
dbms_stats.restore_table_stats(ownname => 'DBMGR',tabname => 'T', AS_OF_TIMESTAMP => ' 04-JUL-12 06.20.40.126087 PM +08:00');
end;

 

13、檢視當前的統計資訊情況

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊為空;
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊為空;索引統計資訊無法恢復到04-JUL-12 06.20.41.126087 PM +08:00 備份前的統計資訊;

 

=============================================================================================

補充測試:

再刪除10條資料,

begin
dbms_stats.gather_table_stats(ownname => 'DBMGR',tabname => 'T', cascade => true);
end;

 

select * from dba_tab_statistics where wner='DBMGR' and table_name='T';

統計資訊:num_rows: 79 last_analyzed2012/7/4 18:31:17
select * from dba_ind_statistics where wner='DBMGR' and index_name='IND_T_OBJECT_ID';

統計資訊:num_rows: 79 last_analyzed2012/7/4 18:31:18

 

索引備份的統計資訊:

 

再刪除10條資料

alter index dbmgr.IND_T_OBJECT_ID rebuild online;

期望看到新增一條rowcnt79的統計資訊備份,索引新的統計資訊變為num_rows69last_analyzed2012/7/4 18:37:51

結果備份了一條空統計資訊:

執行恢復

begin
dbms_stats.restore_table_stats(ownname => 'DBMGR',tabname => 'T', AS_OF_TIMESTAMP => ' 04-JUL-12 06.35.30.958529 PM +08:00');
end;

 

索引統計資訊被恢復為空了,無法在恢復出num_rows:79 last_analyzed2012/7/4 18:31:18的統計資訊;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11411056/viewspace-734586/,如需轉載,請註明出處,否則將追究法律責任。

相關文章