Вы находитесь на странице: 1из 9

SQL Controls:

=============
This part explains how to manage SQL profiles and SQL plan baselines, and migrate
stored outlines to SQL plan baselines.
contains the following chapters:

1. Managing SQL Profiles


2. Managing SQL Plan Baselines
3. Stored Outlines

(1). Managing SQL Profiles:


===========================

A SQL profile is a database object that contains auxiliary statistics specific to a


SQL statement.

SQL profiles are created when a DBA invokes SQL Tuning Advisor.

When profiling a SQL statement, SQL Tuning Advisor uses a specific set of bind
values as input.

The advisor compares the optimizer estimate with values obtained by executing
fragments of the statement on a data sample. When significant variances are found,
SQL Tuning Advisor bundles corrective actions together in a SQL profile, and then
recommends its acceptance.

A SQL profile is a collection of auxiliary statistics on a query, including all


tables and columns referenced in the query. The profile is stored in the data
dictionary. The optimizer uses this information during optimization to determine
the most optimal plan.

As explained in "SQL Profiling", SQL Tuning Advisor invokes Automatic Tuning


Optimizer to generate SQL profile recommendations. Recommendations to implement SQL
profiles occur in a finding, which appears in a separate section of the SQL Tuning
Advisor report.

SQL Profile Recommendation:-


--------------------------

-------------------------------------------------------------------------------
FINDINGS SECTION (2 findings)
-------------------------------------------------------------------------------

1- SQL Profile Finding (see explain plans section below)


--------------------------------------------------------
A potentially better execution plan was found for this statement. Choose
one of the following SQL profiles to implement.

Recommendation (estimated benefit: 99.45%)


------------------------------------------
- Consider accepting the recommended SQL profile.
execute dbms_sqltune.accept_sql_profile(task_name => 'my_task',
object_id => 3, task_owner => 'SH', 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: PARTIAL COMPLETE
Elapsed Time(us): 15467783 226902 98.53 %
CPU Time(us): 15336668 226965 98.52 %
User I/O Time(us): 0 0
Buffer Gets: 3375243 18227 99.45 %
Disk Reads: 0 0
Direct Writes: 0 0
Rows Processed: 0 109
Fetches: 0 109
Executions: 0 1

Recommendation (estimated benefit: 99.99%)


------------------------------------------
- Consider accepting the recommended SQL profile to use parallel execution
for this statement.
execute dbms_sqltune.accept_sql_profile(task_name => 'gfk_task',
object_id => 3, task_owner => 'SH', replace => TRUE,
profile_type => DBMS_SQLTUNE.PX_PROFILE);

Executing this query parallel with DOP 7 will improve its response time
82.22% over the SQL profile plan. However, there is some cost in enabling
parallel execution. It will increase the statement's resource consumption by
an estimated 24.43% which may result in a reduction of system throughput.
Also, because these resources are consumed over a much smaller duration, the
response time of concurrent statements might be negatively impacted if
sufficient hardware capacity is not available.

The following data shows some sampled statistics for this SQL from the past
week and projected weekly values when parallel execution is enabled.

Past week sampled statistics for this SQL


-----------------------------------------
Number of executions 0
Percent of total activity .29
Percent of samples with #Active Sessions > 2*CPU 0
Weekly DB time (in sec) 76.51

Projected statistics with Parallel Execution


--------------------------------------------
Weekly DB time (in sec) 95.21

SQL Profiles and SQL Plan Baselines:


------------------------------------
You can use SQL profiles with or without SQL plan management. No strict
relationship exists between the SQL profile and the plan baseline. If a statement
has multiple plans in a SQL plan baseline, then a SQL profile is useful because it
enables the optimizer to choose the lowest-cost plan in the baseline.
User Interfaces for SQL Profiles:
---------------------------------
Oracle Enterprise Manager Cloud Control (Cloud Control) usually handles SQL
profiles as part of automatic SQL tuning.

On the command line, you can manage SQL profiles with the DBMS_SQLTUNE package. To
use the APIs, you must have the ADMINISTER SQL MANAGEMENT OBJECT privilege.

Basic Tasks for SQL Profiles:


-----------------------------
Basic tasks include accepting (implementing) a SQL profile, altering it, listing
it, and dropping it.

1. Implement a recommended SQL profile.


2. Obtain information about SQL profiles stored in the database.
3. Optionally, modify the implemented SQL profile.
4. Drop the implemented SQL profile when it is no longer needed.

1). Implementing a SQL Profile:


-------------------------------
Implementing (also known as accepting) a SQL profile means storing it persistently
in the database. A profile must be implemented before the optimizer can use it as
input when generating plans.

About SQL Profile Implementation:-


As a rule of thumb, implement a SQL profile recommended by SQL Tuning Advisor.

If the database recommends both an index and a SQL profile, then either use both or
use the SQL profile only. If you create an index, then the optimizer may need the
profile to pick the new index.

Assumptions:-

This tutorial assumes the following:


