`

Oracle 11g SQL Performance Analyzer(原创)

 
阅读更多

Overview The SQL Performance Analyzer
The SQL Performance Analyzer focuses on comparing the performance of a specific SQL workload before and after a major system change. The analyzer does this by building two versions of the SQL workload
performance, which includes both the SQL execution plans as well as their execution statistics. After analyzing SQL performance both before and after you make a major change, the SQL Performance Analyzer provides suggestions to prevent potential performance degradation of SQL statements. This is especially handy when you're planning an upgrade of your database to a newer release of the Oracle database. The
SQL Performance Analyzer, by enabling you to compare SQL performance on two systems running on different versions of the Oracle database, lets you know ahead of the upgrade which of the SQL statements may show a deterioration in performance. Thus, you can reengineer those statements prior to the actual upgrade.

Tips:The SQL Performance Analyzer executes SQL in a serial fashion and ignores concurrency.
You can use the SQL Performance Analyzer to predict performance changes resulting from the following system changes:

Database and application upgrades
Hardware upgrades
Operating system upgrades
Initialization parameter changes
SQL tuning actions such as the creation of SQL profiles
Statistics gathering
Schema changes

Using the DBMS_SQLPA package, you can build and compare two versions of workload performance ----one before the change and---- one after the change----and compare the differences between the two versions and easily trap the SQL statements that are adversely affected by the system change.
In addition to the new DBMS_SQLPA package, you can also use several procedures in the DBMS_SQLTUNE package to create the SQL Tuning Set that you need to capture the SQL workload and conduct a performance analysis.
You use a SQL Tuning Set (STS) to capture the SQL workload on the production system. The STS includes the SQL text, bind variables, as well as information relating to the execution environment, execution plans, and execution statistics of one or more SQL statements. You export the STS from the production system to the test system to provide the SQL workload input to the SQL Performance Analyzer.
Because the STS is a persistent database object, you can modify the STS as you want and even select data from the STS as you would from a table. You can use an STS's filtering capability to weed out any undesirable SQL.
You can use one of the following sources to load statements into an STS:

Automatic workload repository (AWR) snapshots
AWR baselines
A cursor cache
Another STS

You can either collect all the SQL statements at once or over a period of time. The SQL Performance Analyzer, which is primarily designed to predict the impact of major system changes on the SQL workload response time, does the following things when analyzing the SQL workload performance:

Builds different versions of SQL workload performance
Executes SQL serially without respecting concurrency characteristics
Analyzes performance differences including the response time of the before-and after-change SQL workloads
Uses the SQL Tuning Advisor to tune regressed SQL statements

Following is the workflow involved in using the SQL Performance Analyzer:
1. Capture the pre-change SQL workload performance. Use the SQL Performance Analyzer to capture the SQL workload in an STS that you create beforehand. You may also use the AWR instead to capture the top SQL statements in order to provide a complete SQL workload capture. You transport the SQL workload to a test system for analysis.
2. Analyze the pre-change SQL workload performance. The performance data includes execution plans and execution statistics for metrics such as elapsed time and disk reads.
3. Make the system changes. Once you capture the pre-change SQL workload from the production system, make the necessary changes on the test system. For example, if you want to test the impact of an upgrade to a new version of the database, install the new Oracle release software on the test system and upgrade a copy of the production database to the new release.
4. Capture the post-change SQL workload performance. Use the SQL Performance Analyzer to capture the workload, this time on the post-change test system.
5. Analyze the post-change SQL workload performance. Examine the execution plans and execution statistics for the same metrics you examined before you made the system change.
6. Compare and analyze the SQL performance. Use the SQL Performance Analyzer to compare the SQL performance in the production system and the post-change test system in order to identify changes in SQL execution plans. You can also compare statistics relating to user-specified metrics such as execution time, buffer gets, disk reads, and others. You can then calculate the impact of the change on both individual SQL statements and on the SQL workload as a whole. This enables you to foresee whether the change would lead to an improvement or regression in SQL performance or whether it would have no net impact on the SQL workload.

Tips:The SQL workload you must capture from the production system must be from a representative peak period.

Capturing the SQL Workload
Before running SQL Performance Analyzer, capture a set of SQL statements on the production system that represents the SQL workload which you intend to analyze.
The captured SQL statements should include the following information:

  • SQL text
  • Execution environment
  • SQL binds, which are bind values needed to execute a SQL statement and generate accurate execution statistics
  • Parsing schema under which a SQL statement can be compiled
  • Compilation environment, including initialization parameters under which a SQL statement is executed
  • Number of times a SQL statement was executed

Capturing a SQL workload has a negligible performance impact on your production system and should not affect throughput. A SQL workload that contains more SQL statements will better represent the state of the application or database. This will enable SQL Performance Analyzer to more accurately forecast the potential impact of system changes on the SQL workload. Therefore, you should capture as many SQL statements as possible. Ideally, you should capture all SQL statements that are either called by the application or are running on the database.
You can store captured SQL statements in a SQL tuning set and use it as an input source for SQL Performance Analyzer. A SQL tuning set is a database object that includes one or more SQL statements, along with their execution statistics and execution context.  Capturing a SQL workload using a SQL tuning set enables you to:

  • Store the SQL text and any necessary auxiliary information in a single, persistent database object
  • Populate, update, delete, and select captured SQL statements in the SQL tuning set
  • Load and merge content from various data sources, such as the Automatic Workload Repository (AWR) or the cursor cache
  • Export the SQL tuning set from the system where the SQL workload is captured and import it into another system
  • Reuse the SQL workload as an input source for other advisors, such as the SQL Tuning Advisor and the SQL Access Advisor
Setting Up the Test

The SQL performance analyzer requires SQL tuning sets, and SQL tuning sets are pointless unless they contain SQL, so the first task should be to issue some SQL statements. We are only trying to demonstrate the technology, so the example can be really simple. The following code creates a test user called SPA_TEST_USER.
CONN / AS SYSDBA
CREATE USER spa_test_user IDENTIFIED BY spa_test_user
  QUOTA UNLIMITED ON users;
GRANT CONNECT, CREATE TABLE TO spa_test_user;
Next, connect to the test user and create a test table called MY_OBJECTS using a query from the ALL_OBJECTS view.
CONN spa_test_user/spa_test_user
CREATE TABLE my_objects AS
      SELECT * FROM all_objects;
EXEC DBMS_STATS.gather_table_stats(USER, 'MY_OBJECTS', cascade => TRUE);
This schema represents our "before" state. Still logged in as the test user, issue the following statements.
SELECT COUNT(*) FROM my_objects WHERE object_id <= 100;
SELECT object_name FROM my_objects WHERE object_id = 100;
SELECT COUNT(*) FROM my_objects WHERE object_id <= 1000;
SELECT object_name FROM my_objects WHERE object_id = 1000;
SELECT COUNT(*) FROM my_objects WHERE object_id BETWEEN 100 AND 1000;
Notice, all statements make reference to the currently unindexed OBJECT_ID column. Later we will be indexing this column to create our changed "after" state.
The select statements are now in the shared pool, so we can start creating an SQL tuning set.
Creating SQL Tuning Sets using the DBMS_SQLTUNE Package
The DBMS_SQLTUNE package contains procedures and functions that allow us to create, manipulate and drop SQL tuning sets. The first step is to create an SQL tuning set called spa_test_sqlset using the CREATE_SQLSET procedure.
CONN / AS SYSDBA
EXEC DBMS_SQLTUNE.create_sqlset(sqlset_name => 'spa_test_sqlset');
Next, the SELECT_CURSOR_CACHE table function is used to retrieve a cursor containing all SQL statements that were parsed by the SPA_TEST_USER schema and contain the word "my_objects". The resulting cursor is loaded into the tuning set using the LOAD_SQLSET procedure.
DECLARE
  l_cursor  DBMS_SQLTUNE.sqlset_cursor;
BEGIN
  OPEN l_cursor FOR
     SELECT VALUE(a)
     FROM   TABLE(
              DBMS_SQLTUNE.select_cursor_cache(
                basic_filter   => 'sql_text LIKE ''%my_objects%'' and parsing_schema_name = ''SPA_TEST_USER''',
                attribute_list => 'ALL')
            ) a;
  DBMS_SQLTUNE.load_sqlset(sqlset_name     => 'spa_test_sqlset',
                           populate_cursor => l_cursor);
END;
/

The database uses an incremental capture method to populate the STS from the cursor cache over a period of time. During the populating of the STS, the database filters out any undesirable SQL.
If you need to export the captured SQL workload in the form of the STS to the test system so you can invoke the SQL Performance Analyzer there.You need to use following instruduction,if you don't need it,just ignore  this phase.
Transport the SQL Tuning Set 

Before you can transport the SQL tuning set, you must first create a staging table using the CREATE_STGTAB_SQLSET procedure, so you can use this table to export the STS that contains the production SQL workload to the test system. After you export the STS, you must import it into the test database.
SQL> exec dbms_sqltune.create_stgtab_sqlset (table_name => 'stagetab');

Tips:we can't create staging table in SYS schema.
The CREATE_SQLTAB_SQLSET procedure creates a staging table named STAGETAB. Export the production STS into the staging table STAGETAB that you just created, using the PACK_STGTAB_SQLSET procedure:
SQL> exec dbms_sqltune.pack_stgtab_sqlset (sqlset_name =>
     'test_sts',
     staging_table_name => 'stagetab');
Now you must expdp STAGETAB from the production system to the test system. Once you import the staging table by impdp, run the UNPACK_STGTAB_SQLSET procedure to import the STS into the test database where you'll replay the SQL workload.
SQL> exec dbms_sqltune.unpack_stgtab_sqlset (sqlset_name = '%',replace => true, staging_table_name => ('stagetab');
You're now ready to create your SQL Performance Analyzer task.

The DBA_SQLSET_STATEMENTS view allows us to see which statements have been associated with the tuning set.
SELECT sql_text
FROM   dba_sqlset_statements
WHERE  sqlset_name = 'spa_test_sqlset';
SQL_TEXT
--------------------------------------------------------------------------------
SELECT object_name FROM my_objects WHERE object_id = 100
SELECT COUNT(*) FROM my_objects WHERE object_id <= 100
SELECT COUNT(*) FROM my_objects WHERE object_id BETWEEN 100 AND 1000
SELECT COUNT(*) FROM my_objects WHERE object_id <= 1000
SELECT object_name FROM my_objects WHERE object_id = 1000
5 rows selected.
Now we have an SQL tuning set, we can start using the SQL performance analyzer.The DBMS_SQLPA package is the PL/SQL API used to manage the SQL performance ananlyzer. The first step is to create an analysis task using the CREATE_ANALYSIS_TASK function, passing in the SQL tuning set name and making a note of the resulting task name.
CONN / AS SYSDBA
VARIABLE v_task VARCHAR2(64);
EXEC :v_task :=  DBMS_SQLPA.create_analysis_task(sqlset_name => 'spa_test_sqlset');
PL/SQL procedure successfully completed.
SQL> PRINT :v_task
V_TASK
--------------------------------------------------------------------------------
TASK_122
Next, use the EXECUTE_ANALYSIS_TASK procedure to execute the contents of the SQL tuning set against the current state of the database to gather information about the performance before any modifications are made. This analysis run is named before_change.
BEGIN
  DBMS_SQLPA.execute_analysis_task(
    task_name       => :v_task,
    execution_type  => 'test execute',
    execution_name  => 'before_change');
END;
/

The execution_type parameter of the EXECUTE_ANALYSIS_TASK procedure can take one of the following three values:
TEST_EXECUTE:Executes all SQL statements in the captured SQL workload. The database only executes the query portion of the DML statements, in order to avoid adversely impacting user data or the database itself. The database generates both execution plans and execution statistics (for example, disk reads and buffer gets).
COMPARE_PERFORMANCE:Compares performance between two executions of the workload performance analysis.
EXPLAIN PLAN:Lets you generate SQL plans only, without actually executing them.

The EXECUTE_ANALYSIS_TASK procedure executes all DML statements but ignores any DDL statements to avoid unduly affecting the test data.
Now we have the "before" performance information, we need to make a change so we can test the "after" performance. For this example we will simply add an index to the test table on the OBJECT_ID column. In a new SQL*Plus session create the index using the following statements.
CONN spa_test_user/spa_test_user
CREATE INDEX my_objects_index_01 ON my_objects(object_id);
EXEC DBMS_STATS.gather_table_stats(USER, 'MY_OBJECTS', cascade => TRUE);
Now, we can return to our original session and test the performance after the database change. Once again use the EXECUTE_ANALYSIS_TASK procedure, naming the analysis task "after_change".
BEGIN
  DBMS_SQLPA.execute_analysis_task(
    task_name       => :v_task,
    execution_type  => 'test execute',
    execution_name  => 'after_change');
END;
/
Once the before and after analysis tasks are complete, we must run a comparison analysis task. The following code explicitly names the analysis tasks to compare using name-value pairs in the EXECUTION_PARAMS parameter. If this is ommited, the latest two analysis runs are compared.
BEGIN
  DBMS_SQLPA.execute_analysis_task(
    task_name        => :v_task,
    execution_type   => 'compare performance',
    execution_params => dbms_advisor.arglist(
                          'execution_name1',
                          'before_change',
                          'execution_name2',
                          'after_change')
    );
END;
/
With this final analysis run complete, we can check out the comparison report using the REPORT_ANALYSIS_TASK function. The function returns a CLOB containing the report in 'TEXT', 'XML' or 'HTML' format. Its usage is shown below.
Note. Oracle 11gR2 also includes an 'ACTIVE' format that looks more like the Enterprise Manager output.
SET PAGESIZE 0
SET LINESIZE 1000
SET LONG 1000000
SET LONGCHUNKSIZE 1000000
SET TRIMSPOOL ON
SET TRIM ON
SPOOL /tmp/execute_comparison_report.htm
SELECT DBMS_SQLPA.report_analysis_task(:v_task, 'HTML', 'ALL')
FROM   dual;
SPOOL OFF
An example of this file for each available type is shown below.
TEXT
HTML
XML
ACTIVE - Active HTML available in 11gR2 requires a download of Javascript libraries from an Oracle website, so must be used on a PC connected to the internet.
You can do the following during the compare and analysis phase:

Calculate the impact of the change on specific SQL statements.
Calculate the impact of the change on the SQL workload as a whole.
Assign weights to important SQL in the workload.
Detect performance regression and improvements.
Detect changes in the execution plans of the SQL statements.

Recommend the running of the SQL Tuning Advisor to tune regressed SQL statements. After using the advisor, you can create a new after-change version of the SQL workload performance to ensure that you have acceptable performance.

You can use the following views when working with the SQL Performance Analyzer:
DBA_ADVISOR_TASKS shows details about the analysis task.
DBA_ADVISOR_FINDINGS shows analysis findings, which are classified as performance regression, symptoms, informative messages, and errors.
DBA_ADVISOR_EXECUTIONS shows metadata information for task executions.
DBA_ADVISOR_SQLPLANS shows a list of SQL execution plans.
DBA_ADVISOR_SQLSTATS shows a list of SQL compilation and execution statistics.

 

参考至:《McGraw.Hill.OCP.Oracle.Database.11g.New.Features.for.Administrators.Exam.Guide.Apr.2008》

                     http://www.oracle-base.com/articles/11g/sql-performance-analyzer-11gr1.php
                     http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sqltun.htm#CHEFGHHI
                     http://docs.oracle.com/cd/E11882_01/server.112/e41481/spa_intro.htm#RATUG168

本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:czmcj@163.com

0
1
分享到:
评论

相关推荐

    oracle11g新特性--SQL Performance Analyzer

    ### Oracle 11g 新特性详解:SQL Performance Analyzer (SPA) #### 一、概述 随着技术的不断进步,Oracle 数据库也在不断地推出新的版本来满足企业和开发者的需求。Oracle 11g 是 Oracle 公司发布的一款重要版本,...

    使用 SQL Performance Analyzer 进行数据库性能高级分析

    本专题讲座将介绍如何使用量度扩展 (Metric Extension) 来诊断...量度扩展是 Oracle Enterprise Manager Cloud Control 12c 的一个新特性,具有丰富的功能,如多关键量度、设计和部署生命周期以及稳定可靠的用户界面。

    Oracle 11g

    10. **开发工具集成**:Oracle 11g与多种开发工具如JDBC、ODBC、PL/SQL等紧密集成,方便开发者进行应用程序的开发和维护。 在“Oracle 11g数据库基础教程_源代码”中,你将找到与这些概念相关的实践案例,涵盖...

    oracle11g性能诊断

    SQL Performance Analyzer是Oracle 11g中用于测试和分析SQL语句性能的工具。它可以预测SQL在不同数据库环境下的表现,比如升级到新版本或改变表分区策略后,从而帮助优化SQL执行计划。 五、Memory Management ...

    ORACLE11g研究新特性

    Oracle 11g是Oracle数据库的一个重要版本,它在前一版本的基础上引入了许多创新和增强功能,以提高性能、安全性和管理效率。本资源详细探讨了Oracle 11g的关键新特性,对于深入理解这一数据库管理系统具有极大的价值...

    Oracle11g.pdf知识下载

    - **SQL 语句的自动调优**:Oracle 11g 能够自动调整 SQL 语句的执行计划,以优化性能。 - **SQL Plan 的管理**:允许用户管理 SQL 执行计划的变更,确保只有那些经过验证且性能更佳的计划才会被采用。 - **简易的...

    Oracle Performance Analyzer Reference Guide Release 4.5

    1. **性能监控与分析**:Oracle Performance Analyzer能够实时监控数据库的资源使用情况,如CPU、内存、I/O等,通过收集和分析SQL语句的执行计划、等待事件和系统资源消耗,帮助用户找出性能瓶颈。 2. **SQL性能...

    Oracle 11g 王二暖ppt文件

    Oracle 11g是甲骨文公司发布的一款企业级数据库管理系统,它在11g版本中引入了许多新特性和优化,旨在提升性能、可用性、安全性以及管理效率。"王二暖ppt文件"可能是一位名叫王二暖的专家或讲师分享的关于Oracle 11g...

    Oracle11g数据库

    Oracle11g对PL/SQL语言进行了增强,包括新的异常处理机制、调试工具和优化的编译器。 10. **Grid Infrastructure** Oracle Grid Infrastructure是11g的一部分,它包括管理和监控所有Oracle RAC组件的工具,以及...

    精通 Oracle 11g数据库管理

    还介绍了 Oracle 的自动性能监控工具,如 Automatic Workload Repository (AWR) 和 SQL Performance Analyzer,以帮助管理员识别和解决性能问题。 数据库的高可用性是 Oracle 11g 的一大亮点。书中详细讲解了 Real ...

    Oracle单实例版本升级(11g To 12c).docx

    Oracle 单实例版本升级(11g 到 12c) 在本篇文章中,我们将详细介绍 Oracle 单实例版本升级从 11g 到 12c 的过程。这个过程包括数据库升级准备工作、升级步骤和测试计划等几个方面。 数据库升级准备工作 在开始...

    ORACLE 11G 202补丁包

    Oracle 11g是Oracle公司推出的数据库管理系统的一个主要版本,主要服务于企业级的数据存储和管理需求。这个"ORACLE 11G 202补丁包"是针对Oracle 11g数据库系统的一个更新,旨在提升系统的稳定性和性能,修复已知的...

    Oracle11g完全学习手册ppt

    Oracle 11g是Oracle公司推出的数据库管理系统的一个重要版本,具有高效、稳定和安全的特点,广泛应用于企业级数据管理。本“Oracle11g完全学习手册ppt”旨在为初学者和有经验的DBA提供全面的学习资源,帮助理解并...

    oracle 11g 2.0.3

    Oracle 11g 2.0.3 是Oracle公司推出的一款关系型数据库管理系统的重要版本,主要专注于提供高性能、高可用性和高级别的数据管理能力。在这个版本中,Oracle引入了诸多新特性和改进,以增强其在企业级应用中的表现。 ...

    oracle11g数据库正版资源

    Oracle 11g数据库是Oracle公司推出的一款关系型数据库管理系统,是企业级数据管理的重要工具。这个版本在11g R1(11.1.0.x.x)的基础上进行了优化和增强,推出了11g R2(11.2.0.x.x),以提供更高效的数据处理能力、...

    oracle11g ocp 升级 1z0-050考试模拟器 & 原题PDF

    Oracle 11g OCP(Oracle Certified Professional)是Oracle数据库管理员的专业认证,1z0-050是Oracle Database 11g Administrator Certified Professional Upgrade Exam的考试代码,用于验证专业人士在Oracle 11g...

    oracle 11g new feature

    综上所述,Oracle 11g通过引入一系列新特性,如安装与升级增强、SQL性能分析器、SQL计划管理、数据库重放、自动SQL调优等,极大地提升了数据库的管理性、性能和可用性。这些功能的加入,标志着Oracle数据库在企业级...

    oracle11g最新OCP题库

    Oracle 11g是Oracle公司推出的数据库管理系统的一个重要版本,其OCP(Oracle Certified Professional)认证是针对数据库管理员的专业认证,旨在验证个人在Oracle 11g环境中的技能和知识。这个"oracle11g最新OCP题库...

Global site tag (gtag.js) - Google Analytics