DB2常用指令碼整理
--資料表空間擴容
ALTER TABLESPACE BAS_DW_ENT
ADD (Device '/dev/vx/rdsk/n01datadg3/dwentlv_100g_01_01' 3276800) ON DBPARTITIONNUM (1)
ADD (Device '/dev/vx/rdsk/n02datadg3/dwentlv_100g_02_02' 3276800) ON DBPARTITIONNUM (2)
ADD (Device '/dev/vx/rdsk/n03datadg3/dwentlv_100g_03_03' 3276800) ON DBPARTITIONNUM (3)
ADD (Device '/dev/vx/rdsk/n04datadg3/dwentlv_100g_04_04' 3276800) ON DBPARTITIONNUM (4)
ADD (Device '/dev/vx/rdsk/n05datadg3/dwentlv_100g_05_05' 3276800) ON DBPARTITIONNUM (5)
ADD (Device '/dev/vx/rdsk/n06datadg3/dwentlv_100g_06_06' 3276800) ON DBPARTITIONNUM (6)
ADD (Device '/dev/vx/rdsk/n07datadg3/dwentlv_100g_07_07' 3276800) ON DBPARTITIONNUM (7)
ADD (Device '/dev/vx/rdsk/n08datadg3/dwentlv_100g_08_08' 3276800) ON DBPARTITIONNUM (8)
ADD (Device '/dev/vx/rdsk/n09datadg3/dwentlv_100g_09_09' 3276800) ON DBPARTITIONNUM (9)
ADD (Device '/dev/vx/rdsk/n10datadg3/dwentlv_100g_10_10' 3276800) ON DBPARTITIONNUM (10)
ADD (Device '/dev/vx/rdsk/n11datadg3/dwentlv_100g_11_11' 3276800) ON DBPARTITIONNUM (11)
ADD (Device '/dev/vx/rdsk/n12datadg3/dwentlv_100g_12_12' 3276800) ON DBPARTITIONNUM (12)
ADD (Device '/dev/vx/rdsk/n13datadg3/dwentlv_100g_13_13' 3276800) ON DBPARTITIONNUM (13)
ADD (Device '/dev/vx/rdsk/n14datadg3/dwentlv_100g_14_14' 3276800) ON DBPARTITIONNUM (14);
db2 list tablespaces show detail;
db2 list tablespace containers for 42
--資料表空間擴容
ALTER TABLESPACE BAS_MK_APP
ADD (Device '/dev/n0datavg6/rmkapplv_100g_0_01/' 3276800) ON DBPARTITIONNUM (0)
ADD (Device '/dev/n1datavg6/rmkapplv_100g_1_02/' 3276800) ON DBPARTITIONNUM (1)
ADD (Device '/dev/n2datavg6/rmkapplv_100g_2_03/' 3276800) ON DBPARTITIONNUM (2)
ADD (Device '/dev/n3datavg6/rmkapplv_100g_3_04/' 3276800) ON DBPARTITIONNUM (3);
--查看錶空間使用方式
db2 "select substr(tbsp_name,1,20) tbsp_name,tbsp_type,substr(tbsp_state,1,10) tbsp_state,tbsp_total_size_kb/1024 tot_size,
tbsp_free_size_kb/1024 free_size,tbsp_utilization_percent used_pect,TBSP_USING_AUTO_STORAGE,TBSP_PAGE_SIZE/1024 PGSIZE,DBPARTITIONNUM
from sysibmadm.tbsp_utilization where tbsp_total_size_kb/1024>1024 and tbsp_utilization_percent>90 order by DBPARTITIONNUM with ur"
--檢查資料表空間狀態
db2 "select tbsp_id,substr(tbsp_name,1,30) tbsp_name,substr(tbsp_state,1,10) tbsp_state,tbsp_utilization_percent,dbpartitionnum from sysibmadm.tbsp_utilization with ur"
db2 list tablespaces show detail --在單分區上查看錶空間的狀態,正常返回0x0000
db2_all "db2 list tablespaces show detail" --在所有分區上查看錶空間的狀態
db2tbst 0x0000
State = Normal
--查看錶空間資訊
db2 list tablespaces
show detail
db2 list tablespace containers for 0
--查看DMS資料表空間中是否還有可用頁
$db2 list tablespaces show detail --在單分區上查看錶空間的是否還有可用頁
$ db2_all ";db2 connect to qhbidb;db2 list tablespaces show detail" --在所有分區上查看錶空間是否還有可用頁
--擷取資料表空間的高水位資訊
db2 "SELECT varchar(tbsp_name, 16) as tbsp_name,RECLAIMABLE_SPACE_ENABLED,TBSP_USED_PAGES,
TBSP_FREE_PAGES, TBSP_PAGE_TOP from TABLE (MON_GET_TABLESPACE('',-2)) AS t
where t.TBSP_PAGE_TOP > t.TBSP_USED_PAGES"
>>-ALTER TABLESPACE--tablespace-name---------------------------->
>----+-REDUCE--+-------------------------------+--+-----------------------------+-+
+-
database-container-clause
-+ '-
on-db-partitions-clause
-'
+-
all-containers-clause
-----+
+-MAX---------------------------+
+-STOP--------------------------+
'-integer--+---------+----------'
+-K-------+
+-M-------+
+-G-------+
'-PERCENT-'
+-LOWER HIGH WATER MARK--+------+--------------------------------------------+
'-STOP-'
'----------------------------------------------------------------------------'
展示了與回收資料表空間儲存空間相關的兩個子句及其文法。其中MAX參數可以指定最大限度的降低高水位標記來釋放空間。在運行REDUCE命令後,在資料區塊移動的過程中,
也可以使用STOP參數來停止資料表空間移動。如果是DMS資料表空間,則需要先運行LOWER HIGH WATER MARK子句降低高水位標記,再運行REDUCE子句釋放資料表空間。
如果是啟用了自動儲存的資料表空間,直接運行REDUCE子句即可。
清單 7. 自動儲存資料表空間
ALTER TABLESPACE REDUCE 10 M清單 8. DMS 資料表空間
ALTER TABLESPACE LOWER HIGH WATER MARK
ALTER TABLESPACE REDUCE (ALL CONTAINERS 10 M)
--資料庫大小
db2 "call get_dbsize_info(?,?,?,-1)"
--資料庫load曆史資訊檔清理
--建議定期對/dbhome/qhbiinst/NODE0000/SQL00001/db2rhist.asc清理,該檔案過大會導致LOAD操作是存在效能問題。
--例如:清除節點1,2014年10月之前的曆史檔案記錄
export DB2NODE=1
db2 terminate
db2 connect to qhbidb
db2 prune history 201410
--故障日誌抓取
cd /db2diaglog/db2dump
--將FODC_Trap_YYYY_MM_DD-HH.MI.SS.MS檔案夾全部內容取下來
db2diag -t 2014-11-30-23.00.00:2014-12-01-03.00.00 >diag_201441201.log
db2diag -t 2015-8-18 -level "Severe,Error"
--查看load曆史檔案大小
du -sm /db2data/qhbiinst/NODE0000/SQL00001/db2rhist*
awk '{a+= $0}END{print a"MB"}'
du -sm /db2data/qhbiinst/NODE000[1-8]/SQL00001/db2rhist*
awk '{a+= $0}END{print a"MB"}'
du -sm /db2data/qhbiinst/NODE000[9-16]/SQL00001/db2rhist*
awk '{a+= $0}END{print a"MB"}'
du -sm /db2data/qhbiinst/NODE000[17-24]/SQL00001/db2rhist*
awk '{a+= $0}END{print a"MB"}'
du -sm /db2data/qhbiinst/NODE000[1-8]/SQL00001/db2rhist*
awk '{a+= $0}END{print a"MB"}'
du -sm /db2data/qhbiinst/NODE000[1-8]/SQL00001/db2rhist*
awk '{a+= $0}END{print a"MB"}'
--檢查各個點資料庫的運行狀態
$db2_all "db2pd –db qhbidb -"
--如果部分節點狀態不正常,可以通過如下方式去啟動資料庫:
$db2start dbpartitionnum nodenum
--查看錶的狀態
db2 "select tabname, colcount, status from syscat.tables where tabschema not like 'SYS%' order by tabname"
db2 load query table test
--DB2狀態監控
$db2_ps
$db2gcf -u -p 0 -i qhbiinst
Instance : qhbiinst
DB2 Start : Success
Partition 0 : Success
db2 deactivate database 資料庫名稱 -------------去啟用資料庫
db2 activate database 資料庫名稱 -------------啟用資料庫
connect reset -------------斷開當前資料庫連接
db2 restart database databasename -------------重新啟動資料庫
--兩種可以備份方式
---離線資料庫備份,需要至資料庫的獨佔串連,它將備份資料庫中的所有資料表空間;
backup db sample to c:\backup with 3 buffers buffer 1000 without prompting
---線上資料庫備份,可以備份整個資料庫或者單個資料表空間。
backup db sample tablespace (userspace1) online to /dev/rmt0 without prompting
--要執行聯機備份,確保資料庫參數 logretain = on 或 設定了logarchmetd1支援聯機備份
--查看倉庫的transaction
db2pd -d qhbidb -transactions
db2pd -d qhbidb -alldbp -apinfo 59898(applid)
ps -ef
grep "Application PID"
--停止資料庫
db2 force application all
db2 terminate
db2stop
db2start
db2_all "db2 connect to qhbidb"
--經使用db2 activate db qhbidb,再使用db2_all "db2 connect to qhbidb"查看錶是否可以查詢。
--查看當前節點復原情況
db2pd -d qhbidb -reco
--查看當前主機所有分區的復原情況
db2pd -d qhbidb -reco -alldbp
db2_all "db2 list utilities show detail"
--查看不確定事務
export DB2NODE=0
db2 terminate
db2 connect to qhbidb
db2 values current dbpartitionnum
db2 list indoubt transactions
--重組表檢查
db2 "call reorgchk_tb_stats('T','ALL')"
--重組索引檢查
db2 "call reorgchk_ix_stats('T','ALL')"
--統計資訊檢查
db2 "select date(STATS_TIME),count(1) from syscat.tables where type='T' group by date(STATS_TIME) with ur"
--緩衝池命中率
db2 "select * from sysibmadm.bp_hitratio"
db2 get snapshot for bufferpools on qhbidb global
--鎖定擴大、鎖等待
db2 "select lock_waits,deadlocks,lock_escals,lock_timeouts,dbpartitionnum from sysibmadm.snapdb"
--排序溢出
db2 "select total_sorts,sort_overflows,dbpartitionnum from sysibmadm.snapdb"
--SQL語句分析
db2 "select * from sysibmadm.snapdyn_sql"
--表狀態檢查
db2 "select substr(tabschema,1,10) tabschema,substr(tabname,1,30) tabname,status,type from syscat.tables where status<>'N'"
--有效索引讀
db2 "select rows_read/(rows_selected+1),dbpartitionnum as IREF from sysibmadm.snapdb"
--平均結果集大小
db2 "select rows_selected/(select_sql_stmts+1) as avg_result_set,dbpartitionnum from sysibmadm.snapdb"
--同步讀取比例
db2 "select 100-(((pool_async_data_reads+pool_async_index_reads*100)/(pool_index_p_reads+1)) as SRP from sysibmadm.snapdb where DB_NAME='ODSDB'"
--髒頁偷取
db2 "select pool_dirty_pg_steal_clns from sysibmadm.snapdb"
--緩衝區讀寫IO回應時間
db2 "select tbsp_name,(pool_read_time/(pool_data_p_reads+pool_index_p_reads+pool_temp_data_p_reads+pool_temp_index_p_reads+1)) as tsorms from sysibmadm.snaptbsp
order by tsorms desc fetch first 10 rows only"
--平均每條交易的排序次數
db2 "select total_sorts/(commit_sql_stmts+rollback_sql_stmts),dbpartitionnum from sysibmadm.snapdb"
--總的事務數量
db2 "select commit_sql_stmts + rollback_sql_stmts,dbpartitionnum from sysibmadm.snapdb"
--每個事務包括查詢SQL的數量
db2 "select select_sql_stmts,(commit_sql_stmts + rollback_sql_stmts),dbpartitionnum from sysibmadm.snapdb"
--每個事務包括增刪改的語句數量
db2 "select uid_sql_stmts,(commit_sql_stmts + rollback_sql_stmts),dbpartitionnum from sysibmadm.snapdb"
--每個事務返回的結果集行數
db2 "select row_selected,(commit_sql_stmts + rollback_sql_stmts),dbpartitionnum from sysibmadm.snapdb"
--每個事務返回讀的行數
db2 "select rows_read,(commit_sql_stmts + rollback_sql_stmts),dbpartitionnum from sysibmadm.snapdb"
--檢查Package狀態
db2 "select valid,count(1) from syscat.packages group by valid with ur"
--監控表的使用方式
select substr(tabname,1,50) tabname,count(*)
from sysibmadm.snaptab a,syscat.tables b
where a.tabschema=b.tabschema
and a.tabname=b.tabname
and b.tbspace like 'TBS_AGG'
group by substr(a.tabname,1,50) having count(*)=1;
--查看錶所屬的資料表空間
db2 "SELECT TABLES.TABSCHEMA, TABLES.TABNAME, TBSPACES.TBSPACE FROM SYSCAT.TABLES AS TABLES, SYSCAT.TABLESPACES AS TBSPACES
WHERE TABLES.TBSPACEID = TBSPACES.TBSPACEID AND TABNAME = 'SALES'"
-- 尋找預存程序所對應的程式包的名稱
SELECT
CHAR(PROCSCHEMA,20) AS PROCSCHEMA, -- 模式名稱
CHAR(PROCNAME,20) AS PROCNAME, -- 預存程序名稱
CHAR(B.BNAME,20) AS PKGNAME -- 綁定包名稱
FROM SYSCAT.PROCEDURES A
JOIN SYSCAT.ROUTINEDEP B ON A.SPECIFICNAME = B.ROUTINENAME
WHERE PROCSCHEMA='KF2' AND PROCNAME='EXPLAINPLAN_TEST' WITH UR;
--查看錶的相關資訊
db2 reorgchk update statistics on table NWH.CUST_BLACK
db2_all ";db2 connect to qhbidb;db2 reorg table NWH.CUST_BLACK"
本文永久更新連結地址: