You are on page 1of 5

13-2. Enabling Automatic Statistics Gathering DBMS_AUTO_TASK_ADMIN package.

SQL> select client_name,status from dba_autotask_client; CLIENT_NAME STATUS ---------------------------------------------------------------- -------auto optimizer stats collection DISABLED auto space advisor ENABLED sql tuning advisor ENABLED SQL> Execute the dbms_auto_task_admin.enable procedure to enable the automatic statis tics collection task: SQL> begin dbms_auto_task_admin.enable( 2 client_name=>'auto optimizer stats collection', 3 operation=>NULL, 4 window_name=>NULL); 5 end; 6 / PL/SQL procedure successfully completed. Check the status of the auto optimizer stats collection task: SQL> SELECT client_name,status from dba_autotask_client; CLIENT_NAME STATUS ---------------------------------------------------------------- -------auto optimizer stats collection ENABLED auto space advisor ENABLED sql tuning advisor ENABLED DBMS_STATS.GATHER_DATABASE_STATS_JOB_PROC procedure DBMS_STATS.GATHER_DATABASE_STATS procedure the big difference is that the database will collect the statistics only during the maintenance window that you specify. DBA_OPTSTAT_OPERATIONS view SQL> select operation,target,start_time,end_time from dba_optstat_operations 2* where operation='gather_database_stats(auto)'; OPERATION START_TIME END_TIME --------------------- ---------------------------- ---------------------------gather_database_stats 26-APR-11 10.00.02.970000 PM 26-APR-11 10.03.11.671000 PM 13-3. Setting Preferences for Statistics Collection DBMS_STATS.SET_*_PREFS procedure -to change the default values for parameters th at control statistics collection.

SET_TABLE_PREFS: Lets you specify default parameters to be used by the DBMS_STATS.GATHER_*_STATS procedures for a specific table SET_SCHEMA_PREFS: Lets you change the default parameters to be used by the DBMS_STATS.GATHER_*_STATS procedures for all objects in a specific schema

SET_DATABASE_PREFS: Lets you change the default parameters to be used by the DBMS_STATS.GATHER_*_STATS procedures for the entire database, including all user schemas and system schemas such as SYS and SYSTEM SET_GLOBAL_PREFS: Sets global statistics preferences; this procedure lets you change the default statistic collection parameters for any object in the databas e that doesn't have an existing preference at the table level. If you don't set tablel evel preferences or you don't set any parameter explicitly in the DBMS_STATS.GATHER_*_STATS procedure, the parameters default to their global settings. execute dbms_stats.set_database_prefs('ESTIMATE_PERCENT','20'); SQL> select dbms_stats.get_prefs ('STALE_PERCENT','SH') stale_percent from dual; STALE_PERCENT -------------------------------------------------------------------------------10

execute dbms_stats.delete_table_stats('OE','ORDERS'); execute dbms_stats.lock_table_stats('OE','ORDERS'); Bulk Loaded Tables For tables that you bulk load data into, you must collect statistics immediately after loading the data. If you don't collect statistics right after bulk loading data into a table, the datab ase can still use dynamic sampling to estimate the statistics, but these statistics aren't as comprehensive as the statistics that you collect. Collect the statistics when the table has the number of rows that represent its typical state. Once you do this, lock the statistics to prevent the automatic statistics collection job from collecting fresh statistics for this table during the nightly maintenance window. The other option is to make the statistics of the table null. execute dbms_stats.delete_table_stats('OE','ORDERS'); execute dbms_stats.lock_table_stats('OE','ORDERS'); 13-5. Locking Statistics SQL> execute dbms_stats.lock_table_stats(ownname=>'SH',tabname=>'SALES'); SQL> execute dbms_stats.unlock_table_stats(ownname=>'SH',tabname=>'SALES'); 13-6. Handling Missing Statistics Problem Certain tables in your database are missing statistics, because the tables have had data loaded into them outside the nightly batch window. You can't collect statistics on the table during the day when the database is handling other workload.

