Optimization

對 db2 優化器計劃的統計影響

  • January 30, 2017

統計資訊的創建對 db2 優化器給出的估計成本和優化器計劃有任何影響。或者(在最好的情況下)只會改變查詢的執行時間?

我跑了這樣的東西。這是正確的/有用的嗎?

RUNSTATS ON TABLE DB2INST1.ARTISTS WITH DISTRIBUTION DEFAULT NUM_FREQVALUES 50 NUM_QUANTILES 5 AND DETAILED INDEXES ALL;
RUNSTATS ON TABLE DB2INST1.ARTISTS ON KEY COLUMNS;
RUNSTATS ON TABLE DB2INST1.ARTIST ON COLUMNS((COUNTRY,ARTIST_NAME),(ID,ARTIST_NAME),(CONTACT_MAIL,CONTACT_PHONE),(WEBSITE,TWITTER,SOUNDCLOUD,FACEBOOK));

我在這個數據庫上執行的兩個查詢是:

SELECT * FROM ARTISTS WHERE WEBSITE IS NOT NULL AND TWITTER IS NOT NULL AND SOUNDCLOUD IS NOT NULL AND FACEBOOK IS NOT NULL
SELECT YEAR, FORMAT, count(*) AS ANZAHL, sum(TRACK_LENGTH) AS GESAMTLAENGE_sec FROM RELEASES JOIN TRACKS ON RELEASES.ID = TRACKS.RELEASE_ID GROUP BY YEAR, FORMAT ORDER BY YEAR DESC

兩個查詢的執行時間都比執行 runstats 之前要好。但是 db2expln 給出的估計成本和優化器計劃是完全一樣的。

db2expln 命令:

db2expln -q [one of the queries above] -t -g -d $dbname

我只能同意邁克爾所說的,統計對於任何基於成本的優化器(如 db2 和大多數(全部?)現代 RDBM)都非常重要。除此之外,我建議你看看db2exfmt. 它使用稍微不同的方法來確定計劃,並且 IMO 提供了更好的輸出。有幾種方法可以填充解釋表,兩個範例是:

#> db2 "explain plan for SELECT * FROM ARTISTS WHERE WEBSITE IS NOT NULL AND TWITTER IS NOT NULL AND SOUNDCLOUD IS NOT NULL AND FACEBOOK IS NOT NULL"

要麼

#> db2 set current explain mode explain
#> db2 "SELECT * FROM ARTISTS WHERE WEBSITE IS NOT NULL AND TWITTER IS NOT NULL AND SOUNDCLOUD IS NOT NULL AND FACEBOOK IS NOT NULL"
#> db2 set current explain mode no

現在您可以使用db2exfmt來呈現結果。您可以選擇以多種方式呈現的計劃,但我通常只選擇最後一個計劃(-1):

#> db2exfmt -d <database> -g -1 -o query.exfmt  
#> less -S query.exfmt

引用自:https://dba.stackexchange.com/questions/162534