-- 匯出乾淨資料
export to tempfile of del select * from TABLENAME where not 清理條件;
-- 非恢復模式高速載入,替換現有資料,忽略換行符問題
load from tempfile of del modified by delprioritychar replace into TABLENAME nonrecoverable;
delprioritychar:防止資料含換行符導致匯入失敗。replace into:截斷表並重新載入。nonrecoverable:不記錄日誌,速度最快,事後需完整備份。export 可提升整體效率。-- 列出所有應用
db2 list applications | grep DBNAME
-- 強制斷開指定應用 (應用代理 ID 為 123, 456)
db2 "force application (123, 456)"
-- 查看特定應用代理執行狀態
db2 get snapshot for application agentid 299 | grep Row
-- 查看所有配置
db2 get db cfg for [dbname]
-- 修改單一參數 (如 LOGFILSIZ)
db2 update db cfg for [dbname] using LOGFILSIZ 4096
-- 正常停止 (若有活動連線會失敗)
db2stop
-- 強制停止 (終止所有連線)
db2stop force
-- 啟動
db2start
遇到 SQL1025N (資料庫仍作用中) 時必須用 force。
-- 調整預設緩衝池大小 (單位: 頁)
db2 alter bufferpool ibmdefaultbp size 10240
-- 查看緩衝池狀態
db2 "select * from syscat.bufferpools"
-- 歸檔模式 (支援線上備份)
db2 update db cfg for [dbname] using LOGRETAIN ON
-- 修改後資料庫進入 BACKUP PENDING,需離線備份才能恢復正常
-- 循環模式 (僅離線備份)
db2 update db cfg for [dbname] using LOGRETAIN OFF
-- 1. 確認日誌目錄與第一個作用中日誌
db2 get db cfg for [dbname] # 看 "Path to log files" 與 "First active log file"
-- 2. 備份欲刪除的歸檔日誌
-- 3. 以實例擁有者執行 (假設第一作用中日誌為 S0015913.LOG)
db2 connect to [dbname]
db2 prune logfile prior to S0015913.LOG
嚴禁 直接用 rm 刪除日誌檔,會導致資料庫損壞。
# 1. 確認 DIAGPATH
db2 get dbm cfg | grep DIAGPATH
# 2. 停止應用與 DB2 實例
# 3. 備份舊日誌
# 4. 刪除 db2diag.log,DB2 會自動建立新檔
-- 強行中斷 LOAD 導致表處於 "Load Pending" (SQL0668N reason code 3)
-- 終止 LOAD 並釋放表鎖
db2 "load from /dev/null of del terminate into TABLENAME nonrecoverable"
-- 若未用 nonrecoverable,檢查表空間狀態
db2 list tablespaces show detail
-- 非正常狀態需離線備份恢復
-- 多欄位關聯更新
db2 "update A set bal = (select txnamt from B where A.actno=B.actno and A.cnlno=B.cnlno)
where A.actno||A.cnlno in (select actno||cnlno from B)"
-- LIKE 模糊查詢
db2 "select * from A where actno like 'B_AAA%'"
db2 "select * from A where actno like '%-AAA%'"
-- 重導向恢復範例
restore db db1 to /tstdb2/catalog into db newlogpath /tstdb2/db2log buffer 2048
replace existing redirect parallelism 16;
set tablespace containers for 1 using (path '/tstdb2/db2tmp');
set tablespace containers for 2 using
(device '/dev/rtstcontlv00' 2621440, ...);
restore db db1 continue;
-- 恢復完成後若報 ROLL-FORWARD PENDING
db2 rollforward db db1 to end of logs and complete