Tuesday 16 September 2014

dbms_stats.import_table_stats is NOT importing the statistics ;-(

It happens quite often that copying statistics from one database to another, using the dbms_stats various procedures ( create_table_stats, export_table_stats, import_table_stats) is a bit challenging.

You run:

SQL> exec dbms_stats.import_table_stats(user,tabname=>'my_table',stattab=>'my_table_STATS');

PL/SQL procedure successfully completed.

The prompt comes back immediately and checking for example num_ros or last_abalyzed from user_tables confirms that nothing was done.

There are a few possible causes to this: the source and target table have to have the same number of partitions, the same partition names and of course, the table owner has to be the same, or , like in my case, has to be adjusted.
The column called "C5" hold the DB user name.

SQL> update my_table_STATS set c5='new_owner' where c5='old_owner';

2412 rows updated.

SQL> commit;

Commit complete.

SQL> exec dbms_stats.import_table_stats(user,tabname=>'my_table',stattab=>'my_table_STATS');

PL/SQL procedure successfully completed.

SQL> select num_rows from tabs where table_name='MY_TABLE';

  NUM_ROWS
----------
 416503400

 So now the import of stats was successful.