Gathering Oracle CBO Statistics
This examines collection of statistics for the
cost-based optimizer (CBO). A new DBA task in Oracle Database 10g is to
generate statistics on data dictionary objects which are contained in the SYS
schema. The stored procedures dbms_stats.gather_database_stats and
dbms_stats.gather_schema_stats can be used to gather the SYS schema stats. Here
is an example of using dbms_stats.gather_schema_stats to gather data dictionary
Itís a good idea to gather data dictionary statistics on
a regular basis, say once a week on a database with average activity.
Gathering Fixed Table CBO Statistics
Many of the Oracle data dictionary objects are what are
called fixed tables because the DBA generally canít modify them. (in fact, some
of the data dictionary objects can be modified, but you should never ever do
The procedures dbms_stats.gather_database_stats and
dbms_stats.gather_schema_stats offer the gather_fixed parameter. When set to
TRUE the gather_fixed parameter will cause fixed table statistics to be gathered
along with regular database statistics. Here is an example of the collection of
data dictionary stats and also the collection of fixed table statistics:
EXEC dbms_stats.gather_schema_stats(íSYSí, gather_fixed=>TRUE);
Scheduling CBO statistics collection
Determining when to collect statistics is not as easy as
it seems. If you create a table, and then generate statistics on it, are those
statistics really good? If the table is going to be populated with lots of data
at a later time, then the statistics created when the table was first created
will probably be no good. A table with a million rows will often generate a very
different plan than a table with just 10.
If tables are static, then you will probably need to
analyze them only when they change. If tables are dynamic, then a reoccurring
analyze operation is probably the best thing to do. Analyzing your dynamic
tables once a day is far from unusual.
Sometimes you might actually have to create your own
user defined statistics in order to get good performance from new tables. This
is fairly rare, and is an advanced DBA topic, but we wanted you to know that it
was possible to create your own statistics.
Automatic Collection of CBO Statistics
Oracle Database 10g actually automates the collection of
database statistics out of the box. When an Oracle database is created, a job
will be scheduled that will generate the database statistics for you. You will
still need to collect system statistics however, as these are not collected by
the automatic statistics gathering mechanism.
You can disable this automated statistics collection job
using the dbms_scheduler.disable procedure as seen here:
You can re-enable the job using the
dbms_scheduler.enable procedure as seen here:
This is an excerpt from the bestselling "Easy
Oracle Jumpstart" by Robert Freeman and Steve Karam (Oracle ACE and Oracle
Certified Master). Itís only $19.95 when you buy it directly from the
If you like Oracle tuning, you may enjoy the new book "Oracle
Tuning: The Definitive Reference", over 900 pages of BC's favorite tuning
tips & scripts.
You can buy it direct from the publisher for 30%-off and get instant access to
the code depot of Oracle tuning scripts.