Automatic statistics gathering should be sufficient for most database objects which are being modified at a moderate speed. However, there are cases where automatic statistics gathering may not be adequate. Because the automatic statistics gathering runs during an overnight batch window, the statistics on tables which are significantly modified during the day may become stale.
There may be two scenarios in this case.
•Volatile tables that are being deleted or truncated and rebuilt during the course of the day.
•Objects which are the target of large bulk loads which add 10% or more to the object's total size.
So you may wish to manually gather statistics of those objects in order to choose the optimizer the best execution plan. There are two ways to gather statistics.
1)Using DBMS_STATS package.
2)Using ANALYZE command.
A)Using DBMS_STATS package
--------------------------------------------
The DBMS_STATS package have several procedures which help to generate statistics.
1)GATHER_DATABASE_STATS Procedures-Gathers statistics for all objects in the database
2)GATHER_DICTIONARY_STATS Procedure-Gathers statistics for dictionary schemas 'SYS', 'SYSTEM' and schemas of RDBMS components.
3)GATHER_FIXED_OBJECTS_STATS Procedure-Gathers statistics of fixed objects.
4)GATHER_INDEX_STATS Procedure-Gathers index statistics.
5)GATHER_SCHEMA_STATS Procedures-Gathers statistics for all objects in a schema.
6)GATHER_SYSTEM_STATS Procedure-Gathers system statistics.
7)GATHER_TABLE_STATS Procedure-Gathers table and column (and index) statistics.
8)GENERATE_STATS Procedure-Generates object statistics from previously collected statistics of related objects. The currently supported objects are only b-tree and bitmap indexes.
Example:
To gather statistics of all objects inside A schema use
SQL>EXEC DBMS_STATS.GATHER_SCHEMA_STATS('A');
PL/SQL procedure successfully completed.
To gather statictics of table test in ARJU schema use,
SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS('ARJU','TEST');
PL/SQL procedure successfully completed.
2)Using Analyze:
------------------------
Oracle strongly recommend not use ANALYZE command to estimate statistics. Yet it is supported for backward compatibility. To generate statistics of TEST table using ANALYZE use ANALYZE with estimate statistics keyword.,
In this example I verified that statistics of table column num_rows contain information after analyzing.
SQL> SELECT NUM_ROWS FROM USER_TABLES WHERE TABLE_NAME='TEST';
NUM_ROWS
----------
2
SQL> INSERT INTO TEST VALUES('before');
1 row created.
SQL> SELECT NUM_ROWS FROM USER_TABLES WHERE TABLE_NAME='TEST';
NUM_ROWS
----------
2
SQL> ANALYZE TABLE TEST COMPUTE STATISTICS;
Table analyzed.
SQL> SELECT NUM_ROWS FROM USER_TABLES WHERE TABLE_NAME='TEST';
NUM_ROWS
----------
3
After gather statistics num_rows contain accurate information.
No comments:
Post a Comment