a.The SQL Tuning Advisor task STA_SPECIFIC_EMP_TASK includes a recommendation to
create a SQL profile.
b.The name of the SQL profile is my_sql_profile.
c.The PL/SQL block accepts a profile that uses parallel execution (profile_type).
d.The profile uses force matching.

To implement a SQL profile:

Connect SQL*Plus to the database with the appropriate privileges, and then execute
the ACCEPT_SQL_PROFILE function.

For example, execute the following PL/SQL:

DECLARE
my_sqlprofile_name VARCHAR2(30);
BEGIN
my_sqlprofile_name := DBMS_SQLTUNE.ACCEPT_SQL_PROFILE (
task_name => 'STA_SPECIFIC_EMP_TASK'
, name => 'my_sql_profile'
, profile_type => DBMS_SQLTUNE.PX_PROFILE
, force_match => true
);
END;
/

2) Listing SQL Profiles:


------------------------
The data dictionary view DBA_SQL_PROFILES stores SQL profiles persistently in the
database. The statistics are in an Oracle internal format, so you cannot query
profiles directly. However, you can list profiles.

To list SQL profiles:

Connect SQL*Plus to the database with the appropriate privileges, and then query
the DBA_SQL_PROFILES view.

For example, execute the following query:

COLUMN category FORMAT a10


COLUMN sql_text FORMAT a20

SELECT NAME, SQL_TEXT, CATEGORY, STATUS


FROM DBA_SQL_PROFILES;
Sample output appears below:

NAME SQL_TEXT CATEGORY STATUS


------------------------------ -------------------- ---------- --------
SYS_SQLPROF_01285f6d18eb0000 select promo_name, c DEFAULT ENABLED
ount(*) c from promo
tions p, sales s whe
re s.promo_id = p.pr
omo_id and p.promo_c
ategory = 'internet'
group by p.promo_na
me order by c desc

3. Altering a SQL Profile:


--------------------------
You can alter attributes of an existing SQL profile using the attribute_name
parameter of the ALTER_SQL_PROFILE procedure.

The example in this section assumes that you want to change the category of the SQL
profile so it is used only by sessions with the SQL profile category set to TEST,
run the SQL statement, and then change the profile category back to DEFAULT.

To alter a SQL profile:

1. Connect SQL*Plus to the database with the appropriate privileges, and then use
the ALTER_SQL_PROFILE procedure to set the attribute_name.

For example, execute the following code to set the attribute CATEGORY to TEST:
VARIABLE pname my_sql_profile
BEGIN DBMS_SQLTUNE.ALTER_SQL_PROFILE (
name => :pname
, attribute_name => 'CATEGORY'
, value => 'TEST'
);
END;

2. Change the initialization parameter setting in the current database session.

For example, execute the following SQL:

ALTER SESSION SET SQLTUNE_CATEGORY = 'TEST';

3. Test the profiled SQL statement.

4. Use the ALTER_SQL_PROFILE procedure to set the attribute_name.

For example, execute the following code to set the attribute CATEGORY to DEFAULT:

VARIABLE pname my_sql_profile


BEGIN
DBMS_SQLTUNE.ALTER_SQL_PROFILE (
name => :pname
, attribute_name => 'CATEGORY'
, value => 'DEFAULT'
);
END;
/

4. Dropping a SQL Profile:


--------------------------
You can drop a SQL profile with the DROP_SQL_PROFILE procedure.

Assumptions:-
This section assumes the following:

1. You want to drop my_sql_profile.


2. You want to ignore errors raised if the name does not exist.

To drop a SQL profile:

Connect SQL*Plus to the database with the appropriate privileges, call the
DBMS_SQLTUNE.DROP_SQL_PROFILE procedure.

The following example drops the profile named my_sql_profile:

BEGIN
DBMS_SQLTUNE.DROP_SQL_PROFILE (
name => 'my_sql_profile'
);
END;
/
Examples:
=========

1). Creating and Accepting a SQL Profile

1. Use DBMS_SQLTUNE to create a tuning task.


2. Execute the tuning task.
3. Generate the tuning advice report.
4. If SQL profile is part of the tuning advice output, then create and accept it.

Step 1: Use DBMS_SQLTUNE to Create a Tuning Task

DECLARE
tune_sql CLOB;
tune_task VARCHAR2(30);
BEGIN
tune_sql := 'select count(*) from scott.emp';
tune_task := DBMS_SQLTUNE.CREATE_TUNING_TASK(
sql_text => tune_sql
,user_name => 'SCOTT'
,scope => 'COMPREHENSIVE'
,time_limit => 60
,task_name => 'TUNE1'
,description => 'Calling SQL Tuning Advisor for one statement'
);
END;
/

Step 2: Execute the Tuning Task

This step runs the SQL Tuning Advisor to generate advice regarding any queries
associated with the tuning task
(created in step 1):
SQL> exec dbms_sqltune.execute_tuning_task(task_name=>'TUNE1');

Step 3: Run Tuning Advice Report


Now use DBMS_SQLTUNE to extract any tuning advice generated in step 2:
set long 10000
set longchunksize 10000
set lines 132
set pages 200
select dbms_sqltune.report_tuning_task('TUNE1') from dual;

