Gather Database Stats , Schema Stats & Dictionary and Fixed Object Statistics via DBMS_STATS in Oracle

I will explain how to Gather Database Stats , Schema Stats & Dictionary and Fixed Object Statistics via DBMS_STATS in Oracle Database in this article. Let’s go to review how to use DBMS_STATS Gather Database Schema Stats.

 

DBMS_STATS

DBMS_STATS package was introduced in Oracle 8i and used to gather Database,table,Schema,dictionary and fixed object statistic in Oracle database.

Statistic of objects should be up to date in Oracle database for Oracle optimizer. Because Oracle optimizer uses database statistics to generate lots of execution plans in same time and If statistics are up to date ,then Optimizer decide correct execution plans.

 

 

DBMS_STATS Gather Database Schema Stats

You should gather database stats periodically like one time in every week. Or if you upgrade database, you must gather database stats immediately.

 

To gather database stats, use following script.

EXEC DBMS_STATS.gather_database_stats;

 

 

 

Gather Schema Stats

If you have done lots of change on any schema, you need to gather schema stats.

 

To gather Schema stats, use following script.

EXEC DBMS_STATS.gather_schema_stats('MEHMET', estimate_percent => 25, cascade => TRUE);

 

 

Gather Table Stats

To gather table stats, use following script.

EXEC DBMS_STATS.gather_table_stats('MEHMET', 'DEVECI', estimate_percent => 25, cascade => TRUE);

 

Gather Dictionary Stats

To gather dictionary stats, use following script.

EXEC DBMS_STATS.gather_dictionary_stats;

 

 

 

Parallel Gather Database Stats

You can gather database stats with 8 Parallel to perform it very fast.

BEGIN
DBMS_STATS.gather_database_stats(estimate_percent => dbms_stats.auto_sample_size, degree=>8 );
END;
/

 

 

 

Gather All Database Stats

You can gather database, dictionary and fixed objects stats in the same script with 16 Parallel like following. Thus, all objects stats are gathered in the database and database stats will be up to date completely.

BEGIN
DBMS_STATS.GATHER_FIXED_OBJECTS_STATS;
DBMS_STATS.gather_dictionary_stats;
DBMS_STATS.gather_database_stats(estimate_percent => dbms_stats.auto_sample_size, degree=>16 );
END;
/




Periodic Gather Stats Job

You can create a dba job for this long running task as follows. You can create for Schema stats job which will run in 8 parallel each Sunday at 11:00 AM automatically.

SET SERVEROUTPUT ON
DECLARE
l_job NUMBER;
BEGIN
SELECT MAX (job) + 1 INTO l_job FROM dba_jobs;
DBMS_JOB.submit(l_job,
'BEGIN DBMS_STATS.gather_schema_stats(''MSD'',estimate_percent => dbms_stats.auto_sample_size, degree=>8 ); END;',
trunc(next_day(SYSDATE,'SUNDAY'))+11/24,
'TRUNC (SYSDATE+7)+11/24');
COMMIT;
DBMS_OUTPUT.put_line('Job: ' || l_job);
END;
/

 

 

 

You can create a dba job for this long running task as follows. You can create for database stats job which will run in 16 parallel each Sunday at 11:00 AM automatically.

SET SERVEROUTPUT ON
DECLARE
l_job NUMBER;
BEGIN
SELECT MAX (job) + 1 INTO l_job FROM dba_jobs;
DBMS_JOB.submit(l_job,
'BEGIN DBMS_STATS.gather_database_stats(estimate_percent => dbms_stats.auto_sample_size, degree=>16 ); END;',
trunc(next_day(SYSDATE,'SUNDAY'))+6/24,
'TRUNC (SYSDATE+7)+6/24');
COMMIT;
DBMS_OUTPUT.put_line('Job: ' || l_job);
END;
/

 

Do you want to learn Oracle Database for Beginners, then Click and read the following articles.

Oracle Database Tutorials for Beginners ( Junior Oracle DBA )

 2,582 views last month,  1 views today

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

One comment

  1. I’m not on board with this post. Your rationalizations are fautless, but sadly it’s unreliable to rely on whatever others may believe. Please expand this, because I think you are an informative writer and I want to see more from you!

Leave a Reply

IT Tutorial