您的位置:首页 > 数据库

sql调优之--寻找我们最感兴趣的语句

2010-05-11 11:02 162 查看
Tuning an Application / Reducing Load
If your whole application is performing suboptimally, or if you are attempting to
reduce the overall CPU or I/O load on the database server, then identifying
resource-intensive SQL involves the following steps:
1. Determine which period in the day you would like to examine; typically this is the
application's peak processing time.
2. Gather operating system and Oracle statistics at the beginning and end of that
period. The minimum of Oracle statistics gathered should be file I/O
(V$FILESTAT), system statistics (V$SYSSTAT), and SQL statistics (V$SQLAREA,
V$SQL or V$SQLSTATS, V$SQLTEXT, V$SQL_PLAN, and V$SQL_PLAN_
STATISTICS).
3. Using the data collected in step two, identify the SQL statements using the most
resources. A good way to identify candidate SQL statements is to query
V$SQLSTATS. V$SQLSTATS contains resource usage information for all SQL
statements in the shared pool. The data in V$SQLSTATS should be ordered by
resource usage. The most common resources are:
■ Buffer gets (V$SQLSTATS.BUFFER_GETS, for high CPU using statements)
■ Disk reads (V$SQLSTATS.DISK_READS, for high I/O statements)
■ Sorts (V$SQLSTATS.SORTS, for many sorts)
One method to identify which SQL statements are creating the highest load is to
compare the resources used by a SQL statement to the total amount of that resource
used in the period. For BUFFER_GETS, divide each SQL statement's BUFFER_GETS by
the total number of buffer gets during the period. The total number of buffer gets in
the system is available in the V$SYSSTAT table, for the statistic session logical reads.
Similarly, it is possible to apportion the percentage of disk reads a statement performs
out of the total disk reads performed by the system by dividing V$SQL_STATS.DISK_
READS by the value for the V$SYSSTAT statistic physical reads. The SQL sections of
the Automatic Workload Repository report include this data, so you do not need to
perform the percentage calculations manually
After you have identified the candidate SQL statements, the next stage is to gather
information that is necessary to examine the statements and tune them.

调试一个应用/减少负载
如果整个应用表现的是最优化的状态,或者如果你在尝试减少数据库服务器的CPU或者IO负载,那么你可以用如下的方式来识别
消耗资源的sql语句:
1、确定在一天的哪个时间段来抽样数据,一般选择在应用最繁忙的时间段。
2、在那天的初始和结尾抓取操作系统的、数据库的信息。我们至少要包含这些信息,
系统的IO信息(v$filestat),系统信息(v$sysstat),sql语句的信息(v$sqlarea,v$sql或者v$sqlstats,v$sqltext,v$sqlstat,
v$sql_plan以及v$sql_plan_statistics)
3、使用第二部收集到的信息,定位最耗资源的sql语句。一个比较好的方法来识别待甄选的SQL语句的方法是查询
v$sqlstats. v$sqlstats 包含了在共享池中所有sql语句的资源使用信息。在V$SQLSTATS中的信息应该根据资源的使用
来进行排序,而最常见的资源包括:
Buffer gets(V$SQLSTATS.BUFFER_GETS,针对高CPU利用率的语句)
Disk reads(V$SQLSTATS.DISK_READS,针对高I/O的语句)
Sorts(V$SQLSTATS.SORTS,针对sort 操作多的语句)
识别产生最高负载的sql语句的方法是,让某个sql语句消耗的资源和所有的资源进行比较。
针对buffer_gets,让每个sql语句消耗的buffer_gets除以这段时间消耗的buffer_gets的总量。

系统中所有的buffer gets信息存在v$sysstat这个表中,针对的是静态的逻辑读。
相似的,我们可以用某个语句导致的disk读(v$sql_stats.disk_reads)除以这段时间的系统disk读(v$sysstat)。
AWR 报告的 SQL部分将包含这些信息,所以你不需要手工来计算。
当你获得了待选语句之后,你可以进一步抓取信息并调试这些语句。

Gathering Data on the SQL Identified
If you are most concerned with CPU, then examine the top SQL statements that
performed the most BUFFER_GETS during that interval. Otherwise, start with the SQL
statement that performed the most DISK_READS.
Information to Gather During Tuning
The tuning process begins by determining the structure of the underlying tables and
indexes. The information gathered includes the following:
1. Complete SQL text from V$SQLTEXT
2. Structure of the tables referenced in the SQL statement, usually by describing the
table in SQL*Plus
3. Definitions of any indexes (columns, column orderings), and whether the indexes
are unique or non-unique
4. Optimizer statistics for the segments (including the number of rows each table,
selectivity of the index columns), including the date when the segments were last
analyzed
5. Definitions of any views referred to in the SQL statement
6. Repeat steps two, three, and four for any tables referenced in the view definitions
found in step five
7. Optimizer plan for the SQL statement (either from EXPLAIN PLAN, V$SQL_PLAN,
or the TKPROF output)
8. Any previous optimizer plans for that SQL statement

进一步抓取信息
如果你最关心的是CPU,那么检查这段时间里最消耗BUFFER_GETS的语句。
或者,选择导致最多磁盘读操作的语句。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: