SQL Profiles is a new feature from 10g which is managed by Oracle Enterprise
Manager as part of the Automatic SQL Tuning process. Apart from OEM, SQL
Profiles can be managed through the DBMS_SQLTUNE package.
This FAQ
answers basic questions on using SQL Profiles and provides an example using
DBMS_SQLTUNE package.
DBAs and Support Analysts
The query optimizer can sometimes produce inaccurate estimates about an
attribute of a statement due to lack of information,leading to poor execution
plans. Traditionally, users have had to correct this problem by manually adding
hints to the application code to guide the optimizer into making correct
decisions. For packaged applications, changing application code is not an option
and the only alternative available is to log a bug with the application vendor
and wait for a fix.
Automatic SQL Tuning deals with this problem with its
SQL Profiling capability. The Automatic Tuning Optimizer creates a profile of
the SQL statement called a SQL Profile, consisting of auxiliary statistics
specific to that statement. The query optimizer under normal mode makes
estimates about cardinality, selectivity, and cost that can sometimes be off
by a significant amount resulting in poor execution plans. SQL Profile
addresses this problem by collecting additional information using sampling and
partial execution techniques to verify and, if necessary, adjust these
estimates.
During SQL Profiling, the Automatic Tuning Optimizer also uses
execution history information of the SQL statement to appropriately set
optimizer parameter settings, such as changing the OPTIMIZER_MODE
initialization parameter setting from ALL_ROWS to FIRST_ROWS for that SQL
statement.
The output of this type of analysis is a recommendation to
accept the SQL Profile.
An SQL Profile, once accepted, is stored
persistently in the data dictionary. An SQL Profile is specific to a particular
query. If accepted, the optimizer under normal mode uses the information in the
SQL Profile in conjunction with regular database statistics when generating an
execution plan.
The availability of the additional information makes it
possible to produce well-tuned plans for corresponding SQL statement without
requiring any change to the application code.
SQL Profile is a collection of information stored in the data dictionary that
enables the query optimizer to create an optimal execution plan for a SQL
statement.The SQL profile contains corrections for poor optimizer estimates
discovered during Automatic SQL Tuning. This information can improve optimizer
cardinality and selectivity estimates, which in turn leads the optimizer to
select better plans.
The SQL profile does not contain information about
individual execution plans. Rather, the optimizer has the following sources of
information when choosing plans:
It is important to note that the SQL Profile does not freeze the execution plan of a SQL statement, as done by stored outlines. As tables grow or indexes are created or dropped, the execution plan can change with the same SQL Profile. The information stored in it continues to be relevant even as the data distribution or access path of the corresponding statement change. However, over a long period of time, its content can become outdated and would have to be regenerated. This can be done by running Automatic SQL Tuning again on the same statement to regenerate the SQL Profile.
The scope of a SQL Profile can be controlled by the CATEGORY profile attribute. This attribute determines which user sessions can apply the profile. You can view the CATEGORY attribute for a SQL Profile in CATEGORY column of the DBA_SQL_PROFILES view.
By default, all profiles are created in the DEFAULT category. This means
that all user sessions where the SQLTUNE_CATEGORY initialization parameter is
set to DEFAULT can use the profile.
By altering the category of a SQL
profile, you can determine which sessions are affected by the creation of a
profile. For example, by setting the category of a SQL Profile to DEV, only
those users sessions where the SQLTUNE_CATEGORY initialization parameter is set
to DEV can use the profile. All other sessions do not have access to the SQL
Profile and execution plans for SQL statements are not impacted by the SQL
profile. This technique enables you to test a SQL Profile in a restricted
environment before making it available to other user sessions.
SELECT statements
UPDATE statements
INSERT statements (only with a
SELECT clause)
DELETE statements
CREATE TABLE statements (only with the
AS SELECT clause)
MERGE statements (the update or insert operations)
SQL Profiles can be handled by Oracle Enterprise Manager as part of the Automatic SQL Tuning process or manually by using DBMS_SQLTUNE package.
To use the SQL Profiles APIs, you need the CREATE ANY SQL_PROFILE, DROP ANY SQL_PROFILE, and ALTER ANY SQL_PROFILE system privileges.
Accepting a SQL ProfileUse the DBMS_SQLTUNE.ACCEPT_SQL_PROFILE procedure to accept a SQL Profile recommended by the SQL Tuning Advisor. This creates and stores a SQL Profile in the database.
my_sql_tuning_task is the name of the SQL tuning task.
You can view
information about a SQL Profile in the DBA_SQL_PROFILES view.
Alter the STATUS, NAME, DESCRIPTION, and CATEGORY attributes of an existing SQL Profile with the ALTER_SQL_PROFILE procedure.
In this example, my_sql_profile is the name of the SQL Profile that you
want to alter.
The status attribute is changed to disabled which means the
SQL Profile will not be used during SQL compilation.
A SQL Profile can be dropped using the DROP_SQL_PROFILE procedure.
SESSION 1 --
SCOTT
Create table, populate, create index and gather
statistics
Execute query with no_index hint
Full Table Scan used
Plan hash value: 217508114
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST | 1 | 4 | 4 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("N"=1)
SESSION 2 --
SYS
Create and execute tuning task and run report tuning
task.
Accept recommended SQL Profile
Output:
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name : my_sql_tuning_task_2
Tuning Task Owner : SYS
Workload Type : Single SQL Statement
Scope : COMPREHENSIVE
Time Limit(seconds): 60
Completion Status : COMPLETED
Started at : 09/24/2012 12:36:44
Completed at : 09/24/2012 12:36:49
-------------------------------------------------------------------------------
Schema Name: SCOTT
SQL ID : d4wgpc5g0s0vu
SQL Text : select /*+ no_index(test test_idx) */ * from test where n=1
-------------------------------------------------------------------------------
FINDINGS SECTION (1 finding)
-------------------------------------------------------------------------------
1- SQL Profile Finding (see explain plans section below)
--------------------------------------------------------
1- SQL Profile Finding (see explain plans section below)
--------------------------------------------------------
A potentially better execution plan was found for this statement.
Recommendation (estimated benefit: 90.95%)
------------------------------------------
- Consider accepting the recommended SQL profile.
execute dbms_sqltune.accept_sql_profile(task_name =>
'my_sql_tuning_task_2', task_owner => 'SYS', replace => TRUE);
Validation results
------------------
The SQL profile was tested by executing both its plan and the original plan
and measuring their respective execution statistics. A plan may have been
only partially executed if the other could be run to completion in less time.
Original Plan With SQL Profile % Improved
------------- ---------------- ----------
Completion Status: COMPLETE COMPLETE
Elapsed Time (s): .001004 .000331 67.03 %
CPU Time (s): .001 0 100 %
User I/O Time (s): 0 0
Buffer Gets: 22 2 90.9 %
Physical Read Requests: 0 0
Physical Write Requests: 0 0
Physical Read Bytes: 0 0
Physical Write Bytes: 0 0
Rows Processed: 1 1
Fetches: 1 1
Executions: 1 1
Notes
-----
1. Statistics for the original plan were averaged over 10 executions.
2. Statistics for the SQL profile plan were averaged over 10 executions.
-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------
1- Original With Adjusted Cost
------------------------------
Plan hash value: 217508114
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST | 1 | 4 | 4 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("N"=1)
2- Using SQL Profile
--------------------
Plan hash value: 1416057887
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| TEST_IDX | 1 | 4 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("N"=1)
-------------------------------------------------------------------------------
SESSION 1 --
SCOTT
Run query again
Even with no_index hint , index
is used
Note: In Explain Plan we see "SQL profile "my_sql_profile" used for
this statement
Execution Plan
-------------------------------------------------------------------------
Plan hash value: 1416057887
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| TEST_IDX | 1 | 4 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("N"=1)
---------------
Note
-----
- SQL profile "my_sql_profile" used for this statement
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22308399/viewspace-750996/ ,如需转载,请注明出处,否则将追究法律责任。