Skip to Main Content
  • Questions
  • Function to gather the stats on the latest partition of a table

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Sai.

Asked: November 09, 2018 - 1:34 pm UTC

Last updated: November 09, 2018 - 3:46 pm UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

I have a function - ANALYZE_TABLE as below

create or replace FUNCTION ANALYZE_TABLE (P_TAB_NAME IN VARCHAR2) RETURN NUMBER IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
   DBMS_STATS.GATHER_TABLE_STATS(USER,P_TAB_NAME);
   RETURN 0;
END;


Now , how can i use the same function to analyze the only latest partition of a given table name? I have tried some thing like below

create or replace FUNCTION ANALYZE_PART_TABLE (P_TAB_NAME IN VARCHAR2) RETURN varchar2 IS
part_name varchar2(20);
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
   SELECT
    MAX(partition_name) KEEP(DENSE_RANK LAST ORDER BY partition_position)
FROM
    dba_tab_partitions
WHERE
    table_name = P_TAB_NAME;
DBMS_STATS.GATHER_TABLE_STATS(USER,P_TAB_NAME,P_PART_NAME);
   RETURN part_name;
END;


But this is throwing error as Table or view doesn't. All i need is to gather the stats on the latest partition of a given table name. help much appreciated.

Thanks
Sai

and Chris said...

Any particular reason you need to do this instead of using the auto-stats job?

Anyway, you need to grant select on dba_tab_partitions to the user that owns this procedure.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Sai Vatshavai, November 09, 2018 - 3:30 pm UTC

Not sure of Auto stats job is being used in this application. More over i believe auto stats job will do the stats on table level , not on the particular partition or on any dynamically created partition.(please correct me if i am wrong)

even the option which you have suggested, i need to contact DBA to grant the select permissions. Was there any other alternate approach.
Chris Saxon
November 09, 2018 - 3:46 pm UTC

No, you need to have select privs on the view.

The auto job handles partitions too.


More to Explore

Performance

Get all the information about database performance in the Database Performance guide.