[oracle]oracle 更新统计数据
schema 更新统计数据
SQL> exec dbms_stats.gather_schema_stats(ownname => 'c##sapr3',options => 'GATHER AUTO', estimate_percent => dbms_stats.auto_sample_size,method_opt => 'for all columns size repeat',degree =>15,cascade=>TRUE) PL/SQL procedure successfully completed. SQL> exec dbms_stats.gather_schema_stats(ownname => 'c##sapr3',options => 'GATHER AUTO', estimate_percent => dbms_stats.auto_sample_size,method_opt => 'FOR ALL COLUMNS SIZE AUTO',degree =>15,cascade=>TRUE); PL/SQL procedure successfully completed.
Table 更新统计数据
SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS('c##sapr3',tabname =>'USR02',estimate_percent=>80,method_opt=>'FOR ALL COLUMNS SIZE AUTO',degree=>4,cascade=>TRUE); PL/SQL procedure successfully completed.
index更新统计数据
EXEC DBMS_STATS.GATHER_INDEX_STATS('c##sapr3',"USRBF2^0",estimate_percent=>80,degree=>4);
BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'c##sapr3', tabname => '*', estimate_percent => 100, method_opt => 'for all columns size repeat', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; / BEGIN DBMS_STATS.GATHER_SCHEMA_STATS(ownname => 'c##sapr3', estimate_percent => 100, method_opt => 'for all columns size repeat', no_invalidate => FALSE, degree => 8, cascade => TRUE); END; /
参数说明:
ownname:要分析表的拥有者
tabname:要分析的表名.
partname:分区的名字,只对分区表或分区索引有用.
estimate_percent:采样行的百分比,取值范围[0.000001,100],null为全部分析,不采样. 常量:DBMS_STATS.AUTO_SAMPLE_SIZE是默认值,由oracle决定最佳取采样值.
block_sapmple:是否用块采样代替行采样.
method_opt:决定histograms信息是怎样被统计的.method_opt的取值如下(默认值为FOR ALL COLUMNS SIZE AUTO):
for all columns:统计所有列的histograms.
for all indexed columns:统计所有indexed列的histograms.
for all hidden columns:统计你看不到列的histograms
for columns <list> SIZE <N> | REPEAT | AUTO | SKEWONLY:统计指定列的histograms.N的取值范围[1,254]; REPEAT上次统计过的histograms;AUTO由oracle决定N的大小;SKEWONLY multiple end-points with the same value which is what we define by "there is skew in thedata
degree:决定并行度.默认值为null.
granularity:Granularity of statistics to collect ,only pertinent if the table is partitioned.
cascade:是收集索引的信息.默认为FALSE.
stattab:指定要存储统计信息的表,statid如果多个表的统计信息存储在同一个stattab中用于进行区分.statown存储统计信息表的拥有者.以上三个参数若不指定,统计信息会直接更新到数据字典.
no_invalidate: Does not invalidate the dependent cursors if set to TRUE. The procedure invalidates the dependent cursors immediately if set to FALSE.
force:即使表锁住了也收集统计信息.
GATHER_DATABASE_STATS #
This procedure gathers statistics for all objects in the database.
GATHER_DICTIONARY_STATS #
This procedure gathers statistics for dictionary schemas 'SYS', 'SYSTEM' and schemas of RDBMS components.
GATHER_FIXED_OBJECTS_STATS
#
This procedure gathers statistics for all fixed objects (dynamic performance tables).
GATHER_SYSTEM_STATS
#
This procedure gathers system statistics.
exec dbms_stats.flush_database_monitoring_info;
查看统计数据是否更新
SQL> col OWNER for A10 SQL> col TABLE_NAME for A30 SQL> col SUBPARTITION_NAME for A20 SQL> col OBJECT_TYPE for A15 SQL> select owner,table_name,last_analyzed from dba_tables where owner='c##sapr3'; SQL> select owner,table_name,object_type,stale_stats,last_analyzed from dba_tab_statistics where owner = 'c##sapr3' and (stale_stats = 'YES' or last_analyzed in null); SQL> SELECT OWNER,TABLE_NAME,OBJECT_TYPE,STALE_STATS,LAST_ANALYZED FROM DBA_TAB_STATISTICS WHERE (STALE_STATS='YES' OR LAST_ANALYZED IS NULL) AND OWNER NOT IN ('SYS','SYSTEM','SYSMAN','DMSYS','OLAPSYS','XDB','EXFSYS','CTXSYS','WMSYS','DBSNMP','ORDSYS','OUTLN','TSMSYS','MDSYS') AND TABLE_NAME NOT LIKE 'BIN%';
附
alter session set statistics_level=ALL; #session 级别 select /*+gather_plan_statistics*/... #语句级别