Monday, August 17, 2026

Gather stats in Oracle - most commonly used DBMS_STATS.GATHER_TABLE_STATS parameters

Below is a comprehensive explanation of the most commonly used DBMS_STATS.GATHER_TABLE_STATS parameters and when you should use them.

Syntax

DBMS_STATS.GATHER_TABLE_STATS(
ownname => 'SCOTT',
tabname => 'EMP',
partname => NULL,
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
block_sample => FALSE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO',
degree => DBMS_STATS.AUTO_DEGREE,
granularity => 'AUTO',
cascade => TRUE,
no_invalidate => DBMS_STATS.AUTO_INVALIDATE,
options => 'GATHER',
force => FALSE
);


Parameter Explanation Table

ParameterPurposeTypical ValueWhy NeededRecommendation
OWNNAMESchema owner'HR'Identifies schema containing tableMandatory
TABNAMETable name'EMPLOYEES'Table for stats collectionMandatory
PARTNAMESpecific partition'P202501'Gather stats for only one partitionUse only for partitioned tables
ESTIMATE_PERCENTSample size percentageAUTO_SAMPLE_SIZEDetermines how much data Oracle samplesUse AUTO_SAMPLE_SIZE
BLOCK_SAMPLEBlock sampling methodTRUE/FALSESamples blocks instead of rowsUsually FALSE
METHOD_OPTHistogram collection method'FOR ALL COLUMNS SIZE AUTO'Controls histogram creationMost important parameter
DEGREEParallelismAUTO_DEGREESpeeds up large-table stats collectionUse AUTO
GRANULARITYPartition/global stats level'AUTO'Determines partition-level stats collectionUse AUTO
CASCADEGather index stats alsoTRUEUpdates index statisticsAlways TRUE
NO_INVALIDATECursor invalidation controlAUTO_INVALIDATEDetermines when execution plans become obsoleteUse AUTO
OPTIONSCollection modeGATHER AUTO, GATHER STALEControls which objects get analyzedUsually GATHER STALE
FORCEIgnore lock statusTRUE/FALSEGather stats on locked objectsUsually FALSE

Important Parameters Deep Dive

1. ESTIMATE_PERCENT

AUTO_SAMPLE_SIZE

estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE

Why Use It?

Oracle decides the optimal sample size.

Benefits:

  • More accurate cardinality estimates
  • Reduced runtime
  • Recommended by Oracle

Avoid

estimate_percent => 100

unless doing optimizer troubleshooting.


2. METHOD_OPT

This parameter has the most impact on optimizer plans.

Recommended

method_opt => 'FOR ALL COLUMNS SIZE AUTO'

What It Does

Oracle creates histograms only when useful.

Example:

Column:

STATUS
-------
ACTIVE 99%
INACTIVE 1%

Without histogram:

Optimizer assumes even distribution.

With histogram:

Optimizer understands skew.

Result:

  • Better index selection
  • Better join ordering
  • Better cardinality estimates

Other Options

ValuePurpose
FOR ALL COLUMNS SIZE AUTOOracle chooses histograms
FOR ALL COLUMNS SIZE 1No histograms
FOR ALL INDEXED COLUMNS SIZE AUTOHistograms only on indexed columns
FOR COLUMNS SIZE 254 col1,col2Maximum histogram detail

3. CASCADE

Example

cascade => TRUE

What It Does

Gathers statistics on:

  • Table
  • Indexes

Without it:

Table stats updated
Index stats stale

This can lead to poor plans.

Recommended:

cascade => TRUE


4. NO_INVALIDATE

AUTO_INVALIDATE

no_invalidate => DBMS_STATS.AUTO_INVALIDATE

Purpose

When statistics change Oracle may invalidate cached SQL execution plans.

Options:

ValueEffect
TRUEKeep existing plans
FALSEImmediately invalidate plans
AUTO_INVALIDATEOracle decides

Recommended

AUTO_INVALIDATE

for production systems.


5. DEGREE

Controls parallel workers.

Examples

degree => 8

degree => DBMS_STATS.AUTO_DEGREE

Usage

Table SizeRecommendation
< 10 GBAUTO
10-100 GBAUTO
> 100 GB4-16

6. GRANULARITY

Used for partitioned tables.

Example

granularity => 'AUTO'

Options:

ValueDescription
AUTOOracle decides
GLOBALGlobal stats only
PARTITIONPartition stats only
ALLGlobal + Partition
SUBPARTITIONSubpartition stats

Recommendation

AUTO


7. OPTIONS

Very important for maintenance windows.

GATHER

options => 'GATHER'

Gather regardless of staleness.


GATHER STALE

options => 'GATHER STALE'

Only tables with stale statistics.

Recommended for production.


GATHER AUTO

options => 'GATHER AUTO'

Gathers:

  • Missing stats
  • Stale stats
  • Oracle-selected objects

