通常我们知道,DB2的package在统计信息或依赖对象发生变化后需要做下重新绑定,否则DB2优化器会采用效率较差或者错误的执行计划运行该package,造成执行性能严重下降或者SP一直会处于卡死状态。所以定期对SP进行重新绑定操作,对数据库整体性能和效率都有较大的提升,但是查一查,关于DB2的SP重新绑定,提供了三个命令:db2rbind、rebind和bind,很多人都分不清这三者区别,如何使用的问题,所以本文就重点对着三个命令用法进行分析。
其实三者都是提供SP重新绑定,不过传入参数不一样,bind需要带入.bnd的文件,缺陷情况下,C或嵌入式SQL编写的SP在precompile时,都会创建一个以.bnd的结尾的文件,数据库中使用程序包和应用程序编译的原因相同,即就是提高性能和压缩,在程序包构建过程而不是执行过程中,SP中的相关语法语义都得到分析,并且程序包中存储的是被解释效率更高的操作字符组,在服务器管理器调用预编译生生成的代码,只需要提供相应的输入变量就可以执行执行,提高了效率。并且预编译仅仅适用静态SQL,而不适合如prepare execute和execute immediate 动态SQL。这些动态SQL还是需要重新进行编译解释。db2提供绑定文件描述工具db2bfd工具可以查看bnd文件。其用法如下:
C:\Program Files (x86)\IBM\SQLLIB\bnd>db2bfd
Usage: db2bfd [ [-b] [-h] [-s] [-v] ] <filespec>
Where: <filespec> is at least a V7 bind file
Options: -b = display bind file header
-h = display this information
-s = display SQL statements
-v = display host variable declarations
C:\Program Files (x86)\IBM\SQLLIB\bnd>db2bfd -b db2sampl_SQL.bnd
db2sampl_SQL.bnd: Header Contents
Header Fields:
Field Value
----- -----
releaseNum 0x800
Endian 0x4c
numHvars 16
maxSect 14
numStmt 26
optInternalCnt 4
optCount 10
Name Value
------------------ -----
Isolation Level Cursor Stability
Creator "NULLID "
App Name "SQLE6H02"
Timestamp "AAAAABIU:2004/08/01 00:00:00:00"
Cnulreqd Yes
Sql Error No package
Block Block All
Validate Bind
Date Default/local
Time Default/local
*** All other options are using default settings as specified by the server ***
C:\Program Files (x86)\IBM\SQLLIB\bnd>db2bfd -s db2sampl_SQL.bnd
db2sampl_SQL.bnd: SQL Statements = 26
Line Sec Typ Var Len SQL statement text
---- --- --- --- --- ---------------------------------------------------------
55 0 5 0 21 BEGIN DECLARE SECTION
76 0 2 0 19 END DECLARE SECTION
275 0 14 0 32 WHENEVER SQLERROR GOTO ENDOFPROC
3366 1 9 1 26 EXECUTE IMMEDIATE :H00001
3416 2 0 1 40 VALUES CURRENT SQLID INTO :H00008
3418 3 0 1 32 SET CURRENT SQLID = :H00009
3437 4 0 1 34 SET CURRENT SQLID = :H00008
3459 5 0 1 40 VALUES CURRENT SQLID INTO :H00008
3461 6 0 1 32 SET CURRENT SQLID = :H00009
3547 7 11 1 24 PREPARE S1 FROM :H00001
3582 7 11 1 24 PREPARE S1 FROM :H00001
3598 8 0 1 34 SET CURRENT SQLID = :H00008
3616 0 5 0 21 BEGIN DECLARE SECTION
3619 0 2 0 19 END DECLARE SECTION
3652 7 9 4 56 EXECUTE S1 USING :H00004 , :H00015, :H00005, :H00006
3674 7 9 4 56 EXECUTE S1 USING :H00004 , :H00016, :H00005, :H00006
3720 9 0 1 41 VALUES CURRENT SCHEMA INTO :H00008
3774 10 0 1 33 SET CURRENT SCHEMA = :H00009
3977 11 11 1 32 PREPARE BLOBUPDATE FROM :H00001
4005 12 11 1 32 PREPARE CLOBUPDATE FROM :H00001
4248 13 0 1 86 SELECT NUM_DB_STORAGE_PATHS INTO :H00014 FR
OM TABLE(SNAP_GET_DB('', -1))
4416 14 0 1 35 SET CURRENT SCHEMA = :H00008
4475 11 9 3 58 EXECUTE BLOBUPDATE using :H00010 , :H00005, :H00012
4492 11 9 3 58 EXECUTE BLOBUPDATE using :H00010 , :H00005, :H00012
4551 12 9 3 59 EXECUTE CLOBUPDATE using :H00011 , :H00005, :H00013
4573 12 9 3 59 EXECUTE CLOBUPDATE using :H00011 , :H00005, :H00013
确切来说package都是用如C等嵌入式语言开发编译而来,所以在执行绑定操作,我们通常需要使用bind,因为bind命令需要传入bnd参数文件名称,如:
C:\Program Files (x86)\IBM\SQLLIB\bnd>db2 bind db2sampl_SQL.bnd