Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • move_arch_files.ksh Linux/Unix
  • Complete Git Tutorial for Beginners GIT
  • Default User Profile Oracle
  • column level grant syntax Oracle
  • tracksqltime.sql Oracle
  • segment_wise_space.sql segspace.sql Segment wise space usage (allocated and used) Oracle
  • Specify the Rollback segment to use in Transaction Oracle
  • login.sql Oracle
  • currwait.sql Oracle
  • Gather Stats manually using DBMS_STATS after disabling DBMS_SCHEDULER jobs as previous entry Oracle
  • Session_info.ksh Linux/Unix
  • pvm_rbs1.sql (to collect rbs info from db) Oracle
  • standard Monitoring – 1 Oracle
  • RMAN : Consistent Backup, Restore and Recovery using RMAN Oracle
  • How To Resolve Stranded DBA_2PC_PENDING Entries ID 401302.1 (Very Good prooven) Oracle

prepfiles.sh for step by step generating pending statistics files

Posted on 10-Mar-202610-Mar-2026 By Admin No Comments on prepfiles.sh for step by step generating pending statistics files

This script, prepfiles.sh, is a powerful utility for Oracle Database Administrators designed to automate the generation of a comprehensive SQL toolkit for managing and testing database statistics.

By reading a list of tables from mtx_tablelist.lst, the script creates a sequenced set of SQL files that handle the entire lifecycle of Pending Statistics. This allows DBAs to gather and validate new statistics in a “private” area before making them visible to the optimizer, reducing the risk of unexpected execution plan changes.

# config parameters

# Need to provide filename that has all tables in 'OWNER','TABLE_NAME' fomrat.Here it is tablelist.lst
# Provide backup stats table name
# modify 01_cr_stat_table.sql for that backup stats table creation.
# stat table creation systax: exec dbms_stats.create_stat_table('OWNER', 'TABLE_NAME');


#touching file
touch 01_cr_stat_table.sql
sleep 1

# preset.sql file creation
cat <<EOD00 > preset.sql
set time on timing on echo on

EOD00


# file# 2 creation
sed 's/^/exec dbms_stats.export_table_stats( /' tablelist.lst > tempfile
sed "s/$/, NULL, 'DMS_STAT_TABLE_10MAR2026');/" tempfile > tempfile1
cat  preset.sql tempfile1 >  02_exp_stats.sql

sleep 1

#cp 03_view_stat_history_reten.sql 03_view_stat_history_reten.sql
#./cr_03_view_stat_history_reten.sh
cat <<EOD1 > 03_view_stat_history_reten.sql
--check default retention

select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual;


--Change default retention for statistics
--SQL> execute DBMS_STATS.ALTER_STATS_HISTORY_RETENTION (120);
--Where xx=number of days to retain stats.

-- Check stats availability

select DBMS_STATS.GET_STATS_HISTORY_AVAILABILITY from dual;

EOD1


sleep 1

# file# 4 creation
sed "s/^/select dbms_stats.get_prefs('PUBLISH', /" tablelist.lst > tempfile
sed 's/$/) from dual;/' tempfile > tempfile1
cat  preset.sql tempfile1 >  04_check_publish_prefs.sql

# file# 5 creation
sed 's/^/exec dbms_stats.set_table_prefs( /' tablelist.lst > tempfile
sed "s/$/, 'PUBLISH', 'FALSE');/" tempfile > tempfile1
cat preset.sql tempfile1 > 05_set_publish_pref.sql

sleep 1

# file# 6 creation
sed "s/^/select dbms_stats.get_prefs('PUBLISH', /" tablelist.lst > tempfile
sed 's/$/) from dual;/' tempfile > tempfile1
cat preset.sql tempfile1 > 06_check_publish_prefs.sql

sleep 1

# file#7 creation
#cp 07_check_pending_param.sql 07_check_pending_param.sql
cat << EOD2 > 07_check_pending_param.sql
-- This parameter should be false for now. We need to make it true for optimize to use pending stats later.

show parameter OPTIMIZER_USE_PENDING_STATISTICS

EOD2

sleep 1

#file#8 creation
#cp 08_check_existing_pending_stats.sql 08_check_existing_pending_stats.sql
cat << EOD3 > 08_check_existing_pending_stats.sql
-- Need to check if anything else is using pening stats in db or not.
-- following should return 0 rows now.
select * from dba_tab_pending_stats;

EOD3

sleep 1