Recommended when running manually.


LIST STALE

options => 'LIST STALE'

Report only.

No gathering performed.


8. FORCE

Example

force => TRUE

Normally locked statistics are skipped.

force => TRUE

forces collection anyway.

Mostly used by DBAs during troubleshooting.


Best Production Script

For Oracle 12c/19c/21c:

BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => 'SCHEMA_NAME',
tabname => 'TABLE_NAME',
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO',
cascade => TRUE,
degree => DBMS_STATS.AUTO_DEGREE,
granularity => 'AUTO',
no_invalidate => DBMS_STATS.AUTO_INVALIDATE
);
END;
/

Best Production Schema Script

BEGIN
DBMS_STATS.GATHER_SCHEMA_STATS(
ownname => 'SCHEMA_NAME',
options => 'GATHER STALE',
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO',
cascade => TRUE,
degree => DBMS_STATS.AUTO_DEGREE
);
END;
/

DBA Recommendation

For Oracle 19c/21c production systems, the combination below provides the best balance between optimizer accuracy, maintenance window duration, and plan stability:

ParameterRecommended Value
ESTIMATE_PERCENTAUTO_SAMPLE_SIZE
METHOD_OPTFOR ALL COLUMNS SIZE AUTO
CASCADETRUE
DEGREEAUTO_DEGREE
GRANULARITYAUTO
NO_INVALIDATEAUTO_INVALIDATE
OPTIONSGATHER STALE (schema) / default GATHER (table)
FORCEFALSE


=> Dynamic SQL query that generates DBMS_STATS.GATHER_TABLE_STATS statements for all tables in a schema, use one of the following.

Generate Gather Stats Commands for All Tables in a Schema

SELECT 'EXEC DBMS_STATS.GATHER_TABLE_STATS('||
'ownname=>'''||owner||''','||
'tabname=>'''||table_name||''','||
'estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE,'||
'method_opt=>''FOR ALL COLUMNS SIZE AUTO'','||
'cascade=>TRUE,'||
'degree=>DBMS_STATS.AUTO_DEGREE);'
FROM dba_tables
WHERE owner = UPPER('&SCHEMA_NAME')
ORDER BY table_name;

Generate Gather Stats Only for Stale Tables

SELECT 'EXEC DBMS_STATS.GATHER_TABLE_STATS('||
'ownname=>'''||owner||''','||
'tabname=>'''||table_name||''','||
'estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE,'||
'method_opt=>''FOR ALL COLUMNS SIZE AUTO'','||
'cascade=>TRUE,'||
'degree=>DBMS_STATS.AUTO_DEGREE);'
FROM dba_tab_statistics
WHERE owner = UPPER('&SCHEMA_NAME')
AND stale_stats = 'YES'
ORDER BY table_name;

Generate Gather Stats for Tables with Missing Statistics

SELECT 'EXEC DBMS_STATS.GATHER_TABLE_STATS('||
'ownname=>'''||owner||''','||
'tabname=>'''||table_name||''','||
'estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE,'||
'method_opt=>''FOR ALL COLUMNS SIZE AUTO'','||
'cascade=>TRUE);'
FROM dba_tables
WHERE owner = UPPER('&SCHEMA_NAME')
AND last_analyzed IS NULL;

Execute Automatically Using Dynamic PL/SQL

BEGIN
FOR r IN (
SELECT owner, table_name
FROM dba_tab_statistics
WHERE owner = 'SCHEMA_NAME'
AND stale_stats = 'YES'
)
LOOP
DBMS_STATS.GATHER_TABLE_STATS(
ownname => r.owner,
tabname => r.table_name,
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO',
cascade => TRUE,
degree => DBMS_STATS.AUTO_DEGREE
);
END LOOP;
END;
/

Parallel Execution Script Generator (Useful for Large Schemas)

SELECT 'EXEC DBMS_STATS.GATHER_TABLE_STATS('||
'ownname=>'''||owner||''','||
'tabname=>'''||table_name||''','||
'estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE,'||
'method_opt=>''FOR ALL COLUMNS SIZE AUTO'','||
'cascade=>TRUE,'||
'degree=>8);'
FROM dba_tables
WHERE owner='SCHEMA_NAME';

For Oracle 19c/21c production environments, generally recommend gathering only stale statistics using AUTO_SAMPLE_SIZE, FOR ALL COLUMNS SIZE AUTO, and CASCADE=>TRUE rather than forcing stats collection on every table. This reduces maintenance time while keeping optimizer plans accurate.

No comments:

Post a Comment

Gather stats in Oracle - most commonly used DBMS_STATS.GATHER_TABLE_STATS parameters

Below is a comprehensive explanation of the most commonly used DBMS_STATS.GATHER_TABLE_STATS parameters and when you should use them. Synta...