db2 list tablespaces show detail
1.2查看数据库的表死锁
方法一:
打开监控 db2 update monitor switches using lock on
查看锁表 db2 get snapshot for locks on 数据库名称
db2 "force application (handle id)"
停止锁的进程
List Of Locks #在application handle紧跟后面出现list of locks表明该application handle 锁了表或对象
Lock Name = 0x030039020DFF11000000000052
Lock Attributes = 0x00000000
Release Flags = 0x00000004
Lock Count = 1
Hold Count = 0
Lock Object Name = 1163533 #被锁对象名称
Object Type = Row #被锁对象类型
Tablespace Name = tbs_data #被锁对象所在的表空间
Table Schema = DB2INST1
Table Name = t_mytable1 #被锁的表名
Mode = X
方法二:
使用select agent_id,tabname,lock_mode from table(snap_get_lock('dbname')) as aa
查看:进程hander,表名,锁模式
1.3查看和更改快照参数
db2 get monitor switches
监控开关 数据库管理器参数 注释
BUFFERPOOL DFT_MON_BUFPOOL 缓冲区的读写情况和发生时间
LOCK DFT_MON_LOCK 锁持有,锁等待,以及死锁的发生情况
SORT DFT_MON_SORT Heap的使用情况,排序性能
STATEMENT DFT_MON_STMT 语句起始时间,语句内容
TABLE DFT_MON_TABLE Measure of activity (rows read/written)
UOW DFT_MON_UOW Start/end times, completion status
TIMESTAMP DFT_MON_TIMESTAMP Timestamps
为了观察快照中的锁和执行语句情况,一般把LOCK和STATEMENT选项设为ON,也可以酌情把其他开关打开,示例如下: db2 update monitor switches using lock on statement on
CLP方式:
db2 get db cfg
在参数列表中寻找DLCHKTIME和LOCKTIMEOUT两个参数。
-DLCHKTIME 单位是毫秒,是DB2检查死锁的间隔时间,假设该值为10000ms,则意味着每隔10秒钟检查一下当前数据库中有无死锁存在,如有死锁,会选择回滚其中的某一个事务,让另外一个事务完成交易。
-LOCKTIMEOUT单位是秒,是锁等待最长时间,超过该时间仍未获得锁,则返回错误。
设置提示:
-缺省情况下,LOCKTIMEOUT是-1,意味着锁等待时间无限期,这和实际应用需求一般是不太相符的,需要将其值设为大于0的一个数。
-DLCHKTIME时间通常要设得比LOCKTIMEOUT时间小一些,否则未等发现死锁,就会被以锁等待超时而返回错误。
更改示例(CLP方式)
db2 update db cfg using locktimeout 10
CLP方式:
db2 list applications
或db2 list applications show detail
或 db2 list applications for database dbname [ show detail]
该命令可以查看当前是否有多个应用在连接着数据库,从而排查是否有并发的存在。
注意Application Name 和Application Id两栏,Application Name栏列出了应用的名字,db2bp通常意味着目前有CLP在连接数据库,java则意味着可能有db2cc或用户自己的java应用在连接数据库,在application Id栏中可以看到这些应用来自于哪些机器,本机的就显示为 LOCAL + 用户名 + 开始连接上的时间,远程的就会显示为16进制的IP地址+用户名+开始连接上的时间。通过排查并发应用从而消除测试中不必要的锁现象。
使用事件监控器,首先要选定所关注的事件类型,DB2中有很多事件类型,可以用于锁分析的通常会用到以下三种:
DEADLOCKS
DEADLOCKS
WITH DETAILS
STATEMENTS
步骤:
-创建事件监控器
create event monitor evmname for eventtype write to file ‘directory’
例:create event monitor mymonitor for deadlocks, statements
write to file ‘c: emp’
-把事件监控器打开
接上例:
Db2 “set event monitor mymonitor state 1”
注:1为打开,0为关闭
事件监控器开始工作,当所有应用断掉连接后,将事件记录下来
-格式化监控器的文件
db2evmon –db dbname -evm mymonitor >snap.txt
-察看监控器的文件
More snap.txt
Select stmt_text ,(stop_time-start_time) from stmt_ monitor_name Where stmt_operation not in (7,8,9,19) order by decimal(stop_time-start_time) desc fetch first 10 rows only
Select distinct(stmt_text),count(*) Count from stmt_monitor_name Where stmt_operation not in (7,8,9,19) group by stmt_text order by count(*) desc fetch first 10 rows only
Select stmt_text ,user_cpu_time from stmt_monitor_name where stmt_operation not in (7,8,9,19) order by user_cpu_time desc fetch first 10 rows only
1.6.4查看总排序时间降序排列排序时间最长的SQL
Select stmt_text,total_sort_time from stmt_monitor_name where stmt_operation not in (7,8,9,19) order by decimal(total_sort_time) desc fetch first 10 rows only
1.7查看表上的索引
describe indexes for table 表名
list history dropped table all for 数据库名 得到删除表的tableid
db2move databasename export -sn 模块名 –tn 表名 -u db2admin -p *****
db2look -d databasename -e -a -o db2look_htdctr.sql
db2move test import -u 用户名 -p 密码
1.10编目数据库
db2set DB2_CREATE_DB_ON_PATH=YES
CATALOG TCPIP NODE Server51
REMOTE IP_Address
SERVER 51000
REMOTE_INSTANCE DB2
OSTYPE NT;
1.11查询SQLCODE
Db2 ? 22001
Db2 ? sql-204
1.12 备份数据库
备份表空间
Db2 “backup database databasename” tablespace tablespaceName to /path
Db2 backup database dbname to /path
db2 connect to sample
db2 quiesce db immediate force connections
db2 terminate
db2 deactivate db sample
db2 connect to sample
db2 unquiesce db
C:\WINDOWS\SYSTEM32\DRIVERS\ETC\services
db2 get dbm cfg 查看SVCENAME名称
修改db2端口
update dbm cfg using svcename 50000
1.15查看数据库
db2 list db directory
1.16插入空值到表中
import from /dev/null of del replace into db2inst1.表名
1.17建立nickname
1、在db2命令窗口下,运行connect to 目标库 user 用户名 using 密码
2、显示所有对象 list tables
3、建立nickname create nickname nickname的名字 for server名.schma名.table名
查看记录
select * from table fetch first 10 rows only
1.19查看db2实例
Db2ilist
db2 "CREATE DATABASE MIBSSE AUTOMATIC STORAGE YES ON '/db2/xbank2' DBPATH ON '/db2/xbank2' USING CODESET UTF-8 TERRITORY CN COLLATE USING SYSTEM PAGESIZE 4096"
1.22查看BufferPool
db2 "select * from syscat.bufferpools"
1.23单表导入导出
db2 Export to "F:\fund\FssProduct.txt" of del select * from tablename
db2 import from “F:\fund\FssProduct.txt” of del messages “d:\msgs.txt” insert into tablename
1.24 数据库所有快照
db2 get snapshot for dynamic sql on mibsse >snap.out
通过Linux命令查出所有的执行最大次数
grep -n "Number of executions" snap.out |grep -v "= 0" |sort -k 5,5rn|more
1.25查看临时表空间使用情况
db2 get snapshot for tables on mibsse >snap2.out
db2 get snapshot for application agented <agent ID>
1.26通过性能管理视图监控数据库
db2 list tables for schema sysibmadm;
或在db2客户端察看视图
--需要开启BUFFERPOOL监控
db2 update dbm cfg using DFT_MON_BUFPOOL on
Select substr(bp_name,1,30) as bp_name,data_hit_ratio_percent,index_hit_ratio_percent,total_hit_ratio_percent
from sysibmadm.bp_hitratio where bp_name not like 'IBMSYSTEM%';
1.26.2监控Package Cache 大小
with dbcfg1 as (select int(value) as pckcachesz from sysibmadm.dbcfg where name ='pckcachesz')
Select pckcachesz as package_Cache_Size ,pkg_cache_lookups as lookups, pkg_cache_inserts asinserts,pkg_cache_num_overflows as overflows, 100*pkg_cache_size_top/(pckcachesz * 4096) as pkg_Cache_alloc
from dbcfg1,sysibmadm.snapdb
Select agent_id ,rows_selected,rows_read from sysibmadm.APPLICATION_PERFORMANCE[Z1] ;
1.26.4监控允许最长的SQL
Select appl_name,elapsed_time_min as elapsedmin,appl_status as status,authid as auth_id,inbound_comm_address asip_address,stmt_text as SQL_statement
from sysibmadm.long_running_sql order by 2 desc
Select num_executions, average_execution_time_s,prep_time_ms,prep_time_percent, stmt_text
from sysibmadm.query_prep_cost
where average_execution_time_s>0 order by prep_time_percent des
select "NUM_EXECUTIONS", "AVERAGE_EXECUTION_TIME_S",
"STMT_SORTS", "SORTS_PER_EXECUTION", "STMT_TEXT", "DBPARTITIONNUM"
from "SYSIBMADM"."TOP_DYNAMIC_SQL"
where NUM_EXECUTIONS>0
order by 1 desc fetch first 5 rows only;
select "NUM_EXECUTIONS", "AVERAGE_EXECUTION_TIME_S",
"STMT_SORTS", "SORTS_PER_EXECUTION", "STMT_TEXT", "DBPARTITIONNUM"
from "SYSIBMADM"."TOP_DYNAMIC_SQL"
where NUM_EXECUTIONS>0
order by STMT_SORTS desc fetch first 5 rows only
select ai.appl_name,ai.primary_auth_id,ap.lock_waits,ap.lock_wait_time/1000 as "Total Wait (s)",(ap.lock_wait_time/ap.lock_waitsfont-siz