在 DB2 資料庫的日常維護與效能調優中,掌握核心檢測指令至關重要。本文整理了連線、重組檢查、快照監控、效能分析、統計資訊收集與索引管理等實用指令,供 DBA 及開發人員快速參考。
connect to [資料庫名] user [操作用戶名] using [密碼]
指令中請勿包含中括號,直接替換為實際值即可建立連線。
重組可回收空間並改善存取效能,執行前建議先檢查是否必要:
reorgchk on table EAS.T_User
檢測單一表格 EAS.T_User 是否需要重組。
reorgchk update statistics on table all
檢測所有表格並同步更新統計資訊。
select tabname, colcount, status FROM syscat.tables WHERE tabschema NOT LIKE 'SYS%' ORDER BY tabname
狀態欄位說明:N = 正常;C = 待審核。可快速掌握使用者表格健康狀態。
緩衝池命中率(建議 > 95%):
get snapshot for bufferpools on 資料庫名
或使用系統監視視圖:
select index_hit_ratio_percent from sysibmadm.bp_hitratio
鎖快照:
get snapshot for Locks on EASdb get snapshot for all on EASdb
select agent_id, rows_selected, rows_read from sysibmadm.application_performance
查看應用程式存取效能。
select elapsed_time_min, appl_status, agent_id from sysibmadm.long_running_sql order by elapsed_time_min desc fetch first 5 rows only
找出執行時間最長的前 5 條 SQL。
select * from sysibmadm.top_dynamic_sql order by num_executions desc
依執行次數排序,鎖定高頻 SQL 進行優化。
select tabschema, tabname from syscat.tables where stats_time is null
列出無統計資訊的表格。
select tabname from syscat.indexes where stats_time is null
列出無統計資訊的索引。
reorg table EAS.T_City
單表重組。
reorg indexes all for table 表名
僅重組索引。
runstats on table EAS.t_group and detailed indexes all
同步更新表格與所有索引的詳細統計資訊。
原則:大量刪除、修改、新增後,先 reorg table 再 runstats on table。
list applications show detail
顯示所有應用程式連線詳細資訊。
list tablespaces show detail
顯示所有表空間詳細資訊。
create unique index t_groupIdCODEname on eas.t_group (G_ID asc, G_CODE, G_NAME) allow reverse scans
建立支援反向掃描的唯一索引,利於排序查詢。
create index t_groupIdCODEname on eas.t_group (G_ID asc) include (G_CODE, G_NAME)
建立 INCLUDE 索引,涵蓋非鍵值欄位以避免回表。
create table eas.t1( c1 int, c2 double, c3 double generated always as (c1 + c2), c4 generated always as (case when c1 > c2 then 1 else null end) )
示範 GENERATED ALWAYS AS 語法建立衍生欄位。