Beruflich Dokumente
Kultur Dokumente
DBMS_STATS
Usually DBA face issue like unexpected execution plan by sql query. One of the common
reason for this could be statistics of the table or schema which are chosen by optimizer
at time of generating execution plan. Since this could be a production environment So
Database Administrator can't do so much tweaking.
In this situation, One good idea is to Move Table or Schema Statistics to a Different Database using
DBMS_STATS and do what ever research you wants to do. Database administrator can export a
single table stats using dbms_stats.export_table_stats as well as whole schema status using
dbms_stats.export_schema_stats.
One major advantage of moving Table or Schema Statistics is DBA will get same execution plan of
queries at destination schema regardless of actual data stored at destination. This is because the Cost-
Based Optimizer makes its decisions for obtaining data on the basis of statistics.
As first command output shows stats are not gathered for "EXPORT_STATS" table. Database
Administrator should collect the table stats, this can also make an impact on current execution plan of
queries running on this table. After gathering table status Now user_tables is updated with current stats
of the table. Now DBA can take stats backup.
Here, I have taken "EXPORT_STATS" stats backup into "STATS" table. To know more about
export_table_stats.
4. Restore Stats Table using Impdp: As DBA has moved dump on destination server
execute this command on destination server to restore dump file. Now, DBA has to restore stats to the
destination table using impdp command.
[oracle@database ~]$ impdp scott/tiger tables=stats dumpfile=stats.dmp
logfile=stat_bku.log
5. Import Table Stats: Please make sure destination table also has same
structure and name as source table for importing table stats. Here, i am also taking a
table "EXPORT_STATS" for getting back the stats. This table doesn't have any data, while
source table has 585392 rows. Now, we will import table stats into destination table.
Make sure destination table name is sure other wise Oracle will execute the command
without any error, but will not import stats.
From the above screen shot, Database Administrator can notice EXPORT_STATS doesn't have any
row but user_tables stats are showing 585392 rows into this table.This will generate same execution
plan for any query on this table as on source database. In this way DBA can replicate production
enviorment at local system witout copying data from production.
Trick to Copy Table Stats into Another Table: As I said earlier in this post, It's
not possible to import one table stats into another table. In fact DBA can do that by playing a small
trick. Suppose soruce table is EXPORT_STATS and database admin wants to move stats to
IMPORT_STATS then Here is the trick.
SQL> select c1, c5 from stats;
C1 C5
----------------------------- ------------------------------
EXPORT_STATS SCOTT
EXPORT_STATS SCOTT
EXPORT_STATS SCOTT
EXPORT_STATS SCOTT
4 rows selected.
Just update stats C1 column with new table column name and import the stats. This will restore stats
into a new table, but make sure table structure is same.
To export and import statisticians for an entire schema use exec dbms_stats.export_schema_stats in #3
and exec dbms_stats.import_schema_stats in #5 rest of the process is same.