轉自:http://blog.csdn.net/cuker919/article/details/8767328
最近由於數據庫cpu占用非常高,導致VCS常常自動切換,引起很多問題。
最近學習一下數據庫awr分析數據庫sql執行性能的分析報告。下面將初步講解一下:
1、先登陸數據庫,生成awr報告。
linux:~ # su - oracle
oracle@linux:~> sqlplus '/as sysdba'
SQL*Plus: Release 11.1.0.6.0 - Production on Sun Apr 7 14:02:38 2013
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
2、輸入分析命令
SQL> @?/rdbms/admin/awrrpt
Current Instance
~~~~~~~~~~~~~~~~
DB Id DB Name Inst Num Instance
----------- ------------ -------- ------------
2045388596 UTF8 1 utf8
Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~~
Would you like an HTML report, or a plain text report?
Enter 'html' for an HTML report, or 'text' for plain text
Defaults to 'html'
Enter value for report_type: html
3、輸入要生成報告的文件格式
Type Specified: html
Instances in this Workload Repository schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DB Id Inst Num DB Name Instance Host
------------ -------- ------------ ------------ ------------
* 2045388596 1 UTF8 utf8 linux
Using 2045388596 for database Id
Using 1 for instance number
Specify the number of days of snapshots to choose from
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed. Pressing <return> without
specifying a number lists all completed snapshots.
4、輸入要生成報告相隔的天數
Enter value for num_days: 1
Listing the last day's Completed Snapshots
Snap
Instance DB Name Snap Id Snap Started Level
------------ ------------ --------- ------------------ -----
utf8 UTF8 2809 26 Oct 2014 00:00 1
2810 26 Oct 2014 01:00 1
2811 26 Oct 2014 02:00 1
2812 26 Oct 2014 03:00 1
2813 26 Oct 2014 04:00 1
2814 26 Oct 2014 05:00 1
2815 26 Oct 2014 06:00 1
2816 26 Oct 2014 07:00 1
2817 26 Oct 2014 08:00 1
2818 26 Oct 2014 09:00 1
2819 26 Oct 2014 10:00 1
2820 26 Oct 2014 11:00 1
2821 26 Oct 2014 12:00 1
5、輸入相隔的快照之間的Snap Id開始號和結束號
Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 2809
Enter value for end_snap: 2821
End Snapshot Id specified: 2821
Specify the Report Name
~~~~~~~~~~~~~~~~~~~~~~~
The default report file name is awrrpt_1_2809_2821.html. To use this name,
press <return> to continue, otherwise enter an alternative.
6、輸入生成報告的名字:
Enter value for report_name: 20130407awr.html
拿到20130407awr.html報告后進行數據庫sql語句性能分析。下面是awr概念和主要分析名稱解釋(網上copy的資料,不一定全部對):
在網上找到幾篇不錯的文章:
對於awr參數的解釋,比較全的文章
http://wenku.baidu.com/view/ae2ddbc29ec3d5bbfd0a7429.html
http://468302.blog.51cto.com/458302/998786 AWR中的時間模型(db time與cpu time 具體講解)
http://www.oracledatabase12g.com/ oracle專家劉相兵博客
http://www.askmaclean.com/archives/maclean-little-contribute.html 劉相兵相關視頻和文章
http://www.5ienet.com/note/html/stat/index.shtml
http://blog.itpub.net/26686207/ 大牛博客:劉盛 網名leonarding
學用ORACLE_AWR和ASH特性
http://www.5ienet.com/note/html/ash_awr/index.shtml
-------------------------------------------------
Automatic Workload Repository(AWR)是10g引入的一個重要組件。 在里面存貯着近期一段時間內(默認是7天)數據庫活動狀態的詳細信息。
1、生成awr報告
以oracle用戶登錄
sqlplus / as sysdba
@?/rdbms/admin/awrrpt.sql
2、分析報告
SQL ordered by Elapsed Time
Resources reported for PL/SQL code includes the resources used by all SQL statements called by the code.
% Total DB Time is the Elapsed Time of the SQL statement divided into the Total Database Time multiplied by 100
Elapsed Time (s)CPU Time (s)Executions Elap per Exec (s) % Total DB TimeSQL IdSQL ModuleSQL Text
Elapsed Time(S) : SQL 語句執行用總時長,此排序就是按照這個字段進行的。注意該時間不是單個SQL 跑的時間,而是監控范圍內SQL 執行次數的總和時間。單位時間為秒。Elapsed Time = CPU Time + Wait Time
CPU Time(s) : 為SQL 語句執行時CPU 占用時間總時長,此時間會小於等於Elapsed Time 時間。單位時間為秒。
Executions : SQL 語句在監控范圍內的執行次數總計。
Elap per Exec(s): 執行一次SQL 的平均時間。單位時間為秒。
% Total DB Time : 為SQL 的Elapsed Time 時間占數據庫總時間的百分比。
SQL ID: SQL 語句的ID 編號,點擊之后就能導航到下邊的SQL 詳細列表中,點擊IE 的返回可以回到當前SQL ID 的地方。
SQL Module : 顯示該SQL 是用什么方式連接到數據庫執行的,如果是用SQL*Plus 或者PL/SQL 鏈接上來的那基本上都是有人在調試程序。一般用前台應用鏈接過來執行的sql 該位置為空。
SQL Text: 簡單的sql 提示,詳細的需要點擊SQL ID .
SQL ordered by CPU Time
Resources reported for PL/SQL code includes the resources used by all SQL statements called by the code.
% Total DB Time is the Elapsed Time of the SQL statement divided into the Total Database Time multiplied by 100
CPU Time (s)Elapsed Time (s)Executions CPU per Exec (s)% Total DB TimeSQL IdSQL ModuleSQL Text
記錄了執行占CPU 時間總和時間最長的TOP SQL( 注意是監控范圍內該SQL 的執行占CPU 時間總和,而不是單次SQL 執行時間) .
SQL ordered by Gets
Resources reported for PL/SQL code includes the resources used by all SQL statements called by the code.
Total Buffer Gets: 964,486
Captured SQL account for 103.6% of Total
Buffer Gets Executions Gets per Exec %TotalCPU Time (s)Elapsed Time (s)SQL IdSQL ModuleSQL Text
記錄了執行占總buffer gets( 邏輯IO ) 的TOP SQL( 注意是監控范圍內該SQL 的執行占Gets 總和,而不是單次SQL 執行所占的Gets)。
SQL ordered by Reads
Total Disk Reads: 5,606
Captured SQL account for 168.4% of Total
Physical ReadsExecutionsReads per Exec %TotalCPU Time (s)Elapsed Time (s)SQL IdSQL ModuleSQL Text[nextpage]
記錄了執行占總磁盤物理讀( 物理IO ) 的TOP SQL( 請注意是監控范圍內該SQL 的執行占磁盤物理讀總和,而不是單次SQL 執行所占的磁盤物理讀) .
SQL ordered by Executions
Total Executions: 20,124
Captured SQL account for 59.3% of Total
Executions Rows ProcessedRows per ExecCPU per Exec (s)Elap per Exec (s) SQL IdSQL ModuleSQL Text
記錄了按照SQL 的執行次數 排序的TOP SQL .該排序可以看出監控范圍內的SQL 執行次數。
SQL ordered by Parse Calls
Total Parse Calls: 14,635
Captured SQL account for 69.0% of Total
Parse CallsExecutions % Total ParsesSQL IdSQL ModuleSQL Text
記錄了SQL 的軟解析次數 的TOP SQL .
SQL ordered by Sharable Memory
Only Statements with Sharable Memory greater than 1048576 are displayed
Sharable Mem (b)Executions % TotalSQL IdSQL ModuleSQL Text
.記錄了SQL 占用library cache 的大小 的TOP SQL .
Sharable Mem (b) : 占用 library cache 的大小。單位是byte .
SQL ordered by Version Count
Only Statements with Version Count greater than 20 are displayed
Version Count Executions SQL IdSQL ModuleSQL Text
記錄了SQL 的打開子游標 的TOP SQL .
-----------------------------------------------------
手工修改awr的執行計划,同時手工生成報告
SQL> @?/rdbms/admin/awrrpt.sql (可以去兩個快照之間的awr報告)
注:AWR默認保留數據庫7天的快照,每個小時產生一個快照
一、手工修改awr的執行計划
調整AWR產生snapshot的頻率和保留策略,如將收集間隔時間改為30分鍾一次。並且保留5天時間(單位都是分鍾):
SQL> exec dbms_workload_repository.modify_snapshot_settings(interval=>30, retention=>5*24*60); |
二、測試系統:
1、手工先在數據庫中創建一個快照
SQL> execute dbms_workload_repository.create_snapshot(); PL/SQL procedure successfully completed. |
2、打開生產系統,點擊“單井匯總信息”20多秒才出來頁面
3、再生產一個快照
SQL> execute dbms_workload_repository.create_snapshot(); PL/SQL procedure successfully completed. |
4、可以根據上面的awr報告中,找出花費時間比較長的單條sql語句的執行計划
如:sql id為c0yffdyps8uk9 花費了26秒時間
SQL> @?/rdbms/admin/awrsqrpt.sql
Specify the Begin and End Snapshot Ids
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter value for begin_snap: 1679
Begin Snapshot Id specified: 1679
Enter value for end_snap: 1680
End Snapshot Id specified: 1680
Specify the SQL Id
~~~~~~~~~~~~~~~~~~
Enter value for sql_id: (輸入c0yffdyps8uk9)
------------------------------------------------------
1.AWR介紹
AWR(Automatic Workload Repository)是oracle 10g提供的一個內置工具,它采集與DB性能相關統計數據,以WRM$_*和WRH_*的格式命名表,所有表都存儲到SYSAUX表空間中的SYS模式下;AWR報告是DBA進行數據庫性能評定,發現問題SQL的重要手段.
AWR默認保存一周的數據統計信息,產生快照頻率和保留時間都可以手動調整,它完全可以取代statspack.
2.AWR報告生成步驟
2.1利用工具Toad連接到AWR報告生成界面
Database->monitor->ADDM/AWRReports(OEM)

