Optimizer statistics are used for:
Cost Based Optimizer (CBO)
Determines plan of attack for statements based on statistics.Data stored in dictionary views are referred to as “optimizer statistics” because they are used by the CBO. The DBMS_STATS code package allows gathering and displaying this data.
When does optimizer consider data stale?
After 10 percent of the data has changed since the last statistics were gathered on the table.
Using DBMS_STATS
.GATHER_TABLE_STATS - tab, col and index stats
.GATHER_INDEX_STATS - just index stats
.GATHER_SCHEMA_STATS - all obj’s in schema
.GATHER_DATABASE_STATS - all objs in all schemas
.GATHER_DICTIONARY_STATS - on sys-owned dict obj’s
.GATHER_FIXED_OBJECTS_STATS - on dyn perf tables
.GATHER_SYSTEM_STATS - runtime sys stats with wkld.
Constants are:
AUTO_CASCADE - Automatically choose whether to gather index stats with table stats
AUTO_DEGREE - Auto-choose best parallelism degree
AUTO_INVALIDATE - Auto-choose whether to invalidate dependent cursors during stats collection
AUTO_SAMPLE_SIZE - Auto-choose best sample size for table
How to test stats before publishing
Segment Advisor
Identifies table and index segments that have space available for reclamation, and recommends how to reclaim it.
SQL Tuning Advisor
Analyzes code packages by running fragments of the code on a sample of data. It looks for changes that make significant performance gains and bundles them together as a “SQL Profile” and recommends its acceptance. The same profile can then later be used for the same package and is transparent to the user since no changes to the original code are required on the coder’s part.
AWR
Statistics gathered and stored in the dynamic performance views (V$ views) are not persistent and are lost when updated or when the DB shuts down. Automatic Workload Repository stores statistics from perf views across shutdowns.
STATISTICS_LEVEL init param
Sets level of statistics gathering.
Active Session History (ASH)
Data sampled at intervals from all active sessions.
AWR Baselines
Good idea to create a baseline while performance is nominal, which saves that data from purging. Can be used in event of changes/failure to compare performance.