Solution Oracle uses dynamic sampling to compensate for missing statistics. The database will scan a random sample of data blocks in a table when you enable dynamic sampling. You enable/di sable dynamic sampling in the database by setting the optimizer_dynamic_sampling initializatio n parameter. Dynamic sampling is enabled by default, as you can see from the following: SQL> sho parameter dynamic NAME TYPE VALUE ------------------------------------ ----------- ----optimizer_dynamic_sampling integer 2 SQL> The default level of dynamic sampling is 2setting it to 0 disables dynamic sampli ng. You can modify the default value by setting a different sampling level as shown here: SQL> alter system set optimizer_dynamic_sampling=4 scope=both; System altered. SQL> 13-7. Exporting Statistics DBMS_STATS.EXPORT_*_STATS procedures DBMS_STATS.IMPORT_*_STATS procedures to import the statistics into a different d atabase 13-8. Restoring Previous Versions of Statistics DBMS_STATS.RESTORE_STATS procedure to revert to an older set of optimizer statis tics. SQL> select dbms_stats.get_stats_history_availability from dual; GET_STATS_HISTORY_AVAILABILITY ---------------------------------------------------------------------19-APR-11 07.49.26.718000000 AM -04:00 exec dbms_stats.restore_schema_stats(ownname=>'SH',as_of_timestamp=>'19-MAY -11 01.30.31.323000 PM -04:00',no_invalidate=>false) select dbms_stats.get_stats_history_retention from dual; GET_STATS_HISTORY_RETENTION --------------------------31 DBMS_STATS.PURGE_STATS procedure. exec dbms_stats.alter_stats_history_retention(retention=>60); 13-9. Gathering System Statistics DBMS_STATS.GATHER_SYSTEM_STATS procedure: execute dbms_stats.gather_system_stats() execute dbms_stats.gather_system_stats('start') execute dbms_stats.gather_system_stats('stop') 13-14. Improving Performance When Not Using Bind Variables

select * from employees where job = 'Clerk' select * from employees where job=:b 13-19. Maintaining Statistics on Partitioned Tables You can use the incremental statistics maintenance feature in the Oracle 11g rel ease to maintain global statistics after each load into a new partition. 1. Turn on incremental statistics collection for the SH.SALES table: SQL> exec dbms_stats.set_table_prefs('SH','SALES','INCREMENTAL','TRUE'); 2. After each load into a partition, gather global table-level statistics as sho wn here: SQL> exec dbms_stats.gather_table_stats('SH','SALES'); In order to set the incremental statistics collection feature for partitioned ta bles, you must specify the AUTO_SAMPLE_SIZE value for the ESTIMATE_PERCENT parameter and the AUTO value for the GRANULARITY parameter. 13-20. Concurrent Statistics Collection for Large Tables Problem You want to minimize the amount of time it takes to gather statistics by taking advantage of your multiprocessor environment. SET_GLOBAL_PREFS procedure. Solution In Oracle Database 11g Release 2 (11.2.0.2), you can specify the concurrent stat istics gathering mode to gather statistics on multiple tables and multiple partitions (and subpartitions) within a table concurrently. By doing this, you can take advantage of your multi-processor envi ronment and complete the statistics collection process much faster. By default, concurrent statistics gathering is disabled. You enable it by execut ing the SET_GLOBAL_PREFS procedure. Follow these steps to enable concurrent statistics g athering. 1. Set the job_queue_processes parameter to at least 4. SQL>alter system set job_queue_processes=4; If you don't plan on using parallel execution for gathering statistics (see the following section), but want to fully utilize your system resources, you must se t the job_queue_processes parameter to two times the number of CPU cores on the server. 2. Enable concurrent statistics gathering. SQL> begin dbms_stats.set_global_prefs('CONCURRENT','TRUE'); end; / Make sure the user executing this command has the CREATE JOB, MANAGE SCHEDULER, and the MANAGE ANY QUEUE privileges.

DBA_SCHEDULER_JOBS view SQL> select job_name,state,comments from dba_scheduler_jobs where job_class like 'CONC%'; SQL> select job_name,elapsed_time from dba_scheduler_running_jobs where job_name like 'ST$%';

You might also like