DB2中,查看某个应用正在执行的SQL语句

问题:DB2中,知道应用的application ID, 如何查看这个应用正在执行的SQL语句?


答案:这里给出两种方法,第一种是查看应用的snapshot,第二种是使用db2pd工具


方法1:使用db2 snapshot,这种方法不但可以看到正在执行的SQL语句,还可以看这个SQL语句是什么时候开始执行的,结合抓取snapshot的时间,就可以推断出它执行了多久。 但要求执行SQL之前,监控开关是开的。 如何正确地打开监控开关?


C:\windows\system32>db2 get monitor switches

            Monitor Recording Switches

Switch list for member 0
Buffer Pool Activity Information  (BUFFERPOOL) = OFF
Lock Information                        (LOCK) = OFF
Sorting Information                     (SORT) = OFF
SQL Statement Information          (STATEMENT) = ON  2016-04-26 12:59:14.861177
Table Activity Information             (TABLE) = OFF
Take Timestamp Information         (TIMESTAMP) = ON  2016-04-26 11:02:21.175930
Unit of Work Information                 (UOW) = ON  2016-04-26 12:59:14.861179


C:\windows\system32>db2 "values application_id()"

1
--------------------------------------------------------------------------------
*LOCAL.DB2INST1.160426050147

  1 record(s) selected.


C:\windows\system32>db2 "insert into t2 select * from t1"


这时打开另一个session,发出get snapshot命令:

C:\windows\system32>db2 get snapshot for application applid *LOCAL.DB2INST1.160426050147

            Application Snapshot

Application handle                         = 138
Application status                         = UOW Executing
Status change time                         = 2016-04-26 13:52:07.835904
Application code page                      = 1386
Application country/region code            = 86
DUOW correlation token                     = *LOCAL.DB2INST1.160426050147
Application name                           = db2bp.exe
Application ID                             = *LOCAL.DB2INST1.160426050147

..
Database name                              = SAMPLE
Database path                              = C:\DB2INST1\NODE0000\SQL00001\MEMBER0000\
Client database alias                      = SAMPLE
Input database alias                       =
Last reset timestamp                       =
Snapshot timestamp                         = 2016-04-26 13:52:14.584789

..

Statement type                             = Dynamic SQL Statement
Statement                                  = Execute Immediate
Section number                             = 203
Application creator                        = NULLID
Package name                               = SQLC2K26
Consistency Token                          =
Package Version ID                         =
Cursor name                                =
Statement member number                    = 0
Statement start timestamp                  = 2016-04-26 13:52:07.835866
Statement stop timestamp                   =

lapsed time of last completed stmt(sec.ms)= 0.000000
Total Statement user CPU time              = 1.092008
Total Statement system CPU time            = 0.109201
SQL compiler cost estimate in timerons     = 97960
SQL compiler cardinality estimate          = 2943367
Degree of parallelism requested            = 2
Number of agents working on statement      = 2
Number of subagents created for statement  = 2
Statement sorts                            = 0
Total sort time                            = 0
Sort overflows                             = 0
Rows read                                  = 658812
Rows written                               = 654696
Internal rows deleted                      = 0
Internal rows updated                      = 0
Internal rows inserted                     = 0
Rows fetched                               = 0
Buffer pool data logical reads             = Not Collected
Buffer pool data physical reads            = Not Collected


Blocking cursor                            = NO
Dynamic SQL statement text:
insert into t2 select * from t1

从快照中可以看到,这个应用正处于uow executing状态,当前SQL的开始时间是2016-04-26 13:52:07.835866,还没有结束,目前时间是2016-04-26 13:52:14.584789,SQL语句内容为insert into t2 select * from t1


方法2, 使用db2pd,收集如下信息

C:\windows\system32>db2pd -db sample -app -dyn


Database Member 0 -- Database SAMPLE -- Active -- Up 0 days 00:50:23 -- Date 2016-04-26-13.52.10.917000

Applications:
Address            AppHandl [nod-index] NumAgents  CoorEDUID  Status                  C-AnchID C-StmtUID  L-AnchID L-StmtUID  Appid                        
0x0000000007DB0080 138      [000-00138] 3          9220       UOW-Executing           364      1          364      1          *LOCAL.DB2INST1.160426050147 
0x0000000007D80080 144      [000-00144] 1          9628       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050154      
0x0000000007D20080 143      [000-00143] 1          9776       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050153      
0x0000000007DA0080 142      [000-00142] 1          9884       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050152      
0x0000000007C80080 148      [000-00148] 1          6156       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050158      
0x0000000007DC0080 141      [000-00141] 1          3864       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050151      
0x0000000007CD0080 147      [000-00147] 1          2472       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050157      
0x0000000007DE0080 140      [000-00140] 1          4828       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050150      
0x0000000007C70080 146      [000-00146] 1          9280       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050156      
0x00000000071A0080 139      [000-00139] 1          8448       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050149      
0x0000000007CE0080 145      [000-00145] 1          9968       ConnectCompleted        0        0          0        0          *LOCAL.DB2.160426050155      


