SQL Performance Analyzer (SQLPA), a new feature of SQL performance optimization in Oracle 11g

 

In Oracle11g, the Real Application Testing Option provides a useful feature called SQL Performance Analyzer (SQLPA, SPA). The system package DBMS_SQLPA allows you to register and compare SQL statement execution statistics in the SQL Tuning Set (STS). Through the SQL performance analyzer, you can compare the execution of SQL statements before and after database changes. The following will illustrate this new Oracle 11g feature with a simple example.

1. Create sample tables and data

In order to explain later, first, you need to create a table and add some data to the table, the script is as follows:

create table test

  (idnumber not null);

 

begin

for i in 1..10000 loop

insert into test(id)

  values (i);

end loop;

commit;

end;

/

2. Run the SQL statement and get the execution plan

Log in to SQL*Plus and run the SQL statement:

set serveroutput off

col id format 99999

select a.id, b.id

  from testa, test b

 wherea.id=b.id

   andb.id=500;

 

select * from table(

  dbms_xplan.display_cursor);

Its execution plan is as follows, remember its SQL_ID and will add it to the query statement in the SQL tuning set in the following section:

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

SQL_ID 683kdkrs2dmrk, child number 0

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

select a.id, b.id   from test a, test b  where a.id=b.id

and b.id=500

 

Plan hash value: 2625395012

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

| Id  |Operation          | Name | Rows  | Bytes | Cost (%CPU)|

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

|   0 |SELECT STATEMENT   |      |      |       |    15 (100)|

|*  1|  HASH JOIN         |     |     1 |    26 |   15   (7)|

|*  2|   TABLE ACCESS FULL| TEST |     1 |   13 |     7   (0)|

|*  3|   TABLE ACCESS FULL| TEST |     1 |   13 |     7   (0)|

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

 

Predicate Information (identified byoperation id):

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

1 -access("A"."ID"="B"."ID")

2 - filter("A"."ID"=500)

3 - filter("B"."ID"=500)

 

Note

-----

- dynamic sampling used for this statement

3. Capture SQL Tuning Set Query

Once the SQL statements are in the shared pool, you can create a new SQL tuning set and add SQL statements to it:

1) Create an STS:

BEGIN

DBMS_SQLTUNE.CREATE_SQLSET(

   sqlset_name => 'test_sts',

   description => 'STS for SPA demo');

END;

/

2) Add queries to the STS through the query cursor buffer:

DECLARE

 l_cursor DBMS_SQLTUNE.sqlset_cursor;

BEGIN

  OPENl_cursor FOR

     SELECT VALUE(p)

       FROM   TABLE (

          DBMS_SQLTUNE.select_cursor_cache(

            'sql_id = ''&sql_id''', -- basic_filter

            NULL, -- object_filter

            NULL, -- ranking_measure1

            NULL, -- ranking_measure2

            NULL, -- ranking_measure3

            NULL, -- result_percentage

            1)    -- result_limit

       ) p;

 DBMS_SQLTUNE.load_sqlset (

     sqlset_name    => 'test_sts',

     populate_cursor => l_cursor);

END;

/

accept sql_id prompt "Enter value forsql_id: "

683kdkrs2dmrk

PL/SQL procedure successfully completed.

3) The STS content can be queried to determine that the SQL has been properly registered:

col sql format a50

set lines 120

SELECT sql_id,

      substr(sql_text, 1, 50) sql

  FROMTABLE(

     DBMS_SQLTUNE.select_sqlset (

       'test_sts'));

SQL_ID       SQL

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

683kdkrs2dmrk select a.id, b.id

                from test a, test b

where a.i

3. Before changing the database, generate and store the SQL statement execution statistics

This step may take some time as STS queries are run and their execution statistics are stored. Perform the following steps in order:

1) Create a SQLPA analysis task that references STS

var v_out char (50)

begin

 :v_out:=dbms_sqlpa.create_analysis_task(

              sqlset_name => 'test_sts',

             task_name   => 'test_spa_task');

end;

/

print v_out

 

V_OUT

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

test_spa_task

2) Check if the task has been created:

col TASK_NAME format a14

col ADVISOR_NAME format a24

select TASK_NAME,

      ADVISOR_NAME,

      created

  fromDBA_ADVISOR_TASKS

 wheretask_name='test_spa_task';

 

TASK_NAME     ADVISOR_NAME             CREATED

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

test_spa_task SQL Performance Analyzer 15-AUG-07

3) Run the SQLPA analysis task:

begin

 DBMS_SQLPA.EXECUTE_ANALYSIS_TASK(

       task_name      => 'test_spa_task',

      execution_type => 'TEST EXECUTE',

      execution_name => 'test_spa_task_before');

