This article purpose is to record some daily oracle table statisics management procedure usage and some tips for while manage table statistics. Not big deal, if you're familiar with this, pass it.
GATHER_INDEX_STATS Procedure
This procedure gathers index statistics. It attempts to parallelize as much of the work as possible. Restrictions are described in the individual parameters. This operation will not parallelize with certain types of indexes, including cluster indexes, domain indexes, and bitmap join indexes. The granularity
and no_invalidate
arguments are not relevant to these types of indexes.
DBMS_STATS.GATHER_INDEX_STATS (
ownname VARCHAR2,
indname VARCHAR2,
partname VARCHAR2 DEFAULT NULL,
estimate_percent NUMBER DEFAULT to_estimate_percent_type
(GET_PARAM('ESTIMATE_PERCENT')),
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
statown VARCHAR2 DEFAULT NULL,
degree NUMBER DEFAULT to_degree_type(get_param('DEGREE')),
granularity VARCHAR2 DEFAULT GET_PARAM('GRANULARITY'),
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type
(GET_PARAM('NO_INVALIDATE')),
force BOOLEAN DEFAULT FALSE);
Table 142-42 GATHER_INDEX_STATS Procedure Parameters
Parameter Description
|
Schema of index to analyze |
|
Name of index |
|
Name of partition |
|
Percentage of rows to estimate ( |
|
User statistics table identifier describing where to save the current statistics |
|
Identifier (optional) to associate with these statistics within |
|
Schema containing |
|
Degree of parallelism. The default for |
|
Granularity of statistics to collect (only pertinent if the table is partitioned).
'
|
|
Does not invalidate the dependent cursors if set to |
|
Gather statistics on object even if it is locked |
To invoke this procedure you must be owner of the table, or you need the ANALYZE
ANY
privilege. For objects owned by SYS
, you need to be either the owner of the table, or you need the ANALYZE
ANY
DICTIONARY
privilege or the SYSDBA
privilege.
GATHER_SCHEMA_STATS Procedures
This procedure gathers statistics for all objects in a schema.
DBMS_STATS.GATHER_SCHEMA_STATS (
ownname VARCHAR2,
estimate_percent NUMBER DEFAULT to_estimate_percent_type
(get_param('ESTIMATE_PERCENT')),
block_sample BOOLEAN DEFAULT FALSE,
method_opt VARCHAR2 DEFAULT get_param('METHOD_OPT'),
degree NUMBER DEFAULT to_degree_type(get_param('DEGREE')),
granularity VARCHAR2 DEFAULT GET_PARAM('GRANULARITY'),
cascade BOOLEAN DEFAULT to_cascade_type(get_param('CASCADE')),
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
options VARCHAR2 DEFAULT 'GATHER',
objlist OUT ObjectTab,
statown VARCHAR2 DEFAULT NULL,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type (
get_param('NO_INVALIDATE')),
force BOOLEAN DEFAULT FALSE,
obj_filter_list ObjectTab DEFAULT NULL);
DBMS_STATS.GATHER_SCHEMA_STATS (
ownname VARCHAR2,
estimate_percent NUMBER DEFAULT to_estimate_percent_type
(get_param('ESTIMATE_PERCENT')),
block_sample BOOLEAN DEFAULT FALSE,
method_opt VARCHAR2 DEFAULT get_param('METHOD_OPT'),
degree NUMBER DEFAULT to_degree_type(get_param('DEGREE')),
granularity VARCHAR2 DEFAULT GET_PARAM('GRANULARITY'),
cascade BOOLEAN DEFAULT to_cascade_type(get_param('CASCADE')),
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
options VARCHAR2 DEFAULT 'GATHER',
statown VARCHAR2 DEFAULT NULL,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type (
get_param('NO_INVALIDATE'),
force BOOLEAN DEFAULT FALSE,
obj_filter_list ObjectTab DEFAULT NULL);
Table 142-43 GATHER_SCHEMA_STATS Procedure Parameters
Parameter Description
|
Schema to analyze ( |
|
Percentage of rows to estimate ( |
|
Whether or not to use random block sampling instead of random row sampling. Random block sampling is more efficient, but if the data is not randomly distributed on disk, then the sample values may be somewhat correlated. Only pertinent when doing an estimate statistics. |
|
Accepts:
- integer : Number of histogram buckets. Must be in the range [1,254].- REPEAT : Collects histograms only on the columns that already have histograms- AUTO : Oracle determines the columns on which to collect histograms based on data distribution and the workload of the columns.- SKEWONLY : Oracle determines the columns on which to collect histograms based on the data distribution of the columns. The default is |
|
Degree of parallelism. The default for |
|
Granularity of statistics to collect (only pertinent if the table is partitioned).
'
|
|
Gather statistics on the indexes as well. Using this option is equivalent to running the GATHER_INDEX_STATS Procedure on each of the indexes in the schema in addition to gathering table and column statistics. Use the constant |
|
User statistics table identifier describing where to save the current statistics |
|
Identifier (optional) to associate with these statistics within |
|
Further specification of which objects to gather statistics for:
|
|
List of objects found to be stale or empty |
|
Schema containing |
|
Does not invalidate the dependent cursors if set to |
|
Gather statistics on objects even if they are locked |
|
A list of object filters. When provided, |
To invoke this procedure you must be owner of the table, or you need the ANALYZE
ANY
privilege. For objects owned by SYS
, you need to be either the owner of the table, or you need the ANALYZE
ANY
DICTIONARY
privilege or the SYSDBA
privilege.
When you use a specific value for the sampling percentage, DBMS_STATS
honors it except for when:
-
The result is less than 2500 rows (too small a sample) and
-
The specified percentage is more than the certain percentage.
Applying an Object Filter List
The following example specifies that any table with a "T
" prefix in the SAMPLE
schema and any table in the SYS
schema, if stale, will have statistics gathered upon it.
DECLARE
filter_lst DBMS_STATS.OBJECTTAB := DBMS_STATS.OBJECTTAB();
BEGIN
filter_lst.extend(2);
filter_lst(1).ownname := 'SH';
filter_lst(1).objname := 'SALES';
filter_lst(2).ownname := 'SH';
filter_lst(2).objname := 'COSTS';
DBMS_STATS.GATHER_SCHEMA_STATS(ownname=>'SH',obj_filter_list=>filter_lst);
END;
GATHER_TABLE_STATS Procedure
This procedure gathers table and column (and index) statistics. It attempts to parallelize as much of the work as possible, but there are some restrictions as described in the individual parameters.
DBMS_STATS.GATHER_TABLE_STATS (
ownname VARCHAR2,
tabname VARCHAR2,
partname VARCHAR2 DEFAULT NULL,
estimate_percent NUMBER DEFAULT to_estimate_percent_type
(get_param('ESTIMATE_PERCENT')),
block_sample BOOLEAN DEFAULT FALSE,
method_opt VARCHAR2 DEFAULT get_param('METHOD_OPT'),
degree NUMBER DEFAULT to_degree_type(get_param('DEGREE')),
granularity VARCHAR2 DEFAULT GET_PARAM('GRANULARITY'),
cascade BOOLEAN DEFAULT to_cascade_type(get_param('CASCADE')),
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
statown VARCHAR2 DEFAULT NULL,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type (
get_param('NO_INVALIDATE')),
stattype VARCHAR2 DEFAULT 'DATA',
force BOOLEAN DEFAULT FALSE);
Table 142-45 GATHER_TABLE_STATS Procedure Parameters
Parameter Description
|
Schema of table to analyze |
|
Name of table |
|
Name of partition |
|
Percentage of rows to estimate ( |
|
Whether or not to use random block sampling instead of random row sampling. Random block sampling is more efficient, but if the data is not randomly distributed on disk, then the sample values may be somewhat correlated. Only pertinent when doing an estimate statistics. |
|
Accepts either of the following options, or both in combination:
- integer : Number of histogram buckets. Must be in the range [1,254].- REPEAT : Collects histograms only on the columns that already have histograms- AUTO : Oracle determines the columns on which to collect histograms based on data distribution and the workload of the columns.- SKEWONLY : Oracle determines the columns on which to collect histograms based on the data distribution of the columns.- column_name : Name of a column- extension : can be either a column group in the format of (column_name , Colume_name [, ...]) or an expression The default is |
|
Degree of parallelism. The default for |
|
Granularity of statistics to collect (only pertinent if the table is partitioned).
'
|
|
Gathers statistics on the indexes for this table. Using this option is equivalent to running the GATHER_INDEX_STATS Procedure on each of the table's indexes. Use the constant |
|
User statistics table identifier describing where to save the current statistics |
|
Identifier (optional) to associate with these statistics within |
|
Schema containing |
|
Does not invalidate the dependent cursors if set to |
|
Statistics type. The only value allowed is |
|
Gather statistics of table even if it is locked |
To invoke this procedure you must be owner of the table, or you need the ANALYZE
ANY
privilege. For objects owned by SYS
, you need to be either the owner of the table, or you need the ANALYZE
ANY
DICTIONARY
privilege or the SYSDBA
privilege.
Index statistics collection can be parellelized except for cluster, domain and join indexes.
SET_INDEX_STATS Procedures
These procedures set index-related information. In the version of this procedure that deals with user-defined statistics, the statistics type specified is the type to store in the dictionary, in addition to the actual user-defined statistics. If this statistics type is NULL, the statistics type associated with the index or column is stored.
DBMS_STATS.SET_INDEX_STATS (
ownname VARCHAR2,
indname VARCHAR2,
partname VARCHAR2 DEFAULT NULL,
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
numrows NUMBER DEFAULT NULL,
numlblks NUMBER DEFAULT NULL,
numdist NUMBER DEFAULT NULL,
avglblk NUMBER DEFAULT NULL,
avgdblk NUMBER DEFAULT NULL,
clstfct NUMBER DEFAULT NULL,
indlevel NUMBER DEFAULT NULL,
flags NUMBER DEFAULT NULL,
statown VARCHAR2 DEFAULT NULL,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type(
get_param('NO_INVALIDATE')),
guessq NUMBER DEFAULT NULL,
cachedblk NUMBER DEFAULT NULL,
cachehit NUMBER DEFUALT NULL,
force BOOLEAN DEFAULT FALSE);
Use the following for user-defined statistics:
DBMS_STATS.SET_INDEX_STATS (
ownname VARCHAR2,
indname VARCHAR2,
partname VARCHAR2 DEFAULT NULL,
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
ext_stats RAW,
stattypown VARCHAR2 DEFAULT NULL,
stattypname VARCHAR2 DEFAULT NULL,
statown VARCHAR2 DEFAULT NULL,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type(
get_param('NO_INVALIDATE')),
cachedblk NUMBER DEFAULT NULL,
cachehit NUMBER DEFUALT NULL,
force BOOLEAN DEFAULT FALSE);
Table 142-90 SET_INDEX_STATS Procedure Parameters
Parameter Description
|
Name of the schema |
|
Name of the index |
|
Name of the index partition in which to store the statistics. If the index is partitioned and if |
|
User statistics table identifier describing where to store the statistics. If |
|
Identifier (optional) to associate with these statistics within |
|
User-defined statistics |
|
Schema of the statistics type |
|
Name of the statistics type |
|
Number of rows in the index (partition) |
|
Number of leaf blocks in the index (partition) |
|
Number of distinct keys in the index (partition) |
|
Average integral number of leaf blocks in which each distinct key appears for this index (partition). If not provided, then this value is derived from |
|
Average integral number of data blocks in the table pointed to by a distinct key for this index (partition). If not provided, then this value is derived from |
|
See |
|
Height of the index (partition) |
|
For internal Oracle use (should be left as |
|
Schema containing |
|
Does not invalidate the dependent cursors if set to |
|
Guess quality. See the |
|
The average number of blocks in the buffer cache for the segment (index/table/index partition/table partition) |
|
The average cache hit ratio for the segment (index/table/index partition/table partition) |
|
Sets the values even if statistics of the index are locked |
-
To invoke this procedure you must be owner of the table, or you need the
ANALYZE
ANY
privilege. For objects owned bySYS
, you need to be either the owner of the table, or you need theANALYZE
ANY
DICTIONARY
privilege or theSYSDBA
privilege. -
The Optimizer uses the cached data to estimate number of cached blocks for index or statistics table access. The total cost of the operation will be combined from the I/O cost of reading not cached blocks from disk, the CPU cost of getting cached blocks from the buffer cache, and the CPU cost of processing the data.
-
Oracle maintains
cachedblk
andcachehit
at all times but uses correspondent caching statistics for optimization as part of the table and index statistics only when the user callsDBMS_STATS.GATHER_[TABLE/INDEX/SCHEMA/DATABASE]_STATS
procedure for auto mode orDBMS_STATS.GATHER_SYSTEM_STATS
for manual mode. In order to prevent the user from utilizing inaccurate and unreliable data, the optimizer will compute a 'confidence factor' for eachcachehit
and acachedblk
for each object. If the 'confidence factor' for the value meets confidence criteria, this value will be used, otherwise the defaults will be used. -
The automatic maintenance algorithm for object caching statistics assumes that there is only one major workload for the system and adjusts statistics to this workload, ignoring other "minor" workloads. If this is not the case, you must use manual mode for maintaining object caching statistics.
-
The object caching statistics maintenance algorithm for auto mode prevents you from using statistics in the following situations
-
When not enough data has been analyzed, such as when an object has been recently create
-
When the system does not have one major workload resulting in averages not corresponding to real values.
-
ORA-20000
: Object does not exist or insufficient privileges
ORA-20001
: Invalid input value
ORA-20005
: Object statistics are locked
SET_TABLE_STATS Procedure
This procedure sets table-related information.
DBMS_STATS.SET_TABLE_STATS (
ownname VARCHAR2,
tabname VARCHAR2,
partname VARCHAR2 DEFAULT NULL,
stattab VARCHAR2 DEFAULT NULL,
statid VARCHAR2 DEFAULT NULL,
numrows NUMBER DEFAULT NULL,
numblks NUMBER DEFAULT NULL,
avgrlen NUMBER DEFAULT NULL,
flags NUMBER DEFAULT NULL,
statown VARCHAR2 DEFAULT NULL,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type (
get_param('NO_INVALIDATE')),
cachedblk NUMBER DEFAULT NULL,
cachehit NUMBER DEFUALT NULL,
force BOOLEAN DEFAULT FALSE);
Table 142-95 SET_TABLE_STATS Procedure Parameters
Parameter Description
|
Name of the schema |
|
Name of the table |
|
Name of the table partition in which to store the statistics. If the table is partitioned and |
|
User statistics table identifier describing where to store the statistics. If |
|
Identifier (optional) to associate with these statistics within |
|
Number of rows in the table (partition) |
|
Number of blocks the table (partition) occupies |
|
Average row length for the table (partition) |
|
For internal Oracle use (should be left as |
|
Schema containing |
|
Does not invalidate the dependent cursors if set to |
|
The average number of blocks in the buffer cache for the segment (index/table/index partition/table partition) |
|
The average cache hit ratio for the segment (index/table/index partition/table partition) |
|
Sets the values even if statistics of the table are locked |
-
To invoke this procedure you must be owner of the table, or you need the
ANALYZE
ANY
privilege. For objects owned bySYS
, you need to be either the owner of the table, or you need theANALYZE
ANY
DICTIONARY
privilege or theSYSDBA
privilege. -
The Optimizer uses the cached data to estimate number of cached blocks for index or statistics table access. The total cost of the operation will be combined from the I/O cost of reading not cached blocks from disk, the CPU cost of getting cached blocks from the buffer cache, and the CPU cost of processing the data.
-
Oracle maintains
cachedblk
andcachehit
at all times but uses correspondent caching statistics for optimization as part of the table and index statistics only when the user callsDBMS_STATS.GATHER_[TABLE/INDEX/SCHEMA/DATABASE]_STATS
procedure for auto mode orDBMS_STATS.GATHER_SYSTEM_STATS
for manual mode. In order to prevent the user from utilizing inaccurate and unreliable data, the optimizer will compute a 'confidence factor' for eachcachehit
and acachedblk
for each object. If the 'confidence factor' for the value meets confidence criteria, this value will be used, otherwise the defaults will be used. -
The automatic maintenance algorithm for object caching statistics assumes that there is only one major workload for the system and adjusts statistics to this workload, ignoring other "minor" workloads. If this is not the case, you must use manual mode for maintaining object caching statistics.
-
The object caching statistics maintenance algorithm for auto mode prevents you from using statistics in the following situations
-
When not enough data has been analyzed, such as when an object has been recently create
-
When the system does not have one major workload resulting in averages not corresponding to real values.
-
ORA-20000
: Object does not exist or insufficient privileges
ORA-20001
: Invalid input value
ORA-20005
: Object statistics are locked
LOCK_TABLE_STATS Procedure
This procedure locks the statistics on the table.
-
To invoke this procedure you must be owner of the table, or you need the
ANALYZE
ANY
privilege. For objects owned bySYS
, you need to be either the owner of the table, or you need theANALYZE
ANY
DICTIONARY
privilege or theSYSDBA
privilege. -
When statistics on a table are locked, all the statistics depending on the table, including table statistics, column statistics, histograms and statistics on all dependent indexes, are considered to be locked.
-
The
SET_*,
DELETE_*,
IMPORT_*,
GATHER_*
procedures that modify statistics in the dictionary of an individual table, index or column will raise an error if statistics of the object is locked. -
Procedures that operates on multiple objects (such as
GATHER_SCHEMA_STATS
) will skip modifying the statistics of an object if it is locked. Many procedures have force argument to override the lock. -
This procedure either freezes the current set of the statistics or keeps the statistics empty (uncollected) to use Dynamic Sampling.
-
The locked or unlocked state is not exported along with the table statistics when using
EXPORT_*_STATS
procedures.
UNLOCK_TABLE_STATS Procedure
This procedure unlocks the statistics on the table.
-
To invoke this procedure you must be owner of the table, or you need the
ANALYZE
ANY
privilege. For objects owned bySYS
, you need to be either the owner of the table, or you need theANALYZE
ANY
DICTIONARY
privilege or theSYSDBA
privilege. -
When statistics on a table is locked, all the statistics depending on the table, including table statistics, column statistics, histograms and statistics on all dependent indexes, are considered to be locked.
-
The
SET_*,
DELETE_*,
IMPORT_*,
GATHER_*
procedures that modify statistics in the dictionary of an individual table, index or column will raise an error if statistics of the object is locked. -
Procedures that operates on multiple objects (such as
GATHER_SCHEMA_STATS
) will skip modifying the statistics of an object if it is locked. Many procedures have force argument to override the lock.
How to export and import table statistics between two databases.
Unlock the stats on the customer table
exec dbms_stats.unlock_table_stats(ownname => scott, tabname => 'CUSTOMER');
Oracle will gather stats via the overnight or weekend job window
create table for Exports of customer table stats
execute dbms_stats.create_stat_table(ownname= 'scott', stattab= 'backup_stats_uat');
Import the stats from the customer table and indexes
exec dbms_stats.import_table_stats(ownname=>'scott', tabname=>'customer', statown=>'scott', stattab=>'backup_stats_uat', cascade=>true);
Use datapump to export the table
expdp system tables=scott.backup_stats_uat directory=oraexp dumpfile=backup_stats_uat.dmp logfile=backup_stats_uat.log
Transfer the file to the production system
unlock stats on table scott.customer
exec dbms_stats.unlock_table_stats(ownname => scott, tabname => 'customer')
Import the table backup_stats_uat using the backup_stats_uat.dmp
impdp system tables=scott.backup_stats_uat directory=data_pump_dir dumpfile=backup_stats_uat.dmp logfile=imp_backup_stats_uat.log
Once table is imported /import the table and index stats
exec dbms_stats.export_table_stats(ownname=>'scott', tabname=>'customer', stattab=>'backup_stats_uat', cascade=>true, statown=>'scott');
drop the backup_stats_uat table
execute dbms_stats.drop_stat_table(ownname= 'npp', stattab= 'backup_stats_uat');
If you need to regress the stats
exec dbms_stats.restore_table_stats ('scott','customer',sysdate-1);
– drop statistics table
SQL> execute dbms_stats.drop_stat_table(ownname= 'scott', stattab= 'backup_stats');
Tips:
Once lock index statistics, even rebuild index by rebuild clause ,wouldn't make the statistics invalid.
Truncate table partition wouldn't make golbal index statistics and local index statistics invalid.
参考至:http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_stats.htm#ARPLS68567
http://www.dba-oracle.com/t_export_import_cbo_optimizer_statistics_dbms_stats.htm
http://mdesouza.wordpress.com/2011/08/04/export-import-table-stats-from-uat-system-into-production/
本文原创,转载请注明出处、作者
如有错误,欢迎指正