1. SQL COST ANALYSIS
許多情況下,一個簡單的SQL就可能讓DB2系統處于尷尬的狀態。調整參數也不能解決此問題。由于DBA很難去改變這些垃圾SQL的現狀,所以留給DBA的就是下面的情況:
(1). Change or add indexes
(2). Change clustering
(3). Change catalog statistics.
注:一個SQL語句的cost= 每次執行的資源代價*執行的次數。
目前,DBA面臨的挑戰就是要找到那些有很高cost的語句,并且盡力去減少它的代價??梢越柚鶧B2 Explain 工具或者DB2 UDB SQL Event Monitor數據來分析SQL語句的代價。尤其是對SQL Event Monitor的數據分析,但這么做需要耗費很大的精力和時間。
一般DBA的流程是:
(1). Create an SQL Event Monitor, write to file:
$> db2 "create event monitor SQLCOST for statements write to ..."
(2). Activate the event monitor (be sure ample free disk space is available):
$> db2 "set event monitor SQLCOST state = 1"
(3). Let the application run.
(4). Deactivate the event monitor:
$> db2 "set event monitor SQLCOST state = 0"
(5). Use the DB2-supplied db2evmon tool to format the raw SQL Event Monitor data (hundreds of megabytes of free disk space may be required depending on SQL throughput rates):
$> db2evmon -db DBNAME -evm SQLCOST
> sqltrace.txt
(6). Browse through the formatted file scanning for unusually large cost numbers, a time-consuming process:
$> more sqltrace.txt
(7). Undertake a more complete analysis of the formatted file that attempts to identify unique statements (independent of literal values), each unique statement's frequency (how many times it oclearcase/" target="_blank" >ccurred), and the aggregate of its total CPU, sort, and other resource costs. Such a thorough analysis could take a week or more on just a 30-minute sample of application SQL activity.
為了以最快的速度找到相應的SQL,我們可以考慮上文講過的一些方法:
針對第4個tip:計算每個交易從一個table里面取出的行數。如果數值很高,就可以找到相應的語句。
針對第3個tip:計算每個tablespace的asynchronous read percentage and physical I/O read rates.如果一個tablespace有很高的asynchronous read percentage 和高于平均的physical I/O read rates,那么有可能這個tablesapce里面有table scan情況。從catalog中可以找尋tablespace中相應的table(如果一個tablespace上只有一個表,那么很容易定位了),然后從SQL Event Monitor 中尋找相關的table。這樣也可以縮小范圍。
觀察DB2 Explain信息,尋找可疑的地方。有時候,經常執行的、而且是代價比較低的語句也會瘋狂占用系統資源!
很多時候,我們可以充分借助工具!這樣能省時省力。
Staying in Tune
需要特別注意的是,性能優化不能僅僅只是消除那些好的SQL語句,也要保證合理的物理構架,確保高性能的結果、內存分配在pool和heap中,I/O都在DISk之間平衡分布。