end;

/

4) Monitor the task and its status until the task completes:

col TASK_NAME format a20

select execution_name,

      status,

       execution_end

  fromDBA_ADVISOR_EXECUTIONS

 wheretask_name='test_spa_task'

 orderby execution_end;

 

EXECUTION_NAME                 STATUS      EXECUTION

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

test_spa_task_before           COMPLETED   15-AUG-07

5) Change the database

Create an index on table TEST:

create unique index test_idx on test(id);

Index created.

6) After changing the database, run the SQLPA analysis task

The script is similar to the previous one. Just need to change the name to distinguish the execution statistics before and after the database change:

Ø Run the SQLPA analysis task:

begin

DBMS_SQLPA.EXECUTE_ANALYSIS_TASK(

      task_name      => 'test_spa_task',

      execution_type => 'TEST EXECUTE',

      execution_name => 'test_spa_task_after');

end;

/

Ø Monitor the task and its status until it completes:

col TASK_NAME format a20

select execution_name,

      status,

      execution_end

  fromDBA_ADVISOR_EXECUTIONS

 wheretask_name='test_spa_task'

 orderby execution_end;

 

EXECUTION_NAME                 STATUS      EXECUTION

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

test_spa_task_before           COMPLETED   15-AUG-07

test_spa_task_after            COMPLETED   15-AUG-07

7. Compare execution changes due to database changes

The analysis task needs to be run again, this time the analyzer will compare and store the comparison results:

begin

DBMS_SQLPA.EXECUTE_ANALYSIS_TASK(

   task_name        => 'test_spa_task',

  execution_type   => 'COMPAREPERFORMANCE',

  execution_name   => 'test_spa_task_compare',

  execution_params => dbms_advisor.arglist(

        'comparison_metric',

        'buffer_gets'));

end;

/

PL/SQL procedure successfully completed.

Once complete, the analysis report can be printed with REPORT_ANALISIS_TASK as follows:

variable rep CLOB;

begin

  :rep:= DBMS_SQLPA.REPORT_ANALYSIS_TASK(

             task_name=>'test_spa_task',

              type=>'HTML',

             level=>'ALL',

             section=>'ALL');

end;

/

 

SET LONG 100000

set LONGCHUNKSIZE 100000

set LINESIZE 200

set head off

set feedback off

set echo off

spool sts_changes.html

PRINT :rep

spool off

set head on

It is recommended that the SECTION parameter be set to SUMMARY or ALL, rather than set to SECTION_ALL as stated in the DBMS_SQLPA documentation.

The generated report results can be viewed via SQL*Plus (text output) or browser (HTML output):

 

8. Another way to display the comparison results:

In addition to printing reports, reports can also be queried from the following views:

Ø DBA_ADVISOR_FINDINGS

Ø DBA_ADVISOR_SQLPLANS

Ø DBA_ADVISOR_SQLSTATS

9. Clear execution statistics, analysis tasks and related tables

1) Reset the task result:

begin

  dbms_sqlpa.reset_analysis_task(task_name=>'test_spa_task');

end;

/

col TASK_NAME format a20

select execution_name,

      status,

      execution_end

  fromDBA_ADVISOR_EXECUTIONS

 wheretask_name='test_spa_task'

 orderby execution_end;

 

no rows selected

2) Clear the task itself:

begin

 dbms_sqlpa.drop_analysis_task(task_name=>'test_spa_task');

end;

/

 

col TASK_NAME format a14

col ADVISOR_NAME format a24

select TASK_NAME,

      ADVISOR_NAME,

      created

  fromDBA_ADVISOR_TASKS

 wheretask_name='test_spa_task';

 

no rows selected

3) In addition, you can:

Ø Cancel the running analysis task with the procedure cancel_analysis_task.

Ø Use the procedure interrupt_analysis_task to suspend the running analysis task.

Ø Use the procedure resume_analysis_task to resume the suspended analysis task.

Ø Clear related tables and indexes:

drop table gark cascade constraints purge;

10. Summary

Summarize SQLPA as follows:

Ø Can easily capture SQL statements in STS from AWR.

Ø No need to rebuild the test system (only the SELECT part of the INSERT/UPDATE/DELETE statement is executed).

Ø The SQL statement in STS is only a sample of real application load.

For details, please refer to:

Ø Oracle 11gPerformance Tuning Guide – 23.SQL Performance Analyzer

Ø Oracle 11g PL/SQLTypes and Packages Reference – DBMS_SQLPA

Oracle11g Reference

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325072886&siteId=291194637