SQL> exec dbms_stats.alter_schema_tab_monitoring('xys');
PL/SQL 过程已成功完成。
SQL> select * from tt;
ID N
---------- -
1 a
2 A
3 B
4 b
5 c
SQL> update tt set name='m';
已更新5行。
SQL> commit;
提交完成。
--user_tab_modifications中没有记录,oracle flush sga
到user_tab_modifications可能需要一段时间,不过我们可以
通过DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO来手动flush
SQL> select * from user_tab_modifications;
未选定行
--手动flush sga的信息到user_tab_modifications
SQL> exec DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO ;
PL/SQL 过程已成功完成。
SQL> desc user_tab_modifications
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
TABLE_NAME VARCHAR2(30)
PARTITION_NAME VARCHAR2(30)
SUBPARTITION_NAME VARCHAR2(30)
INSERTS NUMBER
UPDATES NUMBER
DELETES NUMBER
TIMESTAMP DATE
TRUNCATED VARCHAR2(3)
DROP_SEGMENTS NUMBER
SQL> select * from user_tab_modifications;
TABLE P SUBPA INSERTS UPDATES DELETES TIMESTAMP TRU DROP_SEGMENTS
----- - ----- ---------- ---------- ---------- -------------- --- -------------
TT 0 5 0 17-4月 -08 NO 0
SQL> edit
已写入 file afiedt.buf
1 declare
2 v_obj dbms_stats.objecttab;
3 begin
4 dbms_stats.gather_schema_stats
5 (OWNNAME=>'XYS',OPTIONS=>'LIST STALE',OBJLIST=>v_obj );
6 for i in 1 .. v_obj .count
7 loop
8 dbms_output.put_line( v_obj (i).ownname );
9 dbms_output.put_line( v_obj (i).objType );
10 dbms_output.put_line( v_obj (i).objName );
11 dbms_output.put_line( v_obj (i).PartName );
12 dbms_output.put_line( v_obj (i).subPartName );
13 dbms_output.put_line
14 ( '-------------------------' );
15 end loop;
16* end;
SQL> /
PL/SQL 过程已成功完成。
Objects are considered stale when 10% of the total rows have been changed. When you issue
GATHER_TABLE_STATS with GATHER STALE, the procedure checks the USER_TAB_MODIFICATIONS view.
If a monitored table has been modified more than 10%, then statistics are gathered again.
The information about changes of tables, as shown in the USER_TAB_MODIFICATIONS view, can be
flushed from the SGA into the data dictionary with the
DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO procedure.
数据的10%被修改的时其statistics被认为就是失真的,那么这个对象就被记录在
user_tab_modifications中,不过我们来验证一下oracle是否会自动搜集具有stale statistics的对象
的statistics呢?
SQL> SET SERVEROUTPUT ON
SQL> /
XYS
TABLE
TT
PL/SQL 过程已成功完成。
--通过下面试验验证oracle不会对那些stale statistics的对象进行statistics自动搜集,仅仅是监视而已。
SQL> select table_name , num_rows,blocks,avg_row_len from user_tables where tabl
e_name='TT';
TABLE NUM_ROWS BLOCKS AVG_ROW_LEN
----- ---------- ---------- -----------
TT 5 5 8
SQL> select * from tt;
ID N
---------- -
1 m
2 m
3 m
4 m
5 m
SQL> insert into tt select *from tt;
已创建5行。
SQL> commit;
提交完成。
--oracle到底什么时候会把sga中的信息flush到user_tab_modifications中也是我的疑问
SQL> select * from user_tab_modifications;
TABLE P SUBPA INSERTS UPDATES DELETES TIMESTAMP TRU DROP_SEGMENTS
----- - ----- ---------- ---------- ---------- -------------- --- -------------
TT 0 5 0 17-4月 -08 NO 0
SQL> select * from user_tab_modifications;
TABLE P SUBPA INSERTS UPDATES DELETES TIMESTAMP TRU DROP_SEGMENTS
----- - ----- ---------- ---------- ---------- -------------- --- -------------
TT 0 5 0 17-4月 -08 NO 0
SQL> select * from user_tab_modifications;
TABLE P SUBPA INSERTS UPDATES DELETES TIMESTAMP TRU DROP_SEGMENTS
----- - ----- ---------- ---------- ---------- -------------- --- -------------
TT 0 5 0 17-4月 -08 NO 0
--等不及了,还是手动flush一下吧
SQL> exec DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO ;
PL/SQL 过程已成功完成。
SQL> select * from user_tab_modifications;
TABLE P SUBPA INSERTS UPDATES DELETES TIMESTAMP TRU DROP_SEGMENTS
----- - ----- ---------- ---------- ---------- -------------- --- -------------
TT 5 5 0 17-4月 -08 NO 0
SQL> select table_name , num_rows,blocks,avg_row_len from user_tables where tabl
e_name='TT';
TABLE NUM_ROWS BLOCKS AVG_ROW_LEN
----- ---------- ---------- -----------
TT 5 5 8
SQL>
上面查询结果显示 NUM_ROWS=5而不是现在的10行数据,也就说oracle不会对那些具有stale statistics的对象自动搜集statistics。
--不过如果有了监视信息,之后可以通过参数OPTIONS=>'GATHER STALE'对这些具有stale statistics
的对象搜集新的statistics。
SQL> edit
已写入 file afiedt.buf
1 declare
2 v_obj dbms_stats.objecttab;
3 begin
4 dbms_stats.gather_schema_stats
5 (OWNNAME=>'XYS',OPTIONS=>'GATHER STALE',OBJLIST=>v_obj );
6 for i in 1 .. v_obj .count
7 loop
8 dbms_output.put_line( v_obj (i).ownname );
9 dbms_output.put_line( v_obj (i).objType );
10 dbms_output.put_line( v_obj (i).objName );
11 dbms_output.put_line( v_obj (i).PartName );
12 dbms_output.put_line( v_obj (i).subPartName );
13 dbms_output.put_line
14 ( '-------------------------' );
15 end loop;
16* end;
SQL> /
XYS
TABLE
TT
-------------------------
PL/SQL 过程已成功完成。
SQL> select * from user_tab_modifications;
未选定行
SQL> select table_name , num_rows,blocks,avg_row_len from user_tables where tabl
e_name='TT';
TABLE NUM_ROWS BLOCKS AVG_ROW_LEN
----- ---------- ---------- -----------
TT 10 5 7
SQL>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/19602/viewspace-1002659/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/19602/viewspace-1002659/