DB2 統計資訊與重組相關操作

fjzcau發表於2015-03-28

--收集表和索引的統計資訊,包括資料分佈
runstats on table on all columns with distribution and detailed indexes all

--收集表和索引的統計資訊
runstats on table for indexes all
db2 "runstats on table db2inst1.t1 on all columns and indexes all"

--使用伯努利演算法抽樣統計,適用於資料倉儲,下面是伯努利演算法10%的抽樣統計
db2 "runstats on table tablespace bernoulli(10) "

--抽樣統計資訊
db2 "runstats on table db2inst1.table1 and indexes all tablesample system (10)"

--This command collects statistics on the table db2user.employee with distribution statistics on only 30 percent of the rows.
db2 "runstats on table db2user.employee with distribution tablesmple bernoulli(30)"

If you want to collect the same statistics repeatedly on a table and do not want to retype the command options,
you simply specify the use profile option.

db2 "runstats on table db2user.employee use profile
"
This command collects statistics on db2user.employee using the options saved in the statistics profile for that table.

db2 "runstats on table db2user.employee
with distribution on columns (empid, empname)
set profile only
"
Notice that the option will only set the profile; the runstats command will not run.
If you need to modify a previously registered statistics profile, use the update profile only
option. Similarly, this option will only update the profile without running the runstats
command. If you want to update the profile as well as update the statistics, use the update profile option instead.

db2 "runstats on table db2user.employee
with distribution default
num_freqvalues 50 num_quantiles 50
update profile
"

Starting in DB2 9, automatic statistics collection allows DB2 to automatically run the runstats
utility in the background to ensure the most current database statistics are available.
The automatic statistics collection feature is enabled by default for any new database created in DB2 9.
You can disable it by explicitly setting the AUTO_RUNSTATS database configuration parameter to OFF.
In order to minimize the performance impact of automatic statistics collection,
throttling of the runstats utility can be used to limit the amount of resources consumed by
the utility. When the database activity is low, the utility runs more aggressively.
On the other hand, when the database activity increases,
the resources allocated to executing runstats are reduced.
Here is how to specify the level of throttling.

db2 "runstats on table db2user.employee
with distribution default
num_freqvalues 50 num_quantiles 50
util_impact_priority 30
"

The acceptable priority value ranges from 1 to 100. The highest priority (meaning unthrottled) is
represented by the value 100 and the value 1 represents the lowest priority. Default priority level is 50.

--runstats 選項
allow write access 預設選項

--檢視錶是否收集過統計資訊
db2 "select char(tabname,40) as tabname, stats_time from syscat.tables where stats_time is null"

--檢視索引是否收集過統計資訊
db2 "select char(indname,30) as indexname,char(tabname,30) as "TabName",nleaf,nlevels,stats_time 
 from syscat.indexes"

db2 -v "reorgchk update statistics on table all"


 --什麼時候需要執行 runstats
Some situations when updating statistics would be beneficial
– After data has been loaded into a table and appropriate indexes have been created
– After creating a new index on a table
– After a table has been reorganized with the REORG utility
– After a table and its indexes have been significantly modified through update, insert, or delete operations

Improving the performance of RUNSTATS
– Collect basic statistics only for relevant columns. E.g.: columns used to join tables
– Consider specifying only those columns for which data distribution  statistics should be collected. 
– Use row-level or page-level sampling when running RUNSTATS
– Use throttling option to limit the performance impact of RUNSTATS  execution

--收集全庫的統計資訊指令碼
#----------------------------------------------------------------------------#
#!/bin/ksh93

if [ "$#" < 3 ] ; then
  echo "Usage:$0 DB_NAME DB_USER USER_PASSWORD"
  exit 1
if

DB=$1
DB_USER=$2
DB_PWD=$3

db2 connect to $DB user $DB_USER using $DB_PWD
db2 "select rtrim('RUNSTATS ON TABLE ' || rtrim(tabschema) || '.' || tabname || ' on all columns with distribution on all columns and sampled detailed indexes all allow write access;')
from syscat.tables
where type='T' " > createRunstats.txt
grep -i runstats  createRunstats.txt > runstats_detailed.sql
db2 -tvf runstats_detailed.sql