2.2選擇快照生成的時間段點擊綠色按鈕執行生成AWR報告

2.3通過數據庫腳本文件生成AWR報告,執行如下腳本
$ORACLE_HOME\RDBMS\admin\awrrpt.sql
3.分析AWR報告
3.1 AWR報告頭信息

Elapsed:采樣時間段
DB Time:用戶操作花費的時間
DB Time遠小於Elapsed Time說明數據庫比較空閑
3.2 AWR負載概要信息

Redo size:標志數據庫的繁忙程度
Parses:小於300則表示正常
Hard parses:硬解析,小於100則表示正常
從以上數據可以初步說明此數據庫的吞吐量與負載正常
3.3 AWR實例效率

Buffer Nowait%:內存獲得數據的未等待比例
Buffer Hit%:小於80%則要加內存
Library Hit%:若低於90%,則需要調大share pool
In-memory sort%:內存排序比例,過低排序會在臨時表中進行,則需調大PGA
Soft Parse%:確保大於99%,否則意味着share pool latch爭用
以上數據可以說明數據庫實例命中率處於正常狀態
3.4 AWR TOP等待事件

在一個沒有問題的數據庫中,CPU time總是列在第一個位置
DB file sequential read:此等待事件為多表連接的順序存在問題,可能沒有正確的使用基表或是不加選擇的使用索引
DB file scattered read:此等待事件比較顯著說明存在大量的全表掃描或沒有創建合適的索引
ARCH wait on SENDERQ:遠程傳送archivelog的等待事件
Oracle大致有100多個空閑等待事件與非空閑等待事件,
Top 5 Timed Events是DB系統中影響比較大的前五個等待事件,
CPU Time是空閑等待時間,它在排第一名說明DB比較空閑,
Db file sequentail read與scattered read表明索引的選擇不是很合理,
3.5 AWR TOP SQL Tuning

對排名前幾位的sql進行turning,Top sql的order by 對象有以下幾種
SQL ordered by Elapsed Time: 根據sql執行時間總和排序
SQL ordered by CPU Time: 根據sql消耗CPU time總和排序
SQL ordered by Gets: 根據sql消耗邏輯IO總和排序
SQL ordered by Reads: 根據sql消耗物理IO總和排序
SQL ordered by Executions: 根據sql執行次數排序
SQL ordered by Parse Calls: 根據sql軟解析次數
主要針對ordered by Elapsed time,orderedby CPU time,orderedbygets,orderedby read排名前三SQL進行觀察並調優.
Oracle對SQL處理的步驟:
1.語法檢查(檢查SQL的拼寫語法是否正確)
2.語義檢查(檢查SQL中的訪問對象是否存在及是否具備相應權限)
3.進行解析(parse)(利用內部算法對SQL解析,生成解析樹(parsetree)及執行計划(execution plan))à軟硬解析發生在此過程中
4.執行SQL,返回結果
Oracle awr和ash區別可(參考)
http://www.cnblogs.com/rootq/archive/2009/09/24/1573196.html
