itsbegin
作者itsbegin·2013-02-18 15:17
软件开发工程师·上海通联支付

【范例】DB2的锁等待与事务的分析与定位

字数 19092阅读 106196评论 2赞 0
[原创] DB2的锁等待与事务的分析与定位
 
通过一个简单的模拟测试来得到DB2关于锁等待现象,并且根据锁等待信息来快速定位对应的事务与SQL语句。
 
测试用例:
[dbonl]: /dbonl> db2 "select * from num_inf fetch first 1 rows only"

NUM DESC REC_OPR_ID REC_UPD_OPR REC_CRT_TS REC_UPD_TS                
------- ------------------------------ ---------- ----------- -------------------------- --------------------------
1001 余额查询 I initOpr 2009-01-01-00.00.00.000000 2009-01-01-00.00.00.000000

提交一个DML并且设置不自动提交:
[dbonl]: /dbonl> db2 +c "insert into num_inf(num,desc,rec_opr_id,rec_upd_opr) values('9990','测试插入','1','initOpr')"
DB20000I The SQL command completed successfully.

此时在另外一个AGENT上进行查询操作,此时查询为挂起等待状态(FETCH 1 ROWS不挂起):
[dbonl]: /dbonl> db2 "select * from num_inf fetch first 1 rows only"                                                          

NUM DESC REC_OPR_ID REC_UPD_OPR REC_CRT_TS REC_UPD_TS                
------- ------------------------------ ---------- ----------- -------------------------- --------------------------
1001 余额查询 I initOpr 2009-01-01-00.00.00.000000 2009-01-01-00.00.00.000000

  1 record(s) selected.

[dbonl]: /dbonl> db2 "select * from num_inf"  

 
查看WAIT LOCKS信息如下:
[dbonl]: /dbonl> db2pd -d dbonl -locks showlocks wait

Database Partition 0 -- Database dbonl -- Active -- Up 70 days 22:00:22

Locks:
Address TranHdl Lockname Type Mode Sts Owner Dur HoldCount Att ReleaseFlg
0x07800000204CCEC0 15 00030092000000001D40006952 Row .NS W 16 1 0 0x00 0x00000001 TbspaceID 3 TableID 146 PartitionID 0 Page 7488 Slot 105
0x07800000204DF500 16 00030092000000001D40006952 Row ..X G 16 1 0 0x08 0x40000000 TbspaceID 3 TableID 146 PartitionID 0 Page 7488 Slot 105

这里可以看到,一个事务在等待(W)并且请求为NS锁类型,一个事务在写入(G)并且锁类型为X独占锁。对应的表为表空间3内的TABLEID为146的表。这时可以查询为那个对象:

[dbonl]: /dbonl> db2 "select tbspace,tabschema,tabname,tableid,tbspaceid from syscat.tables where tbspaceid=3 and tableid=146" 

TBSPACE TABSCHEMA TABNAME TABLEID TBSPACEID
-------------------------------------------------------------------------------------------------------------------------------- 
DATSPACE01 dbonl NUM_INF 146 3

当时的TRANSACTION信息如下:
[dbonl]: /dbonl> db2pd -transactions -d dbonl


Database Partition 0 -- Database dbonl -- Active -- Up 70 days 22:07:45


Transactions:
Address AppHandl [nod-index] TranHdl Locks State Tflag Tflag2 Firstlsn Lastlsn LogSpace SpaceReserved TID AxRegCnt GXID     
0x0780000020301C80 310 [000-00310] 2 1 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F20944 1 0        
0x0780000020302A80 212 [000-00212] 3 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x0000042BA339 1 0        
0x0780000020303880 36 [000-00036] 4 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F21032 1 0        
0x0780000020304680 1331 [000-01331] 5 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x0000042BA33D 1 0        
0x0780000020305480 662 [000-00662] 6 3 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F209F9 1 0        
0x0780000020307080 210 [000-00210] 8 13 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F1AC93 1 0        
0x0780000020308C80 1365 [000-01365] 10 10 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F1AC95 1 0        
0x078000002030A880 1354 [000-01354] 12 1 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F2112F 1 0        
0x078000002030B680 1385 [000-01385] 13 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F20FB7 1 0        
0x078000002030C480 1081 [000-01081] 14 1 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F210E9 1 0        
0x078000002030D280 1221 [000-01221] 15 4 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F2106E 1 0        
0x078000002030E080 1253 [000-01253] 16 3 WRITE 0x00000000 0x00000000 0x0002833655D1 0x0002833655D1 110 210 0x000004F20F03 1 0        
0x078000002030EE80 232 [000-00232] 17 2 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F1ACD8 1 0        
0x078000002030FC80 512 [000-00512] 18 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F1C98D 1 0        
0x0780000020310A80 1121 [000-01121] 19 13 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F1AC97 1 0        
0x0780000020311880 613 [000-00613] 20 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F2093E 1 0        
0x0780000020313480 743 [000-00743] 22 1 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F20DE4 1 0        
0x0780000020315E80 1401 [000-01401] 25 2 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F1AC91 1 0        
0x0780000020317A80 497 [000-00497] 27 0 READ 0x00000000 0x00000000 0x000000000000 0x000000000000 0 0 0x000004F20BF8 1 0        
 