#----------------------------------------------------------------------------#

runstats 最佳實踐:
1)當表資料量發生了很大變化, 如 load 或 reorg,新增索引後,需要收集統計資訊
2)空閒時執行 runstats
3)對大表的某些欄位做統計資訊收集,並可以做抽樣統計
4)系統表也需要做 runstats
5)表和索引要同時收集統計資訊,並重新 rebind

以下情況 reorg 表,可能會提高效能:
1)表中許多行被刪除,reorg後佔用空間會降低
2)發生行溢位的的表,主要是 varchar 變長欄位的表
3)按照某個索引重新組織表資料的物理順序,如聚集索引
4)啟用表資料壓縮時,可透過 reorg 建立字典表,並對錶資料進行壓縮

以下情況 reorg 索引,可能會提高效能:
1)表資料刪除後,導致很多索引頁變為空
2)減少索引層次
3)去除偽刪除的行和頁


reorg 最佳實踐:


--判斷表或索引是否需要重組?
1)reorgchk
F1,F2,F3有*標記,則表需要重組
F4-F8有*標記,則索引需要重組

db2 reorgchk on schema db2inst1

2)sysibmadm.snaptab 檢視 針對表多
overflow_accesses/rows_read > %3,則表需要重組
例項監控器開關: update dbm cfg using dft_mon_table on
db2 "select  substr(tabname,1,18) tabname,rows_read, overflow_accesses
  from sysibmadm.snaptab
  where (rows_read>999) and ((overflow_accesses*100)/(rows_read+1) > 3)
"

--離線reorg表
支援 allow read access(預設),allow no access
use 選項指定臨時表空間,進行重組

*4個階段
1)scan-sort 根據reorg指定的索引對錶進行掃描,排序
2)build 表資料構建
3)replace(copy):替換原有資料
4)index rebuild:基於新資料,重建索引

離線reorg,根據index index-name 選項指定根據哪個索引進行重組,如果沒有指定索引,表重組時不關心資料在物理儲存的順序。
如果表上有cluster index ,即使沒有指定索引,預設也按聚集索引順序重組表。

--離線reorg example
db2 reorg table db2inst1.t1
b2 create temporary tablespace tempts1
db2 "create index i1 on db2inst1.t1(id)"
db2 reorg table db2inst1.t1 index i1 allow read access use tempts1


--檢視 reorg 的過程
1)sysibmadm.snaptab
db2 "select
       substr(tabname, 1, 15) as tab_name,
       substr(tabschema, 1, 15) as tab_schema,
       reorg_phase,
       substr(reorg_type, 1, 20) as reorg_type,
       reorg_status,
       reorg_completion,
       dbpartitionnum
     from sysibmadm.snaptab_reorg
     order by dbpartitionnum"
2)透過快照
db2 get snapshot for tables on testdb
current counter / max counter 來預測完成的百分比
3)db2pd -d testdb -reorg
4)db2 list history reorg all for testdb


--檢視 reorg 程式 
db2 list applications show detail | grep -i db2reorg

--線上reorg表,只是維護索引,不會重建索引(聚集索引除外)
db2 reorg table db2inst1.t1 inplace allow write access

--線上重組索引
db2 reorg indexes all for table db2inst1.t1

db2 list history reorg all for testdb

檢視db2diag.log


--檢視包
db2 list packages for schema db2inst1

--對單個包重繫結
db2 rebind package package-name

--對所有包重繫結
db2rbind testdb  -l db2rbind.log all

--刪除程式包緩衝區 
db2 flush package cache dynamic 

--檢視模式 db2inst1 下無效的包
db2 "select pkgname,valid from syscat.packages where pkgschema='DB2INST1' and valid != 'Y' "



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22661144/viewspace-1477207/,如需轉載,請註明出處,否則將追究法律責任。

相關文章