Dynamic SQL Statements:
Address            AnchID StmtUID    NumEnv     NumVar     NumRef     NumExe     Text 
0x000000002521F000 120    1          1          1          1          1          values application_id()
0x00000000251BE3E0 364    1          1          1          4          4          insert into t2 select * from t1

Dynamic SQL Environments:
Address            AnchID StmtUID    EnvID      Iso QOpt Blk
0x000000002521F120 120    1          1          CS  5    B
0x00000000251FF3E0 364    1          1          CS  5    B

Dynamic SQL Variations:
Address            AnchID StmtUID    EnvID      VarID      NumRef     Typ Lockname                   Val Insert Time                Sect Size  Num Copies
0x000000002521F4A0 120    1          1          1          1          6   01000000010000000100000FD6 Y   2016-04-26-13.48.40.864655 7040       1     
0x0000000025219520 364    1          1          1          4          4   01000000010000000100802DD6 Y   2016-04-26-13.47.27.747480 9968       3     


可以看到应用*LOCAL.DB2INST1.160426050147 是UOW-Executing状态,对应的C-AnchID 和C-StmtUID分别是361,1,而361,1 对应的SQL语句是  insert into t2 select * from t1


补充:

以上说的皆是dynamic SQL,如果是静态SQL,只能通过snapshot来看,并且会看到显示是 Static SQL statement:

Statement type                             = Static SQL Statement
Statement                                  = Execute
Section number                             = 2
application creator                        = MIAOQINGSONG
Package name                               = P431111789
Consistency Token                          =
Package Version ID                         =
Cursor name                                =
Statement member number                    = 0
Statement start timestamp                  = 2016-04-26 14:20:27.609259
Statement stop timestamp                   =


这时可以根据package name和Section number来看一下这个包的执行计划,执行计划中包含了SQL语句


C:\windows\system32>db2expln -database SAMPLE -schema MIAOQINGSONG -package  P431111789 -graph -terminal


******************** PACKAGE ***************************************

Package Name = "MIAOQINGSONG"."P431111789"  Version = ""

        Prep Date = 2016/04/26
        Prep Time = 14:19:40

        Bind Timestamp = 2016-04-26-14.19.40.347000

        Isolation Level          = Cursor Stability
        Blocking                 = Block Unambiguous Cursors
        Query Optimization Class = 5

        Partition Parallel       = No
        Intra-Partition Parallel = Yes (Bind Degree = ANY)

        Reoptimization           = No

        SQL Path                 = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
                                   "MIAOQINGSONG"

-------------------- SECTION ---------------------------------------
Section = 1

Statement:
  DECLARE CURSOR1 CURSOR
  WITH RETURN
  for
     SELECT LASTNAME
     FROM MIAOQINGSONG.EMPLOYEE

Intra-Partition Parallelism Degree = 2

Section Code Page = 1208

Estimated Cost = 6.860869
Estimated Cardinality = 42.000000


Process Using 2 Subagents
|  Access Table Name = MIAOQINGSONG.EMPLOYEE  ID = 2,6
|  |  #Columns = 1
|  |  Parallel Scan
|  |  Skip Inserted Rows
|  |  May participate in Scan Sharing structures
|  |  Scan may start anywhere and wrap, for completion
|  |  Fast scan, for purposes of scan sharing management
|  |  Scan can be throttled in scan sharing management
|  |  Relation Scan
|  |  |  Prefetch: Eligible
|  |  Lock Intents
|  |  |  Table: Intent Share
|  |  |  Row  : Next Key Share
|  Insert Into Synchronous Local Table Queue  ID () = q1
Access Local Table Queue  ID () = q1  #Columns = 3
Return Data to Application
|  #Columns = 1


End of section


..


-------------------- SECTION ---------------------------------------
Section = 2

Statement:

  INSERT INTO MIAOQINGSONG.T2
     SELECT ID
     FROM T1


Intra-Partition Parallelism Degree = 2

Section Code Page = 1208

Estimated Cost = 97959.609375
Estimated Cardinality = 2943367.000000

Process Using 2 Subagents
|  Access Table Name = MIAOQINGSONG.T1  ID = 3,5
|  |  #Columns = 1
|  |  Parallel Scan
|  |  Skip Inserted Rows
|  |  Avoid Locking Committed Data
|  |  Currently Committed for Cursor Stability
|  |  May participate in Scan Sharing structures
|  |  Scan may start anywhere and wrap, for completion
|  |  Fast scan, for purposes of scan sharing management
|  |  Scan can be throttled in scan sharing management
|  |  Relation Scan
|  |  |  Prefetch: Eligible
|  |  Lock Intents
|  |  |  Table: Intent Share
|  |  |  Row  : Next Key Share
|  Insert Into Asynchronous Local Table Queue  ID () = q1
Access Local Table Queue  ID () = q1  #Columns = 1
Insert:  Table Name = MIAOQINGSONG.T2  ID = 3,6


End of section
..



你可能感兴趣的:(DB2中,查看某个应用正在执行的SQL语句)