这时根据TRANSACTIONS和APPLHANDLE对应信息可以找到APPL。再抓一个AGENTS快照:
 
[dbonl]: /dbonl> db2pd -d dobnl -agents

Database DOBNL not activated on database partition 0.
Option -agents is an instance scope option. The database option has been ignored.

Database Partition 0 -- Active -- Up 129 days 02:36:41

Agents:
Current agents: 32
Idle agents: 8
Active coord agents: 21
Active agents total: 21
Pooled coord agents: 3
Pooled agents total: 3

Address AppHandl [nod-index] AgentPid Priority Type State ClientPid Userid ClientNm Rowsread Rowswrtn LkTmOt DBName  
0x078000000029C500 0 [000-00000] 925726 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x07800000001EE340 0 [000-00000] 991368 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x0780000000353C40 0 [000-00000] 4473040 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x0780000000356AE0 0 [000-00000] 8601638 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x0780000000350740 0 [000-00000] 1757362 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x0780000000290080 0 [000-00000] 950384 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x0780000000351520 0 [000-00000] 2281542 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x0780000000355F80 0 [000-00000] 8646718 0 Idle n/a n/a n/a 0 0 NotSet n/a     
0x078000000029EC20 743 [000-00743] 1986794 0 Coord Inst-Active 3092620 dbbke db2sysc 2100 0 NotSet dbonl
0x07800000002919C0 1288 [000-01288] 1069090 0 Coord Inst-Active 2519162 dbonl db2bp 0 0 NotSet n/a     
0x0780000000295B00 1253 [000-01253] 1335438 0 Coord Inst-Active 2519162 dbonl db2bp 537 2 NotSet dbonl
0x0780000000294D20 1081 [000-01081] 1339536 0 Coord Inst-Active 2408696 wlq Switch 3444 22 NotSet dbonl
0x07800000003547A0 1269 [000-01269] 1605758 0 Coord Inst-Active 1077358 dbonl db2bp 0 0 NotSet n/a     
0x07800000003578C0 1221 [000-01221] 3084290 0 Coord Inst-Active 1077358 dbonl db2bp 103 0 NotSet dbonl
0x0780000000359480 662 [000-00662] 8048880 0 Coord Inst-Active 8089738 wlq PackSend 2 0 NotSet dbonl
0x07800000002968E0 497 [000-00497] 1294504 0 Coord Inst-Active 1019229440 db2jcc_a 6265 61 NotSet dbonl
0x07800000001ED7E0 310 [000-00310] 999426 0 Coord Inst-Active 3215556 wlq pbocGenF 225 0 NotSet dbonl
0x07800000001EEEA0 1354 [000-01354] 966880 0 Coord Inst-Active 3248352 wlq Tom 44 6 NotSet dbonl
0x078000000029B9A0 613 [000-00613] 946396 0 Coord Inst-Active 3203168 wlq pbocSndF 49 0 NotSet dbonl
0x078000000029A060 232 [000-00232] 1372320 0 Coord Inst-Active 8585394 wlq ToCtl 34 1 NotSet dbonl
0x07800000003530E0 1385 [000-01385] 8618094 0 Coord Inst-Active 2212084 wlq SavFwd 1899 3 NotSet dbonl
0x078000000029E0C0 1121 [000-01121] 1990834 0 Coord Inst-Active 4550684 wlq TransIn 26710 0 NotSet dbonl
0x0780000000290BE0 1365 [000-01365] 938050 0 Coord Inst-Active 4444378 wlq Manage 952 0 NotSet dbonl
0x0780000000352580 210 [000-00210] 3158162 0 Coord Inst-Active 5292150 wlq PrtOnl 1744 0 NotSet dbonl
0x078000000029ABC0 1401 [000-01401] 1204468 0 Coord Inst-Active 6189128 wlq Daemon 315 0 NotSet dbonl
0x07800000003586A0 512 [000-00512] 8577082 0 Coord Inst-Active 1019224320 db2jcc_a 3825 4 NotSet dbonl
0x0780000000297FA0 1331 [000-01331] 1286168 0 Coord Inst-Active 0 db2evmg_ 0 0 3 dbonl
0x0780000000297440 36 [000-00036] 1290390 0 Coord Inst-Active 893108 dbbke db2taskd 12 0 NotSet dbonl
0x0780000000293160 212 [000-00212] 1364126 0 Coord Inst-Active 893108 dbbke db2stmm 0 0 NotSet dbonl
0x0780000000299000 0 [000-00000] 1282134 0 Coord Pooled n/a n/a n/a 0 0 NotSet dbonl
0x0780000000293F40 0 [000-00000] 1343636 0 Coord Pooled n/a n/a n/a 0 0 NotSet dbonl
0x078000000029D060 0 [000-00000] 921844 0 Coord Pooled n/a n/a n/a 0 0 NotSet dbonl

