Search notes:

Oracle Optimizer Statistics

When the optimizer creates an execution plan for an SQL statement, the optimizer statistics provide the data that allows the optimizer to weigh different plan alternatives and to choose the best plan.
Thus, if these statistics are missing or are out-of-date (stale), the optimizer might generate an unsatisfactory plan.
Optimizer statistics must not be confused with performance statistics which are visible in dynamic performance views (aka v$ views).

Statistics

The optimizer relies on the following 4 types of statistics when determining an execution plan for an SQL statement.
Data dictionary view
Table statistics blocks (and how many empty ones), rows and avarage length (bytes) of rows dba_tab_statistics etc.
Column statistics Number of distinct values («NDV»), number of nulls, distribution of data («histogram») and extended statistics dba_tab_col_statistics, dba_tab_histograms
Index statistics Number of leaf blocks and index levels; clustering factor dba_ind_statistics etc.
System statistics I/O and CPU performance and utilization Time it takes to service a single data block and/or a multi-block read request.

Column statistics

There are four histogram types with which column statistics can be stroed:
  • Frequency
  • Top-frequency
  • Height balanced
  • Hybrid
The histogram type of a column statistic can be queried from the column histogram in dba_tab_cols (and related data dictionary views).

Gathering statistics

Preferred method: use supplied automatic statistics-gathering job.

Manual gathering

dbms_stats.gather_*_stats

Statistics in the data dictionary

The gathered statistical values are found in the data dictionary in
The column stale_stats in all_tab_statistics allows to find tables that have no or stale optimizer statistics.
Tables and clusters that store statistics data include:
c_obj#_intcol#
histgrm$
hist_head$
tab_stats$
ind_stats$
aux_stats$ an auxiliary table used to store parameter values
cache_stats_1$ Cache statistics
cache_stats_0$
association$ User defined stats associations
ustats$ User defined stats
stats_target$ Target list for automated stats collection
It is also possible to store statistics in a «user-schema». Such statistics have no influence on the optimizer.

TODO

Histograms

Without histograms, the optimizer assumes the distribution of values in a column to be uniform.
There are several types of histograms:
  • Frequency
  • Height balanced
  • Top-frequencey (12c +)
  • Hybrid (12c +)
  • ?
drop table tq84_histogram_test;

create table tq84_histogram_test (
    nm number,
    tx varchar2(100)
);

insert into tq84_histogram_test  values (  5, 'A');
insert into tq84_histogram_test  values (  5, 'B');
insert into tq84_histogram_test  values (  5, 'C');
insert into tq84_histogram_test  values (100, 'D');
insert into tq84_histogram_test  values (100, 'E');
insert into tq84_histogram_test  values (100, 'F');
insert into tq84_histogram_test  values (104, 'G');
insert into tq84_histogram_test  values (104, 'H');
insert into tq84_histogram_test  values (104, 'I');
Query histogram:
select
   own.name owner,
   obj.name obj_name,
   col.name col_name,
   -----------------
   hgr.bucket,
   hgr.endpoint,
   hgr.ep_repeat_count,
   hgr.epvalue_raw, hgr.epvalue,
   hgr.savtime,
   dense_rank() over (partition by own.name, obj.name, col.name order by hgr.savtime desc) savtime_rank
from
   sys.obj$       obj                                    join
   sys.user$      own  on obj.owner# = own.user#         join
   sys.col$       col  on obj.obj#   = col.obj#     left join
  "_OPTSTAT_HISTGRM_HISTORY_DEC" hgr on col.obj# = hgr.obj# and
                                        col.col# = hgr.intcol#
where
   own.name = 'RENE'                and
   obj.name = 'TQ84_HISTOGRAM_TEST' and
   col.name = 'NM';

Automatic gathering of statistics

By default, Oracle automatically gathers statistics for database objects where statistics are missing or stale during a predefined maintance window (10 PM through 2 AM on weekdays and 6 AM through 2 AM on weekends).
The maintainance window can be changed using
Disable automatic statistics:
begin
   dbms_auto_task_admin.disable(
      client_name => 'auto optimizer stats collection',
      operation   =>  null,
      window_name =>  null
   );
end;
/
Query some characteristics about the automatic gathering:
select * from dba_autotask_task                where client_name       = 'auto optimizer stats collection';
select * from dba_autotask_client              where client_name       = 'auto optimizer stats collection';
select * from dba_scheduler_programs           where program_name      = 'GATHER_STATS_PROG';

select * from dba_scheduler_window_groups      where window_group_name = 'MAINTENANCE_WINDOW_GROUP';
select * from dba_scheduler_wingroup_members   where window_group_name = 'MAINTENANCE_WINDOW_GROUP';

select * from dba_scheduler_windows            where resource_plan     = 'DEFAULT_MAINTENANCE_PLAN';

See also

The column user_stats in dba_tab_cols and dba_tab_columns indicates if statistics were directly provided by a user.
Dynamic statistics
Enabling and disabling automatic optimizer statistics collection with dbms_auto_task_admin.
Optimizer statistics advisor
Gathering statistics for «local» and «ordinary» indexes.
The OPTIMIZER STATISTICS GATHERING plan operation.
$ORACLE_HOME/rdbms/admin/catmwin.sql defines maintenance window and stats collection job.
Online statistics gathering

Index