15 thoughts on “All About Statistics In Oracle”

  1. This task became an automated task starting from 10g, To check the status of that task:
    SQL> select * from dba_autotask_client where client_name = "auto optimizer stats collection" ;

    does this mean that we no need to gather stats from 10g onwards

  2. As per Oracle documentation, this task gathers statistics on all objects in the database which have stale or missing statistics.

    Also you can:

    Enable automatic optimizer statistics:
    SQL> BEGIN
    DBMS_AUTO_TASK_ADMIN.ENABLE(
    client_name => 'auto optimizer stats collection',
    operation => NULL,
    window_name => NULL);
    END;
    /

    Disable automatic optimizer statistics:
    SQL> BEGIN
    DBMS_AUTO_TASK_ADMIN.DISABLE(
    client_name => 'auto optimizer stats collection',
    operation => NULL,
    window_name => NULL);
    END;
    /

    To check the tables that have stale statistics:
    SQL> select OWNER,TABLE_NAME,LAST_ANALYZED,STALE_STATS from DBA_TAB_STATISTICS where STALE_STATS='YES';

    I've updated the post with these information, I recommend you to check this reference:
    http://docs.oracle.com/cd/B28359_01/server.111/b28274/stats.htm#i41282

    And thanks for highlighting this un clear information in the post.

  3. DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO needs to be run before checking the stale_statistics column. Otherwise wrong info is displayed

  4. Thanks a lot for highlighting that issue, your comment is very true, I would like to support it with this excerpt from MOS (Doc ID 1476052.1)

    "When querying *_TAB_MODIFICATIONS view you should ensure that you run DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO before doing so in order to obtain accurate results."

    The same saying applies on DBA_TAB_STATISTICS as both *_TAB_STATISTICS & *_TAB_MODIFICATIONS views are showing their data from mon_mods_all$ table.
    Oracle automatically updates table mon_mods_all$ from SGA recent data every:

    – For Oracle versions older than 10g mon_mods_all$ table is getting refreshed every 3hours (approx).
    – For Oracle version 10gR2 [only] mon_mods_all$ table is getting refreshed every 15 minutes (+1min run time).
    – For Oracle versions 10g onwards [except for 10gR2] mon_mods_all$ table is getting refreshed once a day.

    This table also get updated when you manually execute GATHER_*_STATS procedure which internally calls DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO procedure.

    In case you don't want to wait for the internal job to run, you can manually run DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO procedure to get the table mon_mods_all$ up to date from SGA data.

    Conclusion:
    What you have said is very true. In general, in order to get accurate information from (*_TAB_MODIFICATIONS, *_TAB_STATISTICS and *_IND_STATISTICS) views you have to manually run DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO procedure.

    References:
    – I've opened an SR with Oracle Support to get the above information, unfortunately Oracle's official documentations doesn't enough information about DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO procedure.
    – MOS Doc ID 1476052.1

    I've updated the post with those information, and thank you again for highlighting that issue.

  5. Thanx for the overview. Maybe you can add info about locked statistics to get the info complete.

    Regards,

    Laurens

  6. when i run your script against one schema I cannot see any date update on the last_analyzed column it's same before and after:
    select last_analyzed from dba_tables where owner='WOAPIFZ';

    LAST_ANALYZED
    ——————–
    18-Nov-2019 22:00:39
    09-Apr-2018 22:00:08
    09-Apr-2018 22:00:08
    18-Nov-2019 22:02:40
    17-Nov-2015 07:36:48

  7. Most probably the statistics are locked on this schema, this is why it will not allow new statistics to be gathered.

    This query will return all the tables that have their statistics locked in your database:
    select OWNER, TABLE_NAME, LAST_ANALYZED, STATTYPE_LOCKED LOCKED from DBA_TAB_STATISTICS
    where STATTYPE_LOCKED is not null
    and OWNER not in ('SYS','SYSTEM','SYSMAN','SQLTXPLAIN','WMSYS')
    order by OWNER, TABLE_NAME;

    To UNLOCK the statistics of a particular schema:
    exec dbms_stats.unlock_schema_stats(<'SCHEMA_NAME>');

Leave a Reply

Your email address will not be published. Required fields are marked *