对1253的AGENTID进行快照可以看到对应的SQL语句:
[dbonl]: /dbonl> db2 get snhot for application agentid 1253                                                                        


            Application Snhot


Application handle = 1253
Application status = UOW Waiting
Status change time = Not Collected
Application code page = 1386
Application country/region code = 1
DUOW correlation token = *LOCAL.dbonl.120203054653
Application name = db2bp
Application ID = *LOCAL.dbonl.120203054653
Sequence number = 00009
TP Monitor client user ID =
TP Monitor client workstation name =
TP Monitor client application name =
TP Monitor client accounting string =


Connection request start timestamp = 02/03/2012 13:46:53.024282
Connect request completion timestamp = 02/03/2012 13:46:53.024401
Application idle time = 1 minute 10 seconds
CONNECT Authorization ID = dbonl
Client login ID = dbonl
Configuration NNAME of client = tldeva
Client database manager product ID = SQL09010
Process ID of client application = 2519162
Platform of client application = AIX 64BIT
Communication protocol of client = Local Client


Inbound communication address = *LOCAL.dbonl


Database name = dbonl
Database path = /onllog/dbonl/NODE0000/SQL00001/
Client database alias = dbonl
Input database alias =
Last reset timestamp =
Snhot timestamp = 02/03/2012 14:04:22.402229
The highest authority level granted =
        Direct DBADM authority
        Direct CREATETAB authority
        Direct BINDADD authority
        Direct CONNECT authority
        Direct CREATE_NOT_FENC authority
        Direct LOAD authority
        Direct IMPLICIT_SCHEMA authority
        Direct CREATE_EXT_RT authority
        Direct QUIESCE_CONN authority
        Indirect SYSADM authority
        Indirect CREATETAB authority
        Indirect BINDADD authority
        Indirect CONNECT authority
        Indirect IMPLICIT_SCHEMA authority
Coordinating database partition number = 0
Current database partition number = 0
Coordinator agent process or thread ID = 1335438
Agents stolen = 0
Agents waiting on locks = 0
Maximum associated agents = 1
Priority at which application agents work = 0
Priority type = Dynamic


Lock timeout (seconds) = -1
Locks held by application = 3
Lock waits since connect = 0
Time application waited on locks (ms) = 0
Deadlocks detected = 0
Lock escalations = 0
Exclusive lock escalations = 0
Number of Lock Timeouts since connected = 0
Total time UOW waited on locks (ms) = Not Collected


Total sorts = 1
Total sort time (ms) = Not Collected
Total sort overflows = 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 data writes = 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
Buffer pool index writes = Not Collected
Buffer pool xda logical reads = Not Collected
Buffer pool xda physical reads = Not Collected
Buffer pool temporary xda logical reads = Not Collected
Buffer pool temporary xda physical reads = Not Collected
Buffer pool xda writes = Not Collected
Total buffer pool read time (milliseconds) = Not Collected
Total buffer pool write time (milliseconds)= Not Collected
Time waited for prefetch (ms) = Not Collected
Unread prefetch pages = Not Collected
Direct reads = Not Collected
Direct writes = Not Collected
Direct read requests = Not Collected
Direct write requests = Not Collected
Direct reads eled time (ms) = Not Collected
Direct write eled time (ms) = Not Collected


