Postgres10高效能開發(4)控制解釋計畫

2021-10-03 10:22:29 字數 2940 閱讀 6844

引數

預設值說明

enable_bitmapscan

on啟用或禁用位圖索引掃瞄

enable_gathermerge

on啟用或禁用收集合併,併發查詢時會出現收集合併

enable_hashagg

on啟用或禁用hashaggregate

enable_hashjoin

on啟用或禁用hash join

enable_indexscan

on啟用或禁用索引掃瞄

enable_indexonlyscan

on啟用或禁用index only scan

enable_material

on啟用或禁用物化中間結果,不建議關閉

enable_mergejoin

on啟用或禁用merge join

enable_nestloop

on啟用或禁用nested loop

enable_seqscan

on啟用或禁用全表掃瞄

enable_sort

on啟用或禁用顯示排序

enable_tidscan

on啟用或禁用行id掃瞄

預設analyze會不定期自動執行。但在大量資料修改後,可能需要手動analyze生成新的統計值

當查詢只涉及到2-3張表聯表時,規劃器可以生成較優的表連線順序。規劃器分析時間隨著表的增多指數級增加。如果表太多,規劃器將使用遺傳概率搜尋,但不一定能找到最佳表連線順序。

因此需要通過join指定表連線順序

只有full join完全指定了連線順序

另外外連線優先順序低於內連線

select * from a left join (b join c on (b.ref = c.id)) on (a.id = b.id);
上面的sql指定先連線b和c,然後再和a連線

可通過調整postgres執行時引數改變規劃器行為

如果要強制指定規劃器使用sql的連線順序,可以將from_collapse_limit,join_collapse_limit 設定為1,防止規劃器重新排序

預設情況下

explain select * from inv_t_invoice i join inv_t_invoice_detail d on i.cl_id = d.cl_invoice_id

join inv_t_invoice_cash c on i.cl_id = c.cl_invoice_id;

#result

hash join (cost=531.63..8435.58 rows=15579 width=3086)

hash cond: ((i.cl_id)::text = (d.cl_invoice_id)::text)

-> nested loop (cost=0.29..7545.20 rows=14881 width=2860)

-> seq scan on inv_t_invoice i (cost=0.00..2278.72 rows=8972 width=2385)

-> index scan using idx_invoice_cash_invoice_id on inv_t_invoice_cash c (cost=0.29..0.57 rows=2 width=475)

index cond: ((cl_invoice_id)::text = (i.cl_id)::text)

-> hash (cost=413.93..413.93 rows=9393 width=226)

-> seq scan on inv_t_invoice_detail d (cost=0.00..413.93 rows=9393 width=226)

注意:規劃器修改了預設聯表順序,先連線inv_t_invoice和inv_t_invoice_cash,然後才是inv_t_invoice_detail

set join_collapse_limit=2;

explain select * from inv_t_invoice i join inv_t_invoice_detail d on i.cl_id = d.cl_invoice_id

join inv_t_invoice_cash c on i.cl_id = c.cl_invoice_id;

nested loop (cost=531.63..8483.11 rows=15579 width=3086)

join filter: ((i.cl_id)::text = (c.cl_invoice_id)::text)

-> hash join (cost=531.34..3026.53 rows=9393 width=2611)

hash cond: ((i.cl_id)::text = (d.cl_invoice_id)::text)

-> seq scan on inv_t_invoice i (cost=0.00..2278.72 rows=8972 width=2385)

-> hash (cost=413.93..413.93 rows=9393 width=226)

-> seq scan on inv_t_invoice_detail d (cost=0.00..413.93 rows=9393 width=226)

-> index scan using idx_invoice_cash_invoice_id on inv_t_invoice_cash c (cost=0.29..0.56 rows=2 width=475)

index cond: ((cl_invoice_id)::text = (d.cl_invoice_id)::text)

通過設定join_collapse_limit小於3,成功讓規劃器按inv_t_invoice、inv_t_invoice_detail、inv_t_invoice_cash的預設順序聯表

Postgres10高效能開發(1)索引

text pattern ops,varchar pattern ops和bpchar pattern ops運算子支援按字元比較,而不是預設語言排序規則比較。可以用於btree索引,但只支援等於比較,不再支援範圍查詢 查詢lc collate方法 show lc collate 結構gin索引是一...

「位運算」助力高效能開發

符號 描述運算規則 按位與 2個位都為1,結果為1 按位或 乙個位為1,結果為1 按位異或 相同為0,相異為1 按位取反 1變0,0變1 左移各二進位全部左移n位,高位丟棄,低位補0 n 右移各二進位全部右移若干位,對無符號數,高位補0,有符號數,各編譯器處理方法不一樣,有的補符號位 算術右移 有的...

Oracle11高效能開發 (5)SQL調優

全表掃瞄select from c fraud detail trowid訪問select from c fraud detail t where rowid 唯一索引掃瞄select from c fraud detail t where t.id 2016fd3b 7aa6 44d0 80f9 ...