Professional Documents
Culture Documents
In this post I'll try to summarize all sorts of statistics in Oracle, I strongly
recommend reading the full article, as it contains information you may find it
valuable in understanding Oracle statistics.
################################
Database | Schema | Table | Index Statistics
################################
Gather Database Stats:
===================
SQL> EXEC DBMS_STATS.GATHER_DATABASE_STATS(
ESTIMATE_PERCENT=>100,METHOD_OPT=>'FOR ALL COLUMNS SIZE SKEWONLY',
CASCADE => TRUE,
degree => 4,
OPTIONS => 'GATHER STALE',
GATHER_SYS => TRUE,
STATTAB => PROD_STATS);
CASCADE => TRUE :Gather statistics on the indexes as well. If not used Oracle wi
ll determine whether to collected or not.
DEGREE => 4 :Degree of parallelism.
options:
=>'GATHER' :Gathers statistics on all objects in the schema.
=>'GATHER AUTO' :Oracle determines which objects need new statistics, and
determines how to gather those statistics.
=>'GATHER STALE':Gathers statistics on stale objects. will return a list
of stale objects.
=>'GATHER EMPTY':Gathers statistics on objects have no statistics.will re
turn a list of no stats objects.
=>'LIST AUTO' : Returns a list of objects to be processed with GATHER AU
TO.
=>'LIST STALE': Returns a list of stale objects as determined by looking
at the *_tab_modifications views.
=>'LIST EMPTY': Returns a list of objects which currently have no statis
tics.
GATHER_SYS => TRUE :Gathers statistics on the objects owned by the 'SYS' user.
STATTAB => PROD_STATS :Table will save the current statistics. see SAVE & IMPORT
STATISTICS section -last third in this post-.
Note: All above parameters are valid for all stats kind (schema,table,..) except
Gather_SYS.
For faster execution:
-----------------SQL> EXEC DBMS_STATS.GATHER_DATABASE_STATS(
ESTIMATE_PERCENT=>DBMS_STATS.AUTO_SAMPLE_SIZE,degree => 8);
What's new?
ESTIMATE_PERCENT=>DBMS_STATS.AUTO_SAMPLE_SIZE => Let Oracle estimate skewed valu
es always gives excellent results.(DEFAULT).
Removed "METHOD_OPT=>'FOR ALL COLUMNS SIZE SKEWONLY'" => As histograms is not re
commended to be gathered on all columns.
Removed "cascade => TRUE" To let Oracle determine whether index statistics to b
e collected or not.
Doubled the "degree => 8" but this depends on the number of CPUs on the machine
and accepted CPU overhead during gathering DB statistics.
This task became an automated task starting with 10g to gathers statistics on al
l objects in the database having stale or missing statistics, To check the statu
s of that task:
SQL> select status from dba_autotask_client where client_name = 'auto optimizer
stats collection';
To Enable automatic optimizer statistics task:
SQL> BEGIN
DBMS_AUTO_TASK_ADMIN.ENABLE(
client_name => 'auto optimizer stats collection',
operation => NULL,
window_name => NULL);
END;
/
In case you want to Disable automatic optimizer statistics task:
SQL> BEGIN
DBMS_AUTO_TASK_ADMIN.DISABLE(
client_name => 'auto optimizer stats collection',
operation => NULL,
window_name => NULL);
END;
/
To check the tables that have stale statistics:
SQL> select OWNER,TABLE_NAME,LAST_ANALYZED,STALE_STATS from DBA_TAB_STATISTICS w
here STALE_STATS='YES';
This will gather statistics during the current workload [which supposed to be re
presentative of actual system I/O and CPU workload on the DB].
To gather WORKLOAD statistics:
SQL> execute dbms_stats.gather_system_stats('start');
Once the workload window ends after 1,2,3.. hours or whatever, stop the system s
tatistics gathering:
SQL> execute dbms_stats.gather_system_stats('stop');
You can use time interval (minutes) instead of issuing start/stop command manual
ly:
SQL> execute dbms_stats.gather_system_stats('interval',60);
Check the system values collected:
------------------------------col pname format a20
col pval2 format a40
select * from sys.aux_stats$;
cpuspeedNW: Shows the noworkload CPU speed, (average number of CPU cycles per s
econd).
ioseektim:
The sum of seek time, latency time, and OS overhead time.
iotfrspeed: I/O transfer speed,tells optimizer how fast the DB can read data in
a single read request.
cpuspeed:
Stands for CPU speed during a workload statistics collection.
maxthr:
The maximum I/O throughput.
slavethr:
Average parallel slave I/O throughput.
sreadtim:
The Single Block Read Time statistic shows the average time for a
random single block read.
mreadtim:
The average time (seconds) for a sequential multiblock read.
mbrc:
The average multiblock read count in blocks.
Notes:
-When gathering NOWORKLOAD statistics it will gather (cpuspeedNW, ioseektim, iot
frspeed) system statistics only.
-Above values can be modified manually using DBMS_STATS.SET_SYSTEM_STATS procedu
re.
-According to Oracle, collecting workload statistics doesn't impose an additiona
l overhead on your system.
Delete system statistics:
--------------------SQL> execute dbms_stats.delete_system_stats();
####################
Data Dictionary Statistics
####################
Facts:
----> Dictionary tables are the tables owned by SYS and residing in the system table
space.
> Normally data dictionary statistics in 9i is not required unless performance i
ssues are detected.
> In 10g Statistics on the dictionary tables will be maintained via the automati
c statistics gathering job run during the nightly maintenance window.
If you choose to switch off that job for application schema consider leaving it
on for the dictionary tables. You can do this by changing the value of AUTOSTATS
_TARGET from AUTO to ORACLE using the procedure:
#############
Extended Statistics "11g onwards"
#############
Extended statistics can be gathered on columns based on functions or column grou
ps.
Gather extended stats on column function:
=====================================
If you run a query having in the WHERE statement a function like upper/lower the
optimizer will be off and index on that column will not be used:
SQL> select count(*) from EMP where lower(ename) = 'scott';
In order to make optimizer work with function based terms you need to gather ext
ended stats:
1-Create extended stats:
>>>>>>>>>>>>>>>>>>>>>
SQL> select dbms_stats.create_extended_stats('SCOTT','EMP','(lower(ENAME))') fro
m dual;
2-Gather histograms:
>>>>>>>>>>>>>>>>>>
SQL> exec dbms_stats.gather_table_stats('SCOTT','EMP', method_opt=> 'for all col
umns size skewonly');
OR
-*You can do it also in one Step:
>>>>>>>>>>>>>>>>>>>>>>>>>
SQL> Begin dbms_stats.gather_table_stats
(ownname => 'SCOTT',tabname => 'EMP',
method_opt => 'for all columns size skewonly for
columns (lower(ENAME))');
end;
/
To check the Existance of extended statistics on a table:
--------------------------------------------------SQL> select extension_name,extension from dba_stat_extensions where owner='SCOTT
'and table_name = 'EMP';
SYS_STU2JLSDWQAFJHQST7$QK81_YB (LOWER("ENAME"))
Drop extended stats on column function:
------------------------------------SQL> exec dbms_stats.drop_extended_stats('SCOTT','EMP','(LOWER("ENAME"))');
Gather extended stats on column group: -related columns=================================
Certain columns in a table that are part of a join condition (where statement a
re correlated e.g.(country,state). You want to make the optimizer aware of this
relationship between two columns and more instead of using separate statistics f
or each columns. By creating extended statistics on a group of columns, the Opti
mizer can determine a more accurate the relation between the columns are used to
gether in a where clause of a SQL statement. e.g. columns like country_id and st
ate_name the have a relationship, state like Texas can only be found in USA so t
he value of state_name are always influenced by country_id.
If there are extra columns are referenced in the "WHERE statement with the colu
mn group the optimizer will make use of column group statistics.
1- create a column group:
>>>>>>>>>>>>>>>>>>>>>
SQL> select dbms_stats.create_extended_stats('SH','CUSTOMERS', '(country_id,cust
_state_province)')from dual;
2- Re-gather stats|histograms for table so optimizer can use the newly generated
extended statistics:
>>>>>>>>>>>>>>>>>>>>>>>
SQL> exec dbms_stats.gather_table_stats ('SH','customers',method_opt=> 'for all
columns size skewonly');
OR
--*You can do it also in one Step:
>>>>>>>>>>>>>>>>>>>>>>>>>
SQL> Begin dbms_stats.gather_table_stats
(ownname => 'SH',tabname => 'CUSTOMERS',
method_opt => 'for all columns size skewonly for
columns (country_id,cust_state_province)');
end;
/
Drop extended stats on column group:
-------------------------------------SQL> exec dbms_stats.drop_extended_stats('SH','CUSTOMERS', '(country_id,cust_sta
te_province)');
########
Histograms
########
For Index:
SQL> Exec dbms_stats.export_INDEX_stats(ownname => 'SCOTT',indname => 'PK_EMP',s
tattab => 'prod_stats');
For Column:
SQL> Exec dbms_stats.export_COLUMN_stats (ownname=>'SCOTT',tabname=>'EMP',colnam
e=>'EMPNO',stattab=>'prod_stats');
3-Import the statistics from PROD_STATS table to the dictionary:
-------------------------------------------------------------------For Database stats:
SQL> Exec DBMS_STATS.IMPORT_DATABASE_STATS
(stattab => 'prod_stats',statown => 'SYSTEM');
For System stats:
SQL> Exec DBMS_STATS.IMPORT_SYSTEM_STATS
(stattab => 'prod_stats',statown => 'SYSTEM');
For Dictionary stats:
SQL> Exec DBMS_STATS.IMPORT_Dictionary_STATS
(stattab => 'prod_stats',statown => 'SYSTEM');
For Fixed Tables stats:
SQL> Exec DBMS_STATS.IMPORT_FIXED_OBJECTS_STATS
(stattab => 'prod_stats',statown => 'SYSTEM');
For Schema stats:
SQL> Exec DBMS_STATS.IMPORT_SCHEMA_STATS
(ownname => 'SCOTT',stattab => 'prod_stats', statown => 'SYSTEM');
For Table stats and it's indexes:
SQL> Exec dbms_stats.import_TABLE_stats
( ownname => 'SCOTT', stattab => 'prod_stats',tabname => 'EMP');
For Index:
SQL> Exec dbms_stats.import_INDEX_stats
( ownname => 'SCOTT', stattab => 'prod_stats', indname => 'PK_EMP');
For COLUMN:
SQL> Exec dbms_stats.import_COLUMN_stats
(ownname=>'SCOTT',tabname=>'EMP',colname=>'EMPNO',stattab=>'prod_stats');
4-Drop Stat Table:
------------------SQL> Exec dbms_stats.DROP_STAT_TABLE (stattab => 'prod_stats',ownname => 'SYSTEM
');
===============
Restore statistics: -From Dictionary===============
Old statistics are saved automatically in SYSAUX for 31 day.
Restore Dictionary stats as of timestamp:
----------------------------------------SQL> Exec DBMS_STATS.RESTORE_DICTIONARY_STATS(sysdate-1);
Restore Database stats as of timestamp:
-------------------------------------SQL> Exec DBMS_STATS.RESTORE_DATABASE_STATS(sysdate-1);
Restore SYSTEM stats as of timestamp:
-------------------------------------SQL> Exec DBMS_STATS.RESTORE_SYSTEM_STATS(sysdate-1);
Restore FIXED OBJECTS stats as of timestamp:
----------------------------------------------SQL> Exec DBMS_STATS.RESTORE_FIXED_OBJECTS_STATS(sysdate-1);