# file# 9 creation
sed 's/,/, tabname => /' tablelist.lst > tempfile
sed 's/^/exec dbms_stats.gather_table_stats(ownname => /' tempfile > tempfile1
sed 's/$/, estimate_percent => 100, degree => 4, cascade => true, no_invalidate => true); /' tempfile1 > tempfile
cat preset.sql tempfile > 09_gather_stats.sql
sleep 1
#file 10 creation
#cp  10_confirm_pending_stats.sql 10_confirm_pending_stats.sql
cat << EOD4 > 10_confirm_pending_stats.sql
--The last analyzed for these tables will not be changed as stats are gathered in pending area.


col owner format a20
col table_name format a30
col last_analyzed format a30
alter session set nls_date_format='DD-MON-RRRR:HH24:MI:SS';

select owner, table_name, num_rows, avg_row_len, last_analyzed from dba_tables
where (owner, table_name) in
(
select owner, table_name from dba_tab_pending_stats;
);


-- You can see the pending stats gathered in dba_tab_pending_stats table.
select owner, table_name, last_analyzed from dba_tab_pending_stats;

EOD4

sleep 1
#file#11 creation
#cp  11_use_pending_stats.sql 11_use_pending_stats.sql
cat << EOD5 > 11_use_pending_stats.sql
-- try to see from session level or system level.

--alter session set OPTIMIZER_USE_PENDING_STATISTICS = true;

--or

--alter system set  OPTIMIZER_USE_PENDING_STATISTICS = true scope=both sid='*';

EOD5

sleep 1

# file #12 creation
sed 's/^/exec dbms_stats.publish_pending_stats(/' tablelist.lst > tempfile
sed 's/$/);/' tempfile > tempfile1
cat preset.sql tempfile1 > 12_publish_pendning_stats.sql

# file #13 creation
sed 's/^/exec dbms_stats.delete_pending_stats(/' tablelist.lst > tempfile
sed 's/$/);/' tempfile > tempfile1
cat preset.sql tempfile1 > 13_ROLLBACK_ONLY_delete_pendning_stats.sql

# file #14 creation
sed 's/,/, tabname => /' tablelist.lst > tempfile
sed 's/^/exec dbms_stats.import_table_stats(ownname => /' tempfile > tempfile1
sed "s/$/,  stattab => 'STAT_TABLE_PHASE21'); /" tempfile1 > tempfile
cat preset.sql tempfile > 14_ROLLBACK_ONLY_imp_table_stats.sql

# file #15 creation
sed 's/,/, tabname => /' tablelist.lst > tempfile
sed 's/^/Execute DBMS_STATS.RESTORE_TABLE_STATS ( /' tempfile > tempfile1
sed "s/$/ , sysdate - 10); /" tempfile1 > tempfile
cat preset.sql tempfile > 15_ROLLBACK_ONLY_restore_from_stats_history.sql

rm tempfile tempfile1
Oracle, shell

Post navigation

Previous Post: tracksqltime.sql
Next Post: refre.sql for multitenant

Related Posts

  • Absolute file number and relative file number Oracle
  • _B_TREE_BITMAP_PLANS issue during 8.1.7 to 9.2.0.8 upgrade Oracle
  • Rman Notes -1 Oracle
  • pvm_rbs1.sql (to collect rbs info from db) Oracle
  • Good link for LIO in Oracle ( Logical IOs) Oracle
  • Rownum with Order by Oracle

Leave a Reply Cancel reply

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

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (403)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (350)
  • SQL Server (6)
  • Uncategorized (5)
  • Videos (0)

Recent Posts

  • Key Management in Oracle: The Core Issue: Missing Master Key12-May-2026
  • SAT Mathematics 10 questions and answer at the end.30-Apr-2026
  • top 10 AI news today30-Apr-2026
  • runon_allpdbs_show_conname.sh23-Apr-2026
  • runon_allcdbs_find_pdbs.sql23-Apr-2026
  • Running PDB on single node in RAC09-Apr-2026
  • find_arc.sql09-Apr-2026
  • pvm_pre_change.sql08-Apr-2026
  • find_encr_wallet.sql08-Apr-2026
  • find_pdbs.sql08-Apr-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Ports used by Oracle Software Oracle
  • How to set Processes Parameter Oracle
  • Metalink Note: Note:250655.1 : ADDM Basics USING THE AUTOMATIC DATABASE DIAGNOSTIC MONITOR Oracle
  • How to Use DBMS_STATS to Move Statistics to a Different Database Oracle
  • How to remove blank lines using vi editor command Linux/Unix
  • Kernel Parameters for Solaris Linux/Unix
  • AWR license Oracle
  • segment_wise_space.sql segspace.sql Segment wise space usage (allocated and used) Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme