首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> DB2 資料庫 >> DB2 教學

DB2 資料庫日常實戰操作指南:15 個高頻維護場景與指令範例

     編輯:DB2 教學

資料裝載與匯入

1. LOAD 高效裝載

export to tempfile of del select * from TABLENAME where not 清理條件;
load from tempfile of del modified by delprioritychar replace into TABLENAME nonrecoverable;
  • delprioritychar:防止換行符導入失敗。
  • replace:覆蓋現有資料。
  • nonrecoverable:無日誌模式,速度最快。

2. LOAD 中斷恢復

db2 "load from /dev/null of del terminate into TABLENAME nonrecoverable"

若未用 nonrecoverable,檢查表空間狀態:db2 list tablespaces show detail,必要時離線備份恢復正常。

應用程式管理

3. 查詢當前連接

db2 list application | grep DBNAME

4. 強制斷開應用

db2 "force application (appl_handle1, appl_handle2)"

5. 檢視應用執行狀態

db2 get snapshot for application agentid 299 | grep Row

資料庫配置與日誌管理

6. 參數查看與修改

db2 get db cfg for [dbname]
db2 update db cfg using <參數名> <參數值>

7. 強制停止資料庫管理器

db2stop force

解決 SQL1025N 錯誤,隨後 db2start 重啟。

8. 緩衝池調整

db2 alter bufferpool ibmdefaultbp size 10240
db2 "select * from syscat.bufferpools"

9. 日誌模式切換

  • 歸檔模式(支援線上備份):db2 update db cfg for [db] using logretain on → 需立即離線備份解除 BACKUP PENDING。
  • 循環模式:db2 update db cfg for [db] using logretain off。

10. 安全清理歸檔日誌

  1. 確認應用與 DB2 已停止。
  2. db2 get db cfg for [db] 查看 Path to log files 與 First active log file。
  3. 備份欲刪除日誌。
  4. 以實例用戶執行:db2 prune logfile prior to S0015913.LOG。

11. 清理 db2diag.log

  1. 確認應用與 DB2 已停止。
  2. 備份舊檔(路徑由 db2 get dbm cfg 中 DIAGPATH 確定)。
  3. 刪除檔案,DB2 會自動建立新檔。

SQL 進階操作

12. 跨表 UPDATE

db2 "update cdmcrd set offset = (select cdmlsl.offset from cdmlsl where cdmlsl.crdno=cdmcrd.crdno) where cdmcrd.crdno in (select cdmlsl.crdno from cdmlsl)"

13. 多欄位條件更新

db2 "update A set bal=(select txnamt from B where actno=A.actno and cnlno=A.cnlno) where A.actno||A.cnlno in (select actno||cnlno from B)"

14. 模糊查詢技巧

db2 "select * from A where actno like 'B_AAA%'"
db2 "select * from A where actno like '%-AAA%'"

資料庫恢復實戰

15. 重導向恢復與前滾

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, device '/dev/rtstcontlv01' 2621440, device '/dev/rtstcontlv02' 2621440, device '/dev/rtstcontlv03' 2621440);
restore db db1 continue;

恢復完成後若報 ROLL-FORWARD PENDING,執行:

db2 rollforward db db1 to end of logs and complete
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved