oracle 03-18 管理性能:SQL调整

Managing Performance: SQL Tuning

Objectives
After completing this lesson, you should be able to:
• Manage optimizer statistics
• Use the SQL Tuning Advisor to:
– Identify SQL statements that are using the most resources
– Tune SQL statements that are using the most resources
• Use the SQL Access Advisor to tune a workload

目标

完成本课程后,您应该能够:

•管理优化器统计信息

•使用SQL优化顾问:

–识别使用最多资源的SQL语句

–调整使用最多资源的SQL语句

•使用SQL Access Advisor调整工作负载

SQL Tuning
SQL tuning process
• Identify poorly tuned SQL statements.
• Tune the individual statements.
• Tune the application as a whole.

SQL调整

SQL调整过程

•识别调整不当的SQL语句。

•调整个别陈述。

•整体优化应用程序

Oracle Optimizer: Overview
The Oracle optimizer determines the most efficient execution plan and is the most important step in the processing of any SQL statement.
The optimizer:
• Evaluates expressions and conditions
• Uses object and system statistics
• Decides how to access the data
• Decides how to join tables
• Determines the most efficient path

Oracle Optimizer:概述

Oracle优化器决定最有效的执行计划,是处理任何SQL语句中最重要的一步。

优化器:

•评估表达式和条件

•使用对象和系统统计

•决定如何访问数据

•决定如何连接表

•确定最有效的路径

Optimizer Statistics
Optimizer statistics are:
• A snapshot at a point in time
• Persistent across instance restarts
• Collected automatically

SQL> SELECT COUNT(*) FROM hr.employees;
COUNT(*)
----------
214
SQL> SELECT num_rows FROM dba_tables
2 WHERE owner='HR' AND table_name = 'EMPLOYEES';
NUM_ROWS
----------
107

优化器统计信息

优化器统计信息包括:

•一个时间点的快照

•跨实例持续重启

•自动收集

Optimizer Statistics Collection
• SQL performance tuning: Depends on collection of accurate statistics
• Optimizer statistics:
– Object statistics
– Operating system statistics
• Ways to collect statistics:
– Automatically: Automatic Maintenance Tasks
– Manually: DBMS_STATS package
– By setting database initialization parameters
– By importing statistics from another database

优化器统计信息收集

•SQL性能调整:取决于准确统计数据的收集

•优化器统计:

–对象统计

–操作系统统计

•收集统计数据的方法:

–自动:自动维护任务

–手动:DBMS_STATS包

–通过设置数据库初始化参数

–从另一个数据库导入统计数据

Using the Optimizer Statistics Console  使用优化器统计控制台

Setting Global Preferences by Using Enterprise Manager Cloud Control   使用Enterprise Manager云控制设置全局首选项

Gathering Optimizer Statistics Manually   手动收集优化器统计信息

Setting Optimizer Statistics Preferences
• Optimizer statistics preferences set the default values of parameters used by:
– Automatic statistics collection
– DBMS_STATS.GATHER_*_STATS procedures
• Set preferences at levels:
– Table, schema, database, or global
• Preferences: CASCADE, DEGREE, ESTIMATE_PERCENT,
NO_INVALIDATE, METHOD_OPT, GRANULARITY, INCREMENTAL, PUBLISH, STALE_PERCENT
• Use DBMS_STATS.SET | GET | DELETE | EXPORT | IMPORT_*_PREFS to manage preferences
EXEC DBMS_STATS.SET_TABLE_PREFS('SH','SALES','STALE_PERCENT','13');  表的修改率超过13%是收集表的最新统计信息
Optimizer statistics gathering task

设置优化器统计信息首选项

•Optimizer statistics preferences设置参数的默认值:

–自动统计数据收集

–数据库管理系统_统计数据收集_*_统计程序

•在级别设置首选项:

–表、架构、数据库或全局

•偏好:层级、学位、估计百分比,

无失效、方法选择、粒度、增量、发布、过期百分比

•使用数据库管理系统_状态集|获取|删除|导出|导入|首选项以管理首选项

