Navigation Bar

Wednesday, September 18, 2024

Script to gather statistics for all tables in a schema and schema index rebuild

Script to gather schema table statistics 

Get the last time the tabbles were analyzed in HR schema as below.
Do not put the table list if you want to check for all tables. 
SELECT OWNER, TABLE_NAME, LAST_ANALYZED FROM ALL_TABLES WHERE OWNER = 'HR' 
and TABLE_NAME IN ('COUNTRIES','REGIONS','LOCATIONS','DEPARTMENTS','JOBS','EMPLOYEES');
OWNERTABLE_NAMELAST_ANALYZED
HRDEPARTMENTS13-SEP-2024 11:30:01
HREMPLOYEES13-SEP-2024 11:30:01
HRJOBS13-SEP-2024 11:30:01
HRLOCATIONS13-SEP-2024 11:30:01
HRREGIONS13-SEP-2024 11:30:01
HRCOUNTRIES13-SEP-2024 11:30:01

The data shows the last timestamp the statistics were gathered on these tables was on 13-Sep-24 at 11:30 AM. We now run a block to gather statistics for all tables in HR schema as below
DECLARE
  CURSOR C1
  IS 
  SELECT TABLE_NAME
  FROM ALL_TABLES
  WHERE OWNER = 'HR';
  
  v_sql VARCHAR2(1000);
BEGIN
  FOR rec IN C1
  LOOP --{
        BEGIN
--    dbms_output.put_line('table_name  ' || rec.TABLE_NAME);
        v_sql := 'ANALYZE TABLE '|| rec.table_name || ' COMPUTE STATISTICS';
        EXECUTE IMMEDIATE v_sql;
     EXCEPTION
      WHEN OTHERS THEN
        dbms_output.put_line('Gather statistics error for table ' || rec.table_name || ' ORA ERROR ' || SQLERRM);
      END;
  END LOOP;  
END;
After running the script the last_analyzed column shows a time of 18-Sep-24.
SELECT OWNER, TABLE_NAME, LAST_ANALYZED FROM ALL_TABLES WHERE OWNER = 'HR' 
and TABLE_NAME IN ('COUNTRIES','REGIONS','LOCATIONS','DEPARTMENTS','JOBS','EMPLOYEES');
OWNERTABLE_NAMELAST_ANALYZED
HRDEPARTMENTS18-SEP-2024 11:13:00
HREMPLOYEES18-SEP-2024 11:13:00
HRJOBS18-SEP-2024 11:13:00
HRLOCATIONS18-SEP-2024 11:13:00
HRREGIONS18-SEP-2024 11:13:00
HRCOUNTRIES18-SEP-2024 11:13:00

Script to rebuild all indexes in a schema

Get the last time the indexes were rebuilt in HR schema as below.
Do not put the table list if you want to check for all tables.
SELECT TABLE_NAME, TABLE_OWNER,INDEX_NAME,STATUS, LAST_ANALYZED, VISIBILITY, CONSTRAINT_INDEX 
from user_indexes where table_name in ('EMPLOYEES','DEPARTMENTS');
TABLE_NAMETABLE_OWNERINDEX_NAMESTATUSLAST_ANALYZEDVISIBILITYCONSTRAINT_INDEX
DEPARTMENTSHRDEPT_ID_PKVALID18-SEP-2024 11:13:00VISIBLENO
EMPLOYEESHREMP_EMAIL_UKVALID18-SEP-2024 11:13:00VISIBLEYES
EMPLOYEESHREMP_EMP_ID_PKVALID18-SEP-2024 11:13:00VISIBLENO
DECLARE
  CURSOR C1
  IS 
  SELECT INDEX_NAME FROM ALL_INDEXES WHERE OWNER = 'HR';
  
  v_sql VARCHAR2(1000);
BEGIN
  FOR rec IN C1
  LOOP --{
    BEGIN
--    dbms_output.put_line('index_name  ' || rec.index_name);
     v_sql := 'ALTER INDEX  '|| rec.index_name || ' REBUILD';
      EXECUTE IMMEDIATE v_sql;
    EXCEPTION
      WHEN OTHERS THEN
        dbms_output.put_line('Rebuild error for index ' || rec.index_name || ' ORA ERROR ' || SQLERRM);
      END;  
  END LOOP;  
END;
After running the script the last rebuild time of shows a time of 18-Sep-24 11:30:15.
SELECT TABLE_NAME, TABLE_OWNER,INDEX_NAME,STATUS, LAST_ANALYZED, VISIBILITY, CONSTRAINT_INDEX 
from user_indexes where table_name in ('EMPLOYEES','DEPARTMENTS');
TABLE_NAMETABLE_OWNERINDEX_NAMESTATUSLAST_ANALYZEDVISIBILITYCONSTRAINT_INDEX
DEPARTMENTSHRDEPT_ID_PKVALID18-SEP-2024 11:30:15VISIBLENO
EMPLOYEESHREMP_EMAIL_UKVALID18-SEP-2024 11:30:15VISIBLEYES
EMPLOYEESHREMP_EMP_ID_PKVALID18-SEP-2024 11:30:15VISIBLENO

Thought for the day
Commit to the LORD whatever you do, and he will establish your plans.
Proverbs 16:3

No comments:

Post a Comment