Gathering Optimizer Statistics in Oracle Database
Gathering Optimizer Statistics in Oracle Database
B. Purpose:
• Collect data about tables, indexes, and columns to estimate their size, cardinality, and
distribution.
• Inform the optimizer about data characteristics, enabling it to select efficient execution
plans.
• Improve query performance by reducing reliance on estimations and increasing plan
accuracy.
C. Prerequisites:
• Access to an Oracle database instance.
• Understanding of statistics types (table, index, column) and their impact on query
optimization.
• Privileges to run statistics gathering commands (e.g., DBA, ANALYZE role).
D. Gathering Methods:
• ANALYZE Command:
o Basic method for gathering table and index statistics.
o Use ANALYZE TABLE tableName [ALL INDEXES| <indexName>] [,
VALIDATE STRUCTURE] [, ESTIMATE STATISTICS ONLY];
o Consider drawbacks like performance impact and limited customization.
• DBMS_STATS Package:
o Flexible and powerful PL/SQL package for advanced statistics gathering.
o Offers various options for targeting specific tables, indexes, columns, and
gathering levels.
o Use DBMS_STATS.GATHER_TABLE_STATS,
DBMS_STATS.GATHER_INDEX_STATS, etc.
o Consult the documentation for detailed parameter options and best practices.
• Automatic Statistics:
o Oracle automatically gathers statistics for new tables and indexes.
o Configure automatic statistics collection with suitable parameters for your
workloads.
o Consider enabling AUTOSTATS or setting
DBMS_STATS.AUTO_SAMPLE_SIZE appropriately.
E. Best Practices:
• Gather statistics regularly, especially after data changes or schema modifications.
• Analyze gathered statistics and identify potential issues like outdated or misleading data.
• Choose the appropriate gathering method based on your needs and performance impact
considerations.
• Test the impact of statistics gathering on your environment before applying changes in
production.
• Monitor statistics usage and update gathering strategies as needed.
-- It gathers statistics for dictionary schemas 'SYS', 'SYSTEM' and other internal schemas.
EXEC DBMS_STATS.gather_dictionary_stats;
EXEC DBMS_STATS.GATHER_FIXED_OBJECTS_STATS;
-- With estimate_percent to 15 percent or any other value , if the db size very huge.
EXEC DBMS_STATS.gather_schema_stats('DBACLSS');
BEGIN
DBMS_STATS.GATHER_TABLE_STATS (
ownname => 'SCOTT',
tabname => 'TEST', --- TABLE NAME
partname => 'TEST_JAN2016' --- PARTITOIN NAME
method_opt=>'for all indexed columns size 1',
GRANULARITY => 'APPROX_GLOBAL AND PARTITION',
degree => 8);
END;
/
-- Gather column-level statistics for the "price" column in the "products" table
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => 'schema',
tabname => 'products',
method_opt => 'FOR COLUMNS size 10 price'
);
END; /
7. Lock/unlock statistics:
EXEC DBMS_STATS.unlock_schema_stats('DBACLASS');
-- unlock stats of a table:
EXEC DBMS_STATS.unlock_table_stats('DBACLASS', 'DBACLASS');
--unlock stats of a partition:
8 . Delete statistics:
exec dbms_stats.delete_fixed_objects_stats;
exec dbms_stats.delete_system_stats('STAT_TAB');
select
dbms_stats.get_prefs(ownname=>'DBACLASS',tabname=>'EMP',pname=>'
PUBLISH') FROM DUAL;
select
DBMS_STATS.get_prefs(ownname=>'DBACLASS',tabname=>'EMP',pname=>'
INCREMENTAL') FROM DUAL;
select
DBMS_STATS.get_prefs(ownname=>'DBACLASS',tabname=>'EMP',pname=>'
GRANULARITY') FROM DUAL;
select
DBMS_STATS.get_prefs(ownname=>'DBACLASS',tabname=>'EMP',pname=>'
STALE_PERCENT') FROM DUAL;
select
DBMS_STATS.get_prefs(ownname=>'DBACLASS',tabname=>'EMP',pname=>'
ESTIMATE_PERCENT') FROM DUAL;
select
DBMS_STATS.get_prefs(ownname=>'DBACLASS',tabname=>'EMP',pname=>'
DEGREE') FROM DUAL;
exec
dbms_stats.set_table_prefs('DBACLASS','EMP','PUBLISH','FALSE');
exec
dbms_stats.set_table_prefs('DBACLASS','EMP','ESTIMATE_PERCENT','
20');
exec dbms_stats.set_table_prefs('DBACLASS','EMP','DEGREE','8');
-- Set schema preferences:
exec dbms_stats.SET_SCHEMA_PREFS('DBATEST','PUBLISH','FALSE');
exec
dbms_stats.SET_SCHEMA_PREFS('DBATEST','ESTIMATE_PERCENT','20');
exec dbms_stats.SET_SCHEMA_PREFS('DBATEST','CASCADE','TRUE');
9 . Deleting preferences :
----- If we are importing stats table from higher version to lower version,
then before importing in the database, we need to upgrade the stats table.
exec DBMS_STATS.ALTER_STATS_HISTORY_RETENTION(60);
F. Conclusion:
By effectively gathering and maintaining accurate optimizer statistics, you can significantly
improve query performance and resource utilization in your Oracle database. Remember to
choose the right tools and approaches, considering your specific requirements and environment.
Additional Notes:
• This SOP provides a basic overview. Refer to the Oracle documentation for detailed
information on individual commands, packages, and parameters.
• Consider including specific examples and best practices relevant to your database version
and usage patterns for a more tailored guide.
• Regularly update the SOP to reflect changes in Oracle releases and statistics gathering
techniques.