Professional Documents
Culture Documents
SQL Tuning Guide - pt2
SQL Tuning Guide - pt2
SQL Tuning Guide - pt2
the number of rows returned. Thus, the table contains 932 rows that meet the conditions,
but the optimizer estimates 53, as shown in bold.
If the database had used dynamic statistics for this plan, then the Note section of the plan
output would have indicated this fact. The optimizer did not use dynamic statistics
because the statement executed serially, standard statistics exist, and the parameter
OPTIMIZER_DYNAMIC_SAMPLING is set to the default of 2.
4. Set the dynamic statistics level to 4 in the session using the following statement:
The new plan shows a more accurate estimate of the number of rows, as shown by the
value 932 in bold:
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
Plan hash value: 2008213504
------------------------------------------------------------------------
| Id | Operation | Name |Rows |Bytes |Cost (%CPU)|Time |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 932 | 271K| 406 (1)| 00:00:05 |
|* 1 | TABLE ACCESS FULL| CUSTOMERS | 932 | 271K| 406 (1)| 00:00:05 |
------------------------------------------------------------------------
Note
-----
- dynamic statistics used for this statement (level=4)
The note at the bottom of the plan indicates that the sampling level is 4. The additional
dynamic statistics made the optimizer aware of the real-world relationship between the
cust_city and cust_state_province columns, thereby enabling it to produce a more
accurate estimate for the number of rows: 932 rather than 53.
12-15
Chapter 12
Managing SQL Plan Directives
See Also:
See Also:
Oracle Database Reference to learn about the
OPTIMIZER_DYNAMIC_SAMPLING initialization parameter
12-16
Chapter 12
Managing SQL Plan Directives
Procedure Description
FLUSH_SQL_PLAN_DIRECTIVE Forces the database to write directives from memory to
persistent storage in the SYSAUX tablespace.
DROP_SQL_PLAN_DIRECTIVE Drops a SQL plan directive. If a directive that triggers
dynamic sampling is creating unacceptable performance
overhead, then you may want to remove it manually.
If a SQL plan directive is dropped manually or automatically,
then the database can re-create it. To prevent its re-creation,
you can use DBMS_SPM.ALTER_SQL_PLAN_DIRECTIVE to
do the following:
• Disable the directive by setting ENABLED to NO
• Prevent the directive from being dropped by setting
AUTO_DROP to NO
To disable SQL plan directives, set
OPTIMIZER_ADAPTIVE_STATISTICS to FALSE.
Prerequisites
You must have the Administer SQL Management Object privilege to execute the DBMS_SPD
APIs.
Assumptions
This tutorial assumes that you want to do the following:
• Write all directives for the sh schema to persistent storage.
• Delete all directives for the sh schema.
BEGIN
DBMS_SPD.FLUSH_SQL_PLAN_DIRECTIVE;
END;
/
3. Query the data dictionary for information about existing directives in the sh schema.
Example 12-3 queries the data dictionary for information about the directive.
4. Delete the existing SQL plan directive for the sh schema.
The following PL/SQL program unit deletes the SQL plan directive with the ID
1484026771529551585:
BEGIN
DBMS_SPD.DROP_SQL_PLAN_DIRECTIVE ( directive_id =>
12-17
Chapter 12
Managing SQL Plan Directives
1484026771529551585 );
END;
/
See Also:
12-18
13
Gathering Optimizer Statistics
This chapter explains how to use the DBMS_STATS.GATHER_*_STATS program units.
See Also:
Note:
Data visibility and privilege requirements may differ when using automatic optimizer
statistics collection with pluggable databases.
To collect the optimizer statistics, the database calls an internal procedure that operates
similarly to the GATHER_DATABASE_STATS procedure with the GATHER AUTO option. Automatic
statistics collection honors all preferences set in the database.
The principal difference between manual and automatic collection is that the latter prioritizes
database objects that need statistics. Before the maintenance window closes, automatic
collection assesses all objects and prioritizes objects that have no statistics or very old
statistics.
13-1
Chapter 13
Configuring Automatic Optimizer Statistics Collection
Note:
When gathering statistics manually, you can reproduce the object
prioritization of automatic collection by using the DBMS_AUTO_TASK_IMMEDIATE
package. This package runs the same statistics gathering job that is
executed during the automatic nightly statistics gathering job.
See Also:
Oracle Database Administrator’s Guide for a table that summarizes how
manageability features work in a container database (CDB)
Prerequisites
Access the Database Home page, as described in "Accessing the Database Home
Page in Cloud Control."
13-2
Chapter 13
Configuring Automatic Optimizer Statistics Collection
Note:
Oracle strongly recommends that you not disable automatic statistics
gathering because it is critical for the optimizer to generate optimal plans for
queries against dictionary and user objects. If you disable automatic
collection, ensure that you have a good manual statistics collection strategy
for dictionary and user schemas.
b. To disable statistics gathering for specific days in the week, check the appropriate
box next to the window name.
c. To change the characteristics of a window group, click Edit Window Group.
d. To change the times for a window, click the name of the window (for example,
MONDAY_WINDOW), and then in the Schedule section, click Edit.
The Edit Window page appears.
In this page, you can change the parameters such as duration and start time for
window execution.
e. Click Apply.
See Also:
Online Help for Oracle Enterprise Manager Cloud Control
13-3
Chapter 13
Configuring Automatic Optimizer Statistics Collection
Note:
Because monitoring and many automatic features are disabled, Oracle
strongly recommends that you do not set STATISTICS_LEVEL to BASIC.
BEGIN
DBMS_AUTO_TASK_ADMIN.ENABLE (
client_name => 'auto optimizer stats collection'
, operation => NULL
, window_name => NULL
);
END;
/
BEGIN
DBMS_AUTO_TASK_ADMIN.DISABLE (
client_name => 'auto optimizer stats collection'
, operation => NULL
, window_name => NULL
);
END;
/
13-4
Chapter 13
Gathering Optimizer Statistics Manually
CLIENT_NAME STATUS
------------------------------- --------
auto optimizer stats collection ENABLED
BEGIN
DBMS_SCHEDULER.SET_ATTRIBUTE (
'MONDAY_WINDOW'
, 'repeat_interval'
, 'freq=daily;byday=MON;byhour=05;byminute=0;bysecond=0'
);
END;
/
See Also:
• Oracle Database PL/SQL Packages and Types Reference to learn about the
DBMS_AUTO_TASK_ADMIN package
• Oracle Database Reference to learn about the STATISTICS_LEVEL initialization
parameter
13-5
Chapter 13
Gathering Optimizer Statistics Manually
See Also:
Procedure Purpose
GATHER_INDEX_STATS Collects index statistics
GATHER_TABLE_STATS Collects table, column, and index statistics
GATHER_SCHEMA_STATS Collects statistics for all objects in a schema
GATHER_DICTIONARY_STATS Collects statistics for all system schemas, including SYS
and SYSTEM, and other optional schemas, such as
CTXSYS and DRSYS
GATHER_DATABASE_STATS Collects statistics for all objects in a database
When the OPTIONS parameter is set to GATHER STALE or GATHER AUTO, the
GATHER_SCHEMA_STATS and GATHER_DATABASE_STATS procedures gather statistics for
any table that has stale statistics and any table that is missing statistics. If a monitored
table has been modified more than 10%, then the database considers these statistics
stale and gathers them again.
Note:
As explained in "Configuring Automatic Optimizer Statistics Collection", you
can configure a nightly job to gather statistics automatically.
See Also:
13-6
Chapter 13
Gathering Optimizer Statistics Manually
This section offers guidelines for typical situations in which you may choose to gather
statistically manually:
13-7
Chapter 13
Gathering Optimizer Statistics Manually
The DBA_TABLES.SAMPLE_SIZE column indicates the actual sample size used to gather
statistics.
See Also:
• "Hybrid Histograms"
• Oracle Database PL/SQL Packages and Types Reference to learn more
about DBMS_STATS.AUTO_SAMPLE_SIZE
Note:
Do not confuse gathering statistics in parallel with gathering statistics
concurrently.
See Also:
13-8
Chapter 13
Gathering Optimizer Statistics Manually
the default value of AUTO to gather subpartition, partition, or global statistics, depending on
partition type. The ALL setting gathers statistics for all types.
See Also:
"Gathering Incremental Statistics on Partitioned Objects"
See Also:
13-9
Chapter 13
Gathering Optimizer Statistics Manually
Note:
Starting in Oracle Database 12c Release 2 (12.2), you no longer need to use
DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO to ensure that view
metadata is current. The statistics shown in the DBA_TAB_STATISTICS,
DBA_IND_STATISTICS, and DBA_TAB_MODIFICATIONS views are obtained from
both disk and memory.
• YES
The statistics are stale.
• NO
The statistics are not stale.
• null
The statistics are not collected.
Executing GATHER_SCHEMA_STATS or GATHER_DATABASE_STATS with the GATHER AUTO
option collects statistics only for objects with no statistics or stale statistics.
PARTITION_NAME STA
--------------- ---
SALES_1995 NO
SALES_1996 NO
SALES_H1_1997 NO
SALES_H2_1997 NO
SALES_Q1_1998 NO
SALES_Q1_1999 NO
.
.
.
13-10
Chapter 13
Gathering Optimizer Statistics Manually
See Also:
Oracle Database Reference to learn about the DBA_TAB_MODIFICATIONS view
BEGIN
DBMS_STATS.GATHER_TABLE_STATS (
ownname => 'sh'
, tabname => 'customers'
, degree => 2
);
END;
/
See Also:
Oracle Database PL/SQL Packages and Types Reference to learn about the
GATHER_TABLE_STATS procedure
13-11
Chapter 13
Gathering Optimizer Statistics Manually
could potentially lead to a suboptimal execution plan. Thus, it is important to keep fixed
object statistics current.
Oracle Database automatically gathers fixed object statistics as part of automated
statistics gathering if they have not been previously collected. You can also manually
collect statistics on fixed objects by calling DBMS_STATS.GATHER_FIXED_OBJECTS_STATS.
Oracle recommends that you gather statistics when the database has representative
activity.
Prerequisites
You must have the SYSDBA or ANALYZE ANY DICTIONARY system privilege to execute
this procedure.
BEGIN
DBMS_STATS.GATHER_FIXED_OBJECTS_STATS;
END;
/
See Also:
13-12
Chapter 13
Gathering Optimizer Statistics Manually
Assumptions
This tutorial assumes the following:
• The oe.orders table is extremely volatile.
• You want to delete and then lock the statistics on the orders table to prevent the
database from gathering statistics on the table. In this way, the database can dynamically
gather necessary statistics as part of query optimization.
• The oe user has the necessary privileges to query DBMS_XPLAN.DISPLAY_CURSOR.
BEGIN
DBMS_STATS.DELETE_TABLE_STATS('OE','ORDERS');
END;
/
BEGIN
DBMS_STATS.LOCK_TABLE_STATS('OE','ORDERS');
END;
/
---------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 2 (100)| |
13-13
Chapter 13
Gathering Optimizer Statistics Manually
| 1 | SORT AGGREGATE | | 1 | |
|
| 2 | TABLE ACCESS FULL| ORDERS | 105 | 2 (0)| 00:00:01
|
--------------------------------------------------------------------
-
Note
-----
- dynamic statistics used for this statement (level=2)
The Note in the preceding execution plan shows that the database used dynamic
statistics for the SELECT statement.
See Also:
Note:
Concurrent statistics gathering mode does not rely on parallel query
processing, but is usable with it.
13-14
Chapter 13
Gathering Optimizer Statistics Manually
• Oracle Scheduler
• Oracle Database Advanced Queuing (AQ)
• Oracle Database Resource Manager (the Resource Manager)
Enable concurrent statistics gathering by setting the CONCURRENT preference with
DBMS_STATS.SET_GLOBAL_PREF.
The database runs as many concurrent jobs as possible. The Job Scheduler decides how
many jobs to execute concurrently and how many to queue. As running jobs complete, the
scheduler dequeues and runs more jobs until the database has gathered statistics on all
tables, partitions, and subpartitions. The maximum number of jobs is bounded by the
JOB_QUEUE_PROCESSES initialization parameter and available system resources.
In most cases, the DBMS_STATS procedures create a separate job for each table partition or
subpartition. However, if the partition or subpartition is empty or very small, then the database
may automatically batch the object with other small objects into a single job to reduce the
overhead of job maintenance.
The following figure illustrates the creation of jobs at different levels, where Table 3 is a
partitioned table, and the other tables are nonpartitioned. Job 3 acts as a coordinator job for
Table 3, and creates a job for each partition in that table, and a separate job for the global
statistics of Table 3. This example assumes that incremental statistics gathering is disabled; if
enabled, then the database derives global statistics from partition-level statistics after jobs for
partitions complete.
Level 1
Level 2
13-15
Chapter 13
Gathering Optimizer Statistics Manually
See Also:
Note:
The ORA$AUTOTASK consumer group is shared with the maintenance tasks
that automatically run during the maintenance windows. Thus, when
concurrency is activated for automatic statistics gathering, the database
automatically manages resources, with no extra steps required.
See Also:
Oracle Database Administrator’s Guide to learn about the Resource
Manager
13-16
Chapter 13
Gathering Optimizer Statistics Manually
Prerequisites
This tutorial has the following prerequisites:
• In addition to the standard privileges for gathering statistics, you must have the following
privileges:
– CREATE JOB
– MANAGE SCHEDULER
– MANAGE ANY QUEUE
• The SYSAUX tablespace must be online because the scheduler stores its internal tables
and views in this tablespace.
• The JOB_QUEUE_PROCESSES initialization parameter must be set to at least 4.
• The Resource Manager must be enabled.
By default, the Resource Manager is disabled. If you do not have a resource plan, then
consider enabling the Resource Manager with the system-supplied DEFAULT_PLAN.
Assumptions
This tutorial assumes that you want to do the following:
• Enable concurrent statistics gathering
• Gather statistics for the sh schema
• Monitor the gathering of the sh statistics
2. Set the JOB_QUEUE_PROCESSES initialization parameter to at least twice the number of CPU
cores.
In Oracle Real Application Clusters, the JOB_QUEUE_PROCESSES setting applies to each
node.
13-17