naabi0
6th October 2001, 00:24
In Oracle, will dbmsstat compute the table statistics or do you have to analyze each table first?

patvdv
6th October 2001, 02:26
There are three ways to collect statistics on schema objects:
1. ANALYZE command
2. DBMS_UTILITY
3. DBMS_STATS (Oracle 8.1.5 and higher)

ANALYZE Command

The ANALYZE command gathers statistics in a serial manner. It can be used with an ESTIMATE or COMPUTE statistics. If you specify ANALYZE TABLE ESTIMATE STATISTICS without a sample size, Oracle will only gather statistics based on 1064 rows. This is important to note because depending on how large the table is, you may get inadequate statistics for the object being analyzed. (i.e. analyze table test estimate statistics;)
When using the ANALYZE command, you should always specify a sample size so that the default does not take place. (i.e. analyze table test estimate statistics sample size 20 percent;)

The COMPUTE statistics option will guarantee that the optimizer has the best statistics available in order to determine an execution path for a given query. (i.e. analyze table test compute statistics;)

By default the ANALYZE TABLE command will generate statistics for the table and all it's indexes provided that the FOR clause is not used.


DBMS_UTILITY Package

The DBMS_UTILITY package provides 2 procedures to assist in the gathering of statistics:
1. ANALYZE_SCHEMA -- Analyzes all the tables, clusters and indexes in a schema.
2. ANALYZE_DATABASE -- Analyzes all the tables, clusters and indexes in a database.

The ANALYZE_SCHEMA procedure accepts 5 arguments:
schema: the schema to be analyzed
method: ESTIMATE, COMPUTE or DELETE. If ESTIMATE, then either estimate_rows or estimate_percent must be non-zero.
estimate_rows: Number of rows to estimate.
estimate_percent: Percentage of rows to estimate. If estimate_rows is specified, then this parameter is ignored.
method_opt:
[ FOR TABLE ] [ FOR ALL [INDEXED] COLUMNS] [SIZE n]
[ FOR ALL INDEXES ]
The ANALYZE_DATABASE procedure accepts 4 arguments:
method: ESTIMATE, COMPUTE or DELETE. If ESTIMATE, then either estimate_rows or estimate_percent must be non-zero
estimate_rows: Number of rows to estimate.
estimate_percent: Percentage of rows to estimate. If estimate_rows is specified, then this parameter is ignored.
method_opt:
[ FOR TABLE ] [ FOR ALL [INDEXED] COLUMNS] [SIZE n]
[ FOR ALL INDEXES ]

DBMS_STATS package (8.1.5 and higher)

The DBMS_STATS package can gather statistics on indexes, tables, columns, and partitions, as well as statistics on all schema objects in a schema or database.
It does not gather cluster statistics, but you can use DBMS_STATS to gather statistics on the individual tables instead of the whole cluster.
The statistics-gathering operations can run either serially or in parallel. Whenever possible, DBMS_STATS calls a parallel query to gather statistics with the specified degree of parallelism; otherwise, it calls a serial query or the ANALYZE statement. Index statistics are not gathered in parallel.
For partitioned tables and indexes, DBMS_STATS can gather separate statistics for each partition as well as global statistics for the entire table or index.
Similarly, for composite partitioning DBMS_STATS can gather separate statistics for subpartitions, partitions, and the entire table or index. Depending on the SQL statement being optimized, the optimizer may choose to use either the partition (or subpartition) statistics or the global statistics.