??xml version="1.0" encoding="utf-8" standalone="yes"?>
下面单做一个验证:
c:>sqlplus / as sysdba
sys@EOS >create table test as select table_name,to_timestamp(last_analyzed) date_test from dba_tables;
表已创徏?br />
sys@EOS> create index idx_test_date on test (date_test);
索引已创建?br />
sys@EOS> desc test
名称 是否为空? cd
----------------------------------------------------- -------- ----------------
--------------------
TABLE_NAME NOT NULL VARCHAR2(30)
DATE_TEST TIMESTAMP(0)
sys@EOS> select date_test from test where date_test > TO_DATE('2007-11-5 00:00:00','yyyy-MM-dd HH24:mi:ss');
执行计划
----------------------------------------------------------
Plan hash value: 944171586
-------------------------------------------------------------------------------- --
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------- --
| 0 | SELECT STATEMENT | | 1 | 22 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_TEST_DATE | 1 | 22 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------- --
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("DATE_TEST">TIMESTAMP'2007-11-05 00:00:00')
Note
-----
- dynamic sampling used for this statement
l计信息
----------------------------------------------------------
7 recursive calls
0 db block gets
18 consistent gets
0 physical reads
0 redo size
280 bytes sent via SQL*Net to client
374 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
从上面可以清楚看刎ͼtimestamp>date情况下,走烦?br />
U正我之前的认识?br />
另外再补充一下,dateq个数据cd一般情况下很少用,产品里面所有的date数据cd全部改ؓtimestamp
]]>
在应用用过E中Q我们经怼到应用响应旉很慢Q甚x有响应,但是应用服务器可能ƈ不是很繁忙,cpu利用率也非常低,引vq种状况的原因有很多U,比如环境问题Q应用资源泄漏,数据库原因等{,本文主要是从一ơ应用性能诊断q程来谈谈如何通过数据库诊断应用性能问题?/p>
问题Q?/p>
试q程中发现应用中某个跌{面执行旉比较长,pȝ压力不大Qcpu利用很低Q该面需要从cache中取数据Q第一ơ的时候加载cacheQ从数据库中查询回数据ƈcacheQ?/p>
诊断Q?/p>
面逻辑比较单,我们先用loadrunner模拟q发试一下这个页面,然后再数据库端捕获sql执行情况?/p>
1、打开db2监控开?/p>
#db2 connect to eos
#db2 update monitor switches using statement on
#db2 reset monitor all
2、几分钟之后Q我们收集sqll计快照
#db2 get snapshot for dynamic sql on eos > dysqlstatus.out
现在l计信息已经存放在dysqlstatus.out中,你可以用Q意方便的文本处理工具查看Q我一般用windows上的gvim来处理,打开dysqlstatus.out
Number of executions = 1
Number of compilations = 1
Worst preparation time (ms) = 2
Best preparation time (ms) = 2
Internal rows deleted = 0
Internal rows inserted = 0
Rows read = 2
Internal rows updated = 0
Rows written = 0
Statement sorts = 0
Statement sort overflows = 0
Total sort time = 0
Buffer pool data logical reads = Not Collected
Buffer pool data physical reads = Not Collected
Buffer pool temporary data logical reads = Not Collected
Buffer pool temporary data physical reads = Not Collected
Buffer pool index logical reads = Not Collected
Buffer pool index physical reads = Not Collected
Buffer pool temporary index logical reads = Not Collected
Buffer pool temporary index physical reads = Not Collected
Total execution time (sec.ms) = 0.000377
Total user cpu time (sec.ms) = 0.010000
Total system cpu time (sec.ms) = 0.000000
Statement text = select ACTIVITYDEFID,ACTIVITYINSTID from wfworkitem wherePROCESSINSTID=104199 and CURRENTSTATE = 4
......
单说一下vi中的处理
:g!/Total execution time/d
只保留文本中的sql执行旉Q我们要按照执行旉来排?br>
通过vim的visual功能选择执行旉块({号后面的数字)Q然后排?br> Total execution time (sec.ms) = 0.050590
Total execution time (sec.ms) = 0.000170
Total execution time (sec.ms) = 0.000247
Total execution time (sec.ms) = 0.000292
Total execution time (sec.ms) = 0.000474
Total execution time (sec.ms) = 0.000330
Total execution time (sec.ms) = 0.000348
Total execution time (sec.ms) = 0.000279
Total execution time (sec.ms) = 0.000385
Total execution time (sec.ms) = 0.000296
Total execution time (sec.ms) = 0.000261
Total execution time (sec.ms) = 0.000195
Total execution time (sec.ms) = 0.000226
Total execution time (sec.ms) = 0.000227
Total execution time (sec.ms) = 0.000193
......
:'<,'>!sort
排序后的l果Q部分)
Total execution time (sec.ms) = 2.027776
Total execution time (sec.ms) = 2.203624
Total execution time (sec.ms) = 2.504677
Total execution time (sec.ms) = 2.951256
Total execution time (sec.ms) = 3.119875
Total execution time (sec.ms) = 3.303277
Total execution time (sec.ms) = 3.303517
Total execution time (sec.ms) = 4.017133
Total execution time (sec.ms) = 4.043329
Total execution time (sec.ms) = 4.252125
Total execution time (sec.ms) = 4.400952
Total execution time (sec.ms) = 4.606765
Total execution time (sec.ms) = 5.208087
Total execution time (sec.ms) = 5.778598
Total execution time (sec.ms) = 8.117470
Total execution time (sec.ms) = 9797.905136
可以看到最长时间的sql total执行旉耗费?797.905123s.
现在我们到dysqlstatus.out中去找这条语?/p>
Number of executions = 4602
Number of compilations = 4294967295
Worst preparation time (ms) = 2
Best preparation time (ms) = 2
Internal rows deleted = 0
Internal rows inserted = 0
Rows read = 2963688
Internal rows updated = 0
Rows written = 0
Statement sorts = 0
Statement sort overflows = 0
Total sort time = 0
Buffer pool data logical reads = Not Collected
Buffer pool data physical reads = Not Collected
Buffer pool temporary data logical reads = Not Collected
Buffer pool temporary data physical reads = Not Collected
Buffer pool index logical reads = Not Collected
Buffer pool index physical reads = Not Collected
Buffer pool temporary index logical reads = Not Collected
Buffer pool temporary index physical reads = Not Collected
Total execution time (sec.ms) = 9797.905136
Total user cpu time (sec.ms) = 9.290000
Total system cpu time (sec.ms) = 1.230000
Statement text = select * from XXXX_T_CNFACTIVITYDEF
q条语句d执行?602ơ,q_每次的执行时?SQ而且q些数据应该是被cacheh?nbsp; Q)
ȝQ?/p>
上面的方法简单ȝ了从数据库层面对应用的性能问题诊断Q希望对大家有所帮助Q对于数据库快照诊断问题的思\对于L数据库通用
补充一个unix上脚本处理方式:
sqlsort.sh
awk 'BEGIN{RS="";FS="\n";ORS="\n"};/Statement text/{print $1, $21, $24}' $1 | awk '$5 > 0 {print "AvgTime:", $11/$5, "\t", $0}'| sort -n | head -n $2|awk '{print $0, "\n"}'
在应用中Q我们经怼到sql执行很慢Q但是数据库cpu和内存用率又不高的情况Q类似的问题基本上由于锁Q排序等原因造成Q本文主要描q如何去定位锁等待问题,谁在锁等待?{待谁持有的锁?锁在那个表?
一、测试准?/p>
1、先在session1执行如下操作Q创建测试表
#db2 connect to eos #export DB2OPTIONS=+C #db2 "create table tacy_test (a int not null primary key,b varchar(10))" #db2 "insert into tacy_test values(1,'a')" #db2 "insert into tacy_test values(2,'a')" #db2 "insert into tacy_test values(3,'a')" #db2 "insert into tacy_test values(4,'a')" #db2 commit
2、在session2执行如下操作
#db2 connect to eos #export DB2OPTIONS=+C
二、生一个lock wait
在session1做一个表更新Q?/p>
#db2 "update tacy_test set b='b' where a=4"
#db2 "update tacy_test set b='c' where a=4"
q程被挂L?/p>
三、定位锁{待
1、先来看看应用的情况Q?/p>
#db2pd -db eos -applications Database Partition 0 -- Database EOS -- Active -- Up 0 days 07:37:37 Applications: Address AppHandl [nod-index] NumAgents CoorPid Status C-AnchID C-StmtUID L-AnchID L-StmtUID Appid 0x10140040 8 [000-00008] 1 8425 Lock-wait 80 2 66 1 *LOCAL.db2inst1.071124043739 0x100CE540 7 [000-00007] 1 8358 UOW-Waiting 0 0 80 2 *LOCAL.db2inst1.071124043708
可以看到有一个应用的状态处于Lock-wait
2、现在我们来看看应用在等什?/p>
#db2pd -db eos -locks showlock wait Database Partition 0 -- Database EOS -- Active -- Up 0 days 07:42:56 Locks: Address TranHdl Lockname Type Mode Sts Owner Dur HldCnt Att Rlse 0x2C8E0760 3 02001806078066020000000052 Row ..X W 2 1 0 0 0x0 TbspaceID 2 TableID 1560 RecordID 0x2668007
锁的cd为RowQ行锁)QX锁(排他锁)Q下面是我们最兛_的锁的位|?/p>
TbspaceID 2 TableID 1560 RecordID 0x2668007
其中TbspaceIDI间IDQTableID的IDQRecordID代表具体位置Q全部应该是0x0266807,其中前面三个字节为page numberQؓ0x02668Q后面一个字节代表solt identifierQؓ0x073、找到相应的?/p>
#db2 "select tbspace,tabschema,tabname,tableid,tbspaceid from syscat.tables where tbspaceid=2 and tableid=1560"
TBSPACE TABSCHEMA TABNAME TABLEID TBSPACEID
------------ ----------- ---------- ------- ---------
USERSPACE1 DB2INST1 TACY_TEST 1560 2
1 record(s) selected.
4、根据RecordID扑ֈ锁在哪行
db2提供了一个强大的数据分析工具db2dartQ可以dump出相应的page数据
#db2dart eos /dd /tsi 2 /oi 1560 /ps 157312p /np 1 /v y Warning: The database state is not consistent. Warning: Reorg rows MAY be due to the inconsistent state of the database. DB2DART Processing completed with warning(s)! Complete DB2DART report found in: /home/db2inst1/sqllib/db2dump/DART0000/EOS.RPT
其中tsiI间id(2)Qoiid(1560)Qps为page number(0x0266807)Q需要{换ؓ十进Ӟ在结ֿdpQnp代表你要获取的页敎ͼv为是否详l输?/p>
现在我们来看看EOS.RPT
______________________________________________________________________________ _______ DART _______ D a t a b a s e A n a l y s i s a n d R e p o r t i n g T o o l IBM DB2 6000 ______________________________________________________________________________ DART (V8.1.0) Report: 2007-11-24-20.59.51.355893 Database Name: EOS Report name: EOS.RPT Old report back-up: EOS.BAK Database Subdirectory: /opt/db2/db2inst1/NODE0000/SQL00001 Operational Mode: Database Inspection Only (INSPECT) ______________________________________________________________________________ ------------------------------------------------------------------------------ Action option: DD Table-object-ID: 1560; Tablespace-ID: 2; First-page: 157312p; Number-pages: 1; Verbose: y Warning: The database state is not consistent. Warning: Reorg rows MAY be due to the inconsistent state of the database. Connecting to Buffer Pool Services... Table object report phase start. Dump format is verbose. ______________________________________ Page 0 of object 1560 from table space 2. BPS Page Header: Page Data Offset = 48 Page Data Length = 4048 Page LSN = 0000 AE97 AE41 Object Page Number = 0 Pool Page Number = 157312 Object ID = 1560 Object Type = Data Object Data Page Header: Slot Count = 8 Total Free Space = 2784 Total Reserve Space = 0 Youngest Reserve Space = n/a Youngest TID = n/a Free Space Offset = 2799 Maximum Record Size = 23 Data Records: Slot 0: Offset Location = 3996 (xF9C) Record Length = 32 (x20) Record Type = Data Object Header Control Record Page count = 1 Object Creation LSN = 0000 AE97 800C Object State = x0000 UDI Since Runstats = 0 DART Field = x00000000 Slot 1: Offset Location = 2992 (xBB0) Record Length = 1004 (x3EC) Record Type = Free Space Control Record Free space entries: 0: 2884 (x0B44), 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC) 4: 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC) 8: 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC) 省略。。? 492: 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC) 496: 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC), 4028 (x0FBC) Slot 2: Offset Location = 2916 (xB64) Record Length = 76 (x4C) Record Type = Table Directory Record MetaIndex Root Page = 157377 Index Type = 2 Table Descriptor Pointer -- Page 157312 Slot 3 Max Insert Search = 0 Flags = x02000200 bit representation = 00000010 00000000 00000010 00000000 Check pending info: Constraint status = x00 Constraint RID = Page 0 Slot 0 last BID = x00000000 Slot 3: Offset Location = 2892 (xB4C) Record Length = 24 (x18) Record Type = Table Description Record Number of Columns = 2 Column 1: Type is Long Integer Length = 4 Prohibits NULLs Prohibits Default Fixed offset: 0 Column 2: Type is Fixed Length Character String Length = 10 Allows NULLs Prohibits Default Fixed offset: 4 Slot 4: Offset Location = 2869 (xB35) Record Length = 23 (x17) Record Type = Table Data Record (FIXEDVAR) Fixed part length value = 15 Column 1: Fixed offset: 0 Type is Long Integer Value = 1 Column 2: Fixed offset: 4 Type is Fixed Length Character String 61202020 20202020 2020 a Slot 5: Offset Location = 2846 (xB1E) Record Length = 23 (x17) Record Type = Table Data Record (FIXEDVAR) Fixed part length value = 15 Column 1: Fixed offset: 0 Type is Long Integer Value = 2 Column 2: Fixed offset: 4 Type is Fixed Length Character String 61202020 20202020 2020 a Slot 6: Offset Location = 2823 (xB07) Record Length = 23 (x17) Record Type = Table Data Record (FIXEDVAR) Fixed part length value = 15 Column 1: Fixed offset: 0 Type is Long Integer Value = 3 Column 2: Fixed offset: 4 Type is Fixed Length Character String 61202020 20202020 2020 a Slot 7: Offset Location = 2800 (xAF0) Record Length = 23 (x17) Record Type = Table Data Record (FIXEDVAR) Fixed part length value = 15 Column 1: Fixed offset: 0 Type is Long Integer Value = 4 Column 2: Fixed offset: 4 Type is Fixed Length Character String 61202020 20202020 2020 a Slots Summary: Total=8, In-use=8, Deleted=0. Table object report phase end. ______________________________________ DB2DART Processing completed with warning(s)! Warning(s) detected during processing. ______________________________________ Complete DB2DART report found in: /home/db2inst1/sqllib/db2dump/DART0000/EOS.RPT _______ D A R T P R O C E S S I N G C O M P L E T E _______
扑ֈSolt 7 (0x07)QokQ你现在可以清楚的知道应用等待的Row?4,a)
ȝ
通过上面的方法,我们单描qC一个db2锁问题的定位ҎQ希望能l大家在分析和定位应用性能问题的时候vC定的帮助
db2 "create event monitor SQLCOST for statements write to file '/home/db2inst1'"
再设|事务状态ؓ打开
db2 "set event monitor SQLCOST state=1"
注:1为打开Q?为关闭,攉数据之后Q记得关闭你的监控器Q否则。。?/font> 跑你的测试后Q在你的/home/db2inst1目录下会生成一些evm文g 用下面指令获取诊断信息: db2evmon -db eos51 -evm SQLCOST>sqlcost1.txt 完成之后删除你的监控? db2 "drop event monitor SQLCOST" 生成的采样例子,从下面的例子中,你可以清除的看到SQL执行的时_CPU消耗情况,排序是否溢出QBufferPool的用情况,Ҏq些信息QSQL的执行效率一目了Ӟ 26) Statement Event ... Appl Handle: 336 Appl Id: C0A80421.O905.0ABDA5065446 Appl Seq number: 0657 Record is the result of a flush: FALSE ------------------------------------------- Type : Dynamic Operation: Execute Section : 7 Creator : NULLID Package : SYSSN300 Consistency Token : SYSLVL01 Package Version ID : Cursor : SQL_CURSN300C7 Cursor was blocking: FALSE Text : update WFProcessInst set relateData=? where processInstID= ? ------------------------------------------- Start Time: 04/25/2007 14:57:19.402248 Stop Time: 04/25/2007 14:57:19.409622 Exec Time: 0.007374 seconds Number of Agents created: 1 User CPU: 0.000000 seconds System CPU: 0.000000 seconds [licl1] Fetch Count: 0 Total sort time: 0 Sort overflows: 0 [licl2] Rows read: 1 Rows written: 1 Internal rows deleted: 0 Internal rows updated: 0 Internal rows inserted: 0 Bufferpool data logical reads: 9 Bufferpool data physical reads: 0 Bufferpool temporary data logical reads: 0 Bufferpool temporary data physical reads: 0 Bufferpool index logical reads: 3 Bufferpool index physical reads: 0 Bufferpool temporary index logical reads: 0 Bufferpool temporary index physical reads: 0 [licl3] SQLCA: sqlcode: 0 sqlstate: 00000 [licl1]SQL执行旉和CPU消耗情? [licl2]SQL的排序情况,可以看到q个SQL没有排序Q当然也没有排序溢出 [licl3]Bufferpool的用情况,逻辑d物理ȝҎ
]]>