使用DB2優化概要強制修改DB2的執行計畫

2021-08-02 04:44:04 字數 3903 閱讀 2580

db2中sql語句的執行計畫是db2優化器根據統計資訊來制定的,有的時候,需要人工干預,比如需要強制使用索引掃瞄。這時候可以使用優化概要來實現。下面的例子中,sql原本使用了表掃瞄,在使用了優化概要強制規定使用索引掃瞄之後,執行計畫變成了索引掃瞄。

$ db2level

informational tokens are "db2 v10.5.0.5", "s141128", "ip23626", and fix pack "5".

product is installed at "/opt/ibm/db2/v10.5.5".

新建一張分割槽表,並建立非分割槽索引,發現sql語句的訪問計畫並未使用索引掃瞄,而是使用了tbscan

$ db2 connect to sample

$ db2 "create tablespace tbs1"

$ db2 "create tablespace tbs2"

$ db2 "create tablespace tbs3"

$ db2 "create table t1 (id integer , name char(20) ) partition by range(id) (part p1 starting(1) ending(40) in tbs1, part p2 starting(41) ending(80) in tbs2, part p3 starting(81) ending(120) in tbs3)"

$ db2 "create index idx1 on t1 (id) not partitioned"

$ db2 "insert into t1 values(1,'a'),(2,'b'),(41,'c'),(42,'d'),(81,'e'),(82,'f')"

$ db2 "runstats on table t1 and indexes all"

$ db2 -tvf $home/sqllib/misc/explain.ddl

$ db2 "set current explain mode explain"

$ db2 "select name from t1 where id = 42"

$ db2 "set current explain mode no"

$ db2exfmt -d sample -g tic -w -1 -n % -s % -# 0 -o output1.txt

執行計畫如下:

$ cat output1.txt

original statement:

------------------

select

name

from

t1 where

id = 42

optimized statement:

-------------------

select

q1.name as "name"

from

e105q5a.t1 as q1

where

(q1.id = 42)

access plan:

-----------

total cost: 6.81003

query degree: 1

rows

return

( 1)

cost

i/o |1

tbscan

( 2)

6.81003 1 |

6 dp-table: e105q5a

t1q1

需要先建立乙個表systools.opt_profile。還要有乙個xml檔案:a1.xml中的e105q5a是例項名,也是預設的模式名。optguidelines規定表t1使用索引掃瞄,使用的索引為idx1

$ db2 "create table systools.opt_profile (schema varchar(128) not null, name varchar(128) not null, profile blob (2m) not null, primary key (schema, name))"

$ cat a1.xml

<?xml version="1.0" encoding="utf-8"?>

$ cat insert.del

"e105q5a", "prof1", "a1.xml"

$ db2 "import from insert.del of del modified by lobsinfile insert into systools.opt_profile"

$ db2set db2_optprofile=yes

$ db2 terminate

$ db2stop

$ db2start

$ db2 connect to sample

$ db2 set current optimization profile='prof1'

$ db2 set current explain mode explain

$ db2 "select name from t1 where id = 42"

$ db2 set current explain mode no

$ db2exfmt -d sample -g tic -w -1 -n % -s % -# 0 -o output2.txt

再次檢視執行計畫,發現使用了索引掃瞄:

$ cat output2.txt

profile information:

--------------------

opt_prof: (optimization profile name)

e105q5a.prof1

stmtprof: (statement profile name)

use index scan instead of table scan

original statement:

------------------

select

name

from

t1 where

id = 42

optimized statement:

-------------------

select

q1.name as "name"

from

e105q5a.t1 as q1

where

(q1.id = 42)

access plan:

-----------

total cost: 6.81152

query degree: 1

rows

return

( 1)

cost

i/o |1

fetch

( 2)

6.81152

1 /-----+-----\

1 6

ixscan dp-table: e105q5a

( 3) t1

0.00173777 q10 |

6 index: e105q5a

idx1

q1

可以看到,根據db2優化器的估計,這裡使用ixscan比使用tbscan的cost還要高,所以db2選擇了表掃瞄。在後續的測試中,發現當表的記錄數很多的時候,即使沒有使用優化概要,db2也會選擇索引掃瞄。

其他的說明:

DB2優化引數

4c 8g的乙個小機 資料量每日1萬左右。調整如下引數 fmsas為資料庫名,根據實際情況調整。先輸入db2cmd進入命令列介面,然後connect to 資料庫名,就可以執行下面命令了。update db cfg for fmsas using sortheap 3768 update db cf...

db2 優化培訓

第一天 上午 1.1 db2基礎,包括架構 使用者 例項 表空間 緩衝池 安全等重點概念。1.2 db2效能優化方法系統,包括經典三招 效能問題分析 使用pat方法找到效能瓶頸 硬體規劃等。下午 1.3 db2效能優化 從監控開始,包括監控方法學 作業系統監控 快照監控 管理試圖監控 事件監控器 d...

關於DB2的使用(DB2資料命令)

公司所用的資料庫有金倉和db2 首先要用命令視窗直接開啟db2需要在cmd中輸入 db2cmd 1 啟動db2資料庫 db2start 2 連線資料庫 db2 connect to 資料庫名稱 3 建立資料庫 db2 create db 資料庫名稱 4 刪除資料庫 db2 drop db 資料庫名稱...