执行数据库管理系统_STATS.SET_TABLE_首选项('SH'、'SALES'、'STALE'u PERCENT'、'13');

优化器统计信息收集任务

Concurrent Statistics Gathering
• The auto statistics gather job uses concurrency.
• Prevent concurrent statistics gathering to make the system overwhelmed through careful resource usage capping.
• Allow gathering index statistics concurrently.
• Allow gathering of statistics for multiple partitioned tables concurrently.

并发统计信息收集

•自动统计数据收集作业使用并发。

•通过谨慎的资源使用限制,防止并发统计数据收集,使系统不堪重负。

•允许同时收集索引统计信息。

•允许同时收集多个分区表的统计信息

Viewing Statistics Information   查看统计信息

SQL Plan Directives
• A SQL plan directive is additional information and instructions that the optimizer can use to generate a better plan:
– Collect missing statistics
– Create column group statistics
– Perform dynamic sampling
• Directives can be used for multiple statements:
– Directives are collected on query expressions
• They are persisted to disk in the SYSAUX tablespace.
• Directives are automatically maintained:
–Created as needed during compilation or execution:
–– Missing statistics, cardinality misestimates
– Purged if not used after a year

查看统计信息InformSQL计划指令

•SQL计划指令是附加信息和

优化器可以用来生成更好的

计划:

–收集缺失的统计数据

–创建列组统计信息

–执行动态采样

•指令可用于多个语句:

–对查询表达式收集指令

•它们被保存到SYSAUX表空间的磁盘上。

•指令自动维护:

–在编译或执行过程中根据需要创建:

-缺少统计数据,基数估计错误

–如果一年后不使用,则清除

Adaptive Execution Plans
• A query plan changes during execution because runtime conditions indicate that optimizer estimates are inaccurate.
• All adaptive execution plans rely on statistics that are collected during query execution.
• The two adaptive plan techniques are:
– Dynamic plans
– Re-optimization
• The database uses adaptive execution plans when OPTIMIZER_FEATURES_ENABLE is set to 12.1.0.1 or higher, and OPTIMIZER_ADAPTIVE_REPORTING_ONLY is set to the default value of FALSE.

适应性执行计划

•查询计划在执行期间发生变化,因为运行时条件表明优化器的估计不准确。

•所有自适应执行计划都依赖于查询执行期间收集的统计信息。

•两种适应性计划技术是:

–动态计划

–重新优化

•当OPTIMIZER_FEATURES_ENABLE设置为12.1.0.1或更高版本,并且OPTIMIZER_adaptive_REPORTING_ONLY设置为默认值FALSE时,数据库使用自适应执行计划。

Using the SQL Advisors  使用SQL顾问

spa 性能分析器

Automatic SQL Tuning Results  自动SQL调整结果

Implementing Automatic Tuning Recommendations  实施自动调整建议

SQL Tuning Advisor: Overview  SQL优化顾问:概述

Using the SQL Tuning Advisor
• Use the SQL Tuning Advisor to analyze SQL statements and obtain performance recommendations.
• Sources for SQL Tuning Advisor to analyze:
– Top Activity: Analyzes the top SQL statements currently active
– SQL Tuning Sets: Analyzes a set of SQL statements you provide
– Historical SQL (AWR): Analyzes SQL statements from statements collected by AWR snapshots

使用SQL优化顾问

•使用SQL优化顾问分析SQL语句并获得性能建议。

•供SQL优化顾问分析的源:

–Top活动:分析当前活动的顶级SQL语句

–SQL调整集:分析您提供的一组SQL语句

–历史SQL(AWR):从AWR快照收集的语句中分析SQL语句

SQL Tuning Advisor Recommendations  SQL优化顾问建议

通过手工命令调用STA

SQL> desc dbms_sqltune;  查看存储包参数

创建一张大表

SQL> select count(*) from dba_objects;

COUNT(*)
----------
73279

由于要在HR下建立个大表,要确保HR下的表空间足够大

 

QL> show user
USER is "SYS"
SQL> create table hr.test1 tablespace inventory as select * from dba_objects;

Table created.

Duplicate SQL  重复的SQL

SQL Access Advisor: Overview  

Using the SQL Access Advisor

Workload Source  工作量来源

Recommendation Options  推荐方案

Reviewing Recommendations

SQL Performance Analyzer: Overview
• Targeted users: DBAs, QAs, application developers
• Helps predict the impact of system changes on SQL workload response time
• Builds different versions of SQL workload performance (that is, SQL execution plans and execution statistics)
• Executes SQL serially (concurrency not honored)
• Analyzes performance differences
• Offers fine-grained performance analysis on individual SQL
• Is integrated with SQL Tuning Advisor to tune regressions

SQL性能分析器:概述

•目标用户、QADBA

•帮助预测系统更改对SQL工作负载响应时间的影响

•构建不同版本的SQL工作负载性能(即SQL执行计划和执行统计)

•串行执行SQL(不支持并发)

•分析绩效差异

•提供对单个SQL的细粒度性能分析

•与SQL优化顾问集成以优化回归

SQL Performance Analyzer: Use Cases
SQL Performance Analyzer is beneficial in the following use cases:
• Database upgrades
• Implementation of tuning recommendations
• Schema changes
• Statistics gathering
• Database parameter changes
• OS and hardware changes
It is accessible through Enterprise Manager and the DBMS_SQLPA package.

SQL性能分析器:用例

SQL Performance Analyzer在以下用例中非常有用:

•数据库升级

•调整建议的实施

•模式更改

•统计数据收集

•数据库参数更改

•操作系统和硬件更改

它可以通过Enterprise Manager和DBMS_SQLPA包访问

Using SQL Performance Analyzer
1. Capture SQL workload on production.
2. Transport the SQL workload to a test system.
3. Build “before-change” performance data.
4. Make changes.
5. Build “after-change” performance data.
6. Compare results from steps 3 and 5.
7. Tune regressed SQL.

使用SQL性能分析器

1捕获生产上的SQL工作负载。

2将SQL工作负载传输到测试系统。

3构建“更改前”性能数据。

4做出改变。

5构建“更改后”性能数据。

6比较步骤3和步骤5的结果。

7优化回归SQL

Quiz
Even when you enable Automatic Maintenance tasks, the SQL Tuning Advisor always has to be started separately.
a. True
b. False

Quiz
You can receive performance recommendations for historical SQL statements that are collected by AWR snapshots.
a. True
b. False

Quiz
The SQL Access Advisor can recommend the proper set of materialized views, materialized view logs, partitioning, and indexes for a given workload.
a. True
b. False

Quiz
The SQL Performance Analyzer provides you with detailed information about the performance of SQL statements, such as before-and-after execution statistics, and statements with performance improvement or degradation.
a. True
b. False

Summary
In this lesson, you should have learned how to:
• Manage optimizer statistics
• Use the SQL Tuning Advisor to:
– Identify SQL statements that are using the most resources
– Tune SQL statements that are using the most resources
• Use the SQL Access Advisor to tune a workload

摘要

在本课中,您应该学习如何:

•管理优化器统计信息

•使用SQL优化顾问:

–识别使用最多资源的SQL语句

–调整使用最多资源的SQL语句

•使用SQL Access Advisor调整工作负载

Practice: Overview
This practice covers using SQL Tuning Advisor.

实践:概述

这个实践包括使用SQL调优顾问

原文地址:https://www.cnblogs.com/cloud7777/p/13235235.html