Number of SQL requests since last commit = 1
Commit statements = 3
Rollback statements = 5
Dynamic SQL statements attempted = 24
Static SQL statements attempted = 8
Failed statement operations = 6
Select SQL statements executed = 3
Xquery statements executed = 0
Update/Insert/Delete statements executed = 3
DDL statements executed = 0
Inactive stmt history memory usage (bytes) = 0
Internal automatic rebinds = 0
Internal rows deleted = 0
Internal rows inserted = 0
Internal rows updated = 0
Internal commits = 1
Internal rollbacks = 0
Internal rollbacks due to deadlock = 0
Binds/precompiles attempted = 0
Rows deleted = 0
Rows inserted = 3
Rows updated = 0
Rows selected = 216
Rows read = 537
Rows written = 3


UOW log space used (Bytes) = Not Collected
Previous UOW completion timestamp = Not Collected
Eled time of last completed uow (sec.ms)= Not Collected
UOW start timestamp = Not Collected
UOW stop timestamp = Not Collected
UOW completion status = Not Collected


Open remote cursors = 0
Open remote cursors with blocking = 0
Rejected Block Remote Cursor requests = 0
Accepted Block Remote Cursor requests = 3
Open local cursors = 0
Open local cursors with blocking = 0
Total User CPU Time used by agent (s) = 0.012200
Total System CPU Time used by agent (s) = 0.000805
Host execution eled time = 0.041733


Package cache lookups = 15
Package cache inserts = 3
Application section lookups = 24
Application section inserts = 4
Catalog cache lookups = 17
Catalog cache inserts = 0
Catalog cache overflows = 0
Catalog cache high water mark = 0


Workspace Information


 Shared high water mark = 0
 Total shared overflows = 0
 Total shared section inserts = 0
 Total shared section lookups = 0
 Private high water mark = 26288
 Total private overflows = 0
 Total private section inserts = 4
 Total private section lookups = 6


Most recent operation = Execute Immediate
Most recent operation start timestamp = 02/03/2012 14:03:12.497939
Most recent operation stop timestamp = 02/03/2012 14:03:12.498063
Agents associated with the application = 1
Number of hash joins = 0
Number of hash loops = 0
Number of hash join overflows = 0
Number of small hash join overflows = 0
Statement type = Dynamic SQL Statement
Statement = Execute Immediate
Section number = 203
Application creator = NULLID
Package name = SQLC2F0A
Consistency Token = AAAAAHLV
Package Version ID =
Cursor name =
Statement database partition number = 0
Statement start timestamp = 02/03/2012 14:03:12.497939
Statement stop timestamp = 02/03/2012 14:03:12.498063
Eled time of last completed stmt(sec.ms)= 0.000124
Total Statement user CPU time = 0.000000
Total Statement system CPU time = 0.000000
SQL compiler cost estimate in timerons = 8
SQL compiler cardinality estimate = 1
Degree of parallelism requested = 1
Number of agents working on statement = 0
Number of subagents created for statement = 1
Statement sorts = 0
Total sort time = 0
Sort overflows = 0
Rows read = 0
Rows written = 1
Rows deleted = 0
Rows updated = 0
Rows inserted = 0
Rows fetched = 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 logical reads = Not Collected
Buffer pool temporary index logical reads = Not Collected
Buffer pool temporary index physical reads = Not Collected
Buffer pool xda logical reads = Not Collected
Buffer pool xda physical reads = Not Collected
Buffer pool temporary xda logical reads = Not Collected
Buffer pool temporary xda physical reads = Not Collected
Blocking cursor = NO
Dynamic SQL statement text:
insert into num_inf(num,desc,rec_opr_id,rec_upd_opr) values('9990','测试插入','1','initOpr')




Agent process/thread ID = 1335438
  Agent Lock timeout (seconds) = -1
  Memory usage for agent:


    Memory Pool Type = Other Memory
       Current size (bytes) = 131072
       High water mark (bytes) = 655360
       Configured size (bytes) = 4294967296


    Memory Pool Type = Application Control Heap
       Current size (bytes) = 65536
       High water mark (bytes) = 65536
       Configured size (bytes) = 655360


    Memory Pool Type = Application Heap
       Current size (bytes) = 196608
       High water mark (bytes) = 196608
       Configured size (bytes) = 1245184

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论2 条评论

shlei6067shlei6067联盟成员数据库管理员NJ
2013-04-25 17:31
总结的不错。
Priest_LeonPriest_Leon系统架构师99无限
2013-04-01 11:01
蛮不错的
Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广