For this example, the SQL Tuning Advisor recommends creating a SQL profile. Here is
a snippet from the output
that contains the recommendation and the code required to create the SQL profile:
Recommendation (estimated benefit: 86.11%)
------------------------------------------
- Consider accepting the recommended SQL profile to use parallel execution for this
statement.
execute dbms_sqltune.accept_sql_profile(task_name => 'TUNE1', task_owner => 'SYS',
replace => TRUE, profile_type => DBMS_SQLTUNE.PX_PROFILE);
-------------------------------------------
Executing this query parallel with DOP 8 will improve its response time
86.11% over the original plan. However, there is some cost in enabling
parallel execution...

Step 4: Create and Accept the SQL Profile


To create the SQL profile, you need to run the code recommended by the SQL Tuning
Advisor (from step 3)—for
example:

begin
-- This is the code from the SQL Tuning Advisor
dbms_sqltune.accept_sql_profile(
task_name => 'TUNE1',
task_owner => 'SYS',
replace => TRUE,
profile_type => DBMS_SQLTUNE.PX_PROFILE);
--
end;
/

When the prior code is run, it creates and enables the SQL profile. Now whenever
the associated SQL query is
executed, the SQL profile will be considered by the optimizer when formulating an
execution plan.

If you need to later drop the tuning task, you can use the
DBMS_SQLTUNE.DROP_TUNING_TASK procedure. Here’s an
example of dropping the tuning task:

SQL> exec DBMS_SQLTUNE.DROP_TUNING_TASK(task_name=>'TUNE1');

2). Determining if a Query is Using a SQL Profile

You’ve created a SQL profile for a query and wonder if the SQL profile is being
used by the optimizer when the
query executes.

Query the SQL_PROFILE column of V$SQL to display any SQL queries currently in
memory that have used a SQL profile.
For example:

select sql_id, child_number, sql_profile


from v$sql
where sql_profile is not null;

To view historical high resource-consuming SQL statements that have used a SQL
profile, then query the
DBA_HIST_SQLSTAT view:

select sql_id, sql_profile


from dba_hist_sqlstat
where sql_profile is not null;
3). Automatically Accepting SQL Profiles

You realize that the Automatic SQL Tuning job runs on a daily basis (in Oracle
Database 11g or higher). You determine
that the automatic tuning job generates reasonable SQL profiles for problematic
queries and now want to enable the
automatic acceptance of SQL profiles generated by the automatic tuning job.

Use the DBMS_AUTO_SQLTUNE.SET_AUTO_TUNING_TASK_PARAMETER procedure to enable the


automatic acceptance of
SQL profiles recommended by the Automatic SQL Tuning task—for example:
BEGIN
DBMS_AUTO_SQLTUNE.SET_AUTO_TUNING_TASK_PARAMETER(
parameter => 'ACCEPT_SQL_PROFILES', value => 'TRUE');
END;
/
If you want to disable the automatic acceptance of SQL profiles, then do so as
follows (using the FALSE
parameter):
BEGIN
DBMS_AUTO_SQLTUNE.SET_AUTO_TUNING_TASK_PARAMETER(
parameter => 'ACCEPT_SQL_PROFILES', value => 'FALSE');
END;
/

You can report on the details of the automatic tuning task configuration via this
query

SELECT
parameter_name
,parameter_value
FROM dba_advisor_parameters
WHERE task_name = 'SYS_AUTO_SQL_TUNING_TASK'
AND parameter_name
IN ('ACCEPT_SQL_PROFILES',
'MAX_SQL_PROFILES_PER_EXEC',
'MAX_AUTO_SQL_PROFILES',
'EXECUTION_DAYS_TO_EXPIRE');

4). Displaying SQL Profile Information


You have created and accepted several SQL profiles and now want to view information
related to these database objects.

SQL> select name, type, status, sql_text from dba_sql_profiles;

5). Disabling a SQL Profile


First verify the name of the SQL profile that you want to disable:

SQL> select name, status from dba_sql_profiles;


NAME STATUS
------------------------------ --------
SYS_SQLPROF_013f199ac5990000 ENABLED

Now use the DBMS_SQLTUNE.ALTER_SQL_PROFILE procedure to modify the status of the


profile to DISABLED:

BEGIN
DBMS_SQLTUNE.ALTER_SQL_PROFILE(
name => 'SYS_SQLPROF_013f199ac5990000',
attribute_name => 'STATUS',
value => 'DISABLED');
END;
/

You can enable a disabled SQL profile as shown:

BEGIN
DBMS_SQLTUNE.ALTER_SQL_PROFILE(
name => 'SYS_SQLPROF_013f199ac5990000',
attribute_name => 'STATUS',
value => 'ENABLED');
END;
/

5). Dropping a SQL Profile


SQL> exec dbms_sqltune.drop_sql_profile('SYS_SQLPROF_012eda58a1be0001');

=======================THE
END================================================================================
====================

Вам также может понравиться