Thursday, February 27, 2025

DBMS_STATS.GATHER_STATS for tables schemas indexes columns

 

ActionFull CommandDescription
Update statistics for a specific tableEXEC DBMS_STATS.GATHER_TABLE_STATS('HR', 'EMPLOYEES');Updates statistics for the EMPLOYEES table in the HR schema.
Update statistics for a table and its indexesEXEC DBMS_STATS.GATHER_TABLE_STATS('HR', 'EMPLOYEES', cascade => TRUE);Updates statistics for the EMPLOYEES table and all its indexes.
Update statistics for a specific indexEXEC DBMS_STATS.GATHER_INDEX_STATS('HR', 'EMP_SALARY_IDX');Updates statistics for the EMP_SALARY_IDX index in the HR schema.
Update statistics for all tables in a schemaEXEC DBMS_STATS.GATHER_SCHEMA_STATS('HR');Updates statistics for all tables and indexes in the HR schema.
Update statistics for the entire databaseEXEC DBMS_STATS.GATHER_DATABASE_STATS;Updates statistics for all schemas and tables in the database. (Time-consuming on large DBs)
Check last statistics update for a tableSELECT table_name, last_analyzed FROM user_tables WHERE table_name = 'EMPLOYEES';Shows the last time statistics were gathered for the EMPLOYEES table.
Check last statistics update for an indexSELECT index_name, last_analyzed FROM user_indexes WHERE table_name = 'EMPLOYEES';Shows the last time statistics were gathered for indexes of EMPLOYEES.
Update statistics in parallel for faster executionEXEC DBMS_STATS.GATHER_TABLE_STATS('HR', 'EMPLOYEES', degree => 4);Runs statistics gathering in parallel for faster performance (using 4 CPU threads).
Update statistics for specific columnsEXEC DBMS_STATS.GATHER_TABLE_STATS('HR', 'EMPLOYEES', columns => ('SALARY'));Updates statistics only for the SALARY column.

Best Practices:

Use cascade => TRUE to update both table and indexes.
Use degree => 4 for faster execution on large tables.
Schedule statistics updates during off-peak hours.

Would you like a PL/SQL script to automate this for multiple tables? 😊

No comments:

Post a Comment

Enable OpenSSH on Windows 11

Step 1: Install OpenSSH Server You can do this via PowerShell (run as Administrator ): Check if it's already available: Get-WindowsCapab...