欧美阿v视频在线大全_亚洲欧美中文日韩V在线观看_www性欧美日韩欧美91_亚洲欧美日韩久久精品

主頁 > 知識庫 > 淺談pg_hint_plan定制執行計劃

淺談pg_hint_plan定制執行計劃

熱門標簽:電話機器人怎么換人工座席 江蘇400電話辦理官方 廣州電銷機器人公司招聘 天津開發區地圖標注app 移動外呼系統模擬題 400電話申請客服 電銷機器人能補救房產中介嗎 濟南外呼網絡電話線路 地圖標注要花多少錢

有的時候PG給出的執行計劃由于很多原因并不是最優的,需要手動指定執行路徑時我們可以加載pg_hint_plan這個插件。

1 安裝插件

預先安裝Postgresql10.7

cd postgresql-10.7/contrib/
wget https://github.com/ossc-db/pg_hint_plan/archive/REL10_1_3_3.tar.gz
tar xzvf pg_hint_plan-REL10_1_3_3.tar.gz
cd pg_hint_plan-REL10_1_3_3
make
make install

檢查文件

cd $PGHOME
ls lib/pg_hint_plan.so
lib/pg_hint_plan.so
ls share/extension/
pg_hint_plan--1.3.0--1.3.1.sql pg_hint_plan--1.3.2--1.3.3.sql pg_hint_plan.control plpgsql.control
pg_hint_plan--1.3.1--1.3.2.sql pg_hint_plan--1.3.3.sql   plpgsql--1.0.sql  plpgsql--unpackaged--1.0.sql

2 加載插件

2.1 當前會話加載

LOAD 'pg_hint_plan';

注意這樣加載只在當前回話生效。

2.2 用戶、庫級自動加載

alter user postgres set session_preload_libraries='pg_hint_plan';
alter database postgres set session_preload_libraries='pg_hint_plan';

配置錯了的話就連不上數據庫了!

如果配置錯了,連接template1庫執行

alter database postgres reset session_preload_libraries;
alter user postgres reset session_preload_libraries;

2.3 cluster級自動加載

在postgresql.conf中修改shared_preload_libraries=‘pg_hint_plan'

重啟數據庫

3 檢查是否已經加載

pg_hint_plan加載后在extension里面是看不到的,所以需要確認插件是否已經加載

show session_preload_libraries;
 session_preload_libraries
---------------------------
 pg_hint_plan

或者

show shared_preload_libraries;

如果使用load方式加載不需要檢查。

4 使用插件定制執行計劃

4.1 初始化測試數據

create table t1 (id int, t int, name varchar(255));
create table t2 (id int , salary int);
create table t3 (id int , age int);
insert into t1 values (1,200,'jack');
insert into t1 values (2,300,'tom');
insert into t1 values (3,400,'john');
insert into t2 values (1,40000);
insert into t2 values (2,38000);
insert into t2 values (3,18000);
insert into t3 values (3,38);
insert into t3 values (2,55);
insert into t3 values (1,12);
explain analyze select * from t1 left join t2 on t1.id=t2.id left join t3 on t1.id=t3.id;
              QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
 Hash Right Join (cost=89.82..337.92 rows=17877 width=540) (actual time=0.053..0.059 rows=3 loops=1)
 Hash Cond: (t3.id = t1.id)
 -> Seq Scan on t3 (cost=0.00..32.60 rows=2260 width=8) (actual time=0.002..0.002 rows=3 loops=1)
 -> Hash (cost=70.05..70.05 rows=1582 width=532) (actual time=0.042..0.043 rows=3 loops=1)
   Buckets: 2048 Batches: 1 Memory Usage: 17kB
   -> Hash Right Join (cost=13.15..70.05 rows=1582 width=532) (actual time=0.034..0.039 rows=3 loops=1)
    Hash Cond: (t2.id = t1.id)
    -> Seq Scan on t2 (cost=0.00..32.60 rows=2260 width=8) (actual time=0.002..0.002 rows=3 loops=1)
    -> Hash (cost=11.40..11.40 rows=140 width=524) (actual time=0.017..0.017 rows=3 loops=1)
      Buckets: 1024 Batches: 1 Memory Usage: 9kB
      -> Seq Scan on t1 (cost=0.00..11.40 rows=140 width=524) (actual time=0.010..0.011 rows=3 loops=1)
 Planning time: 0.154 ms
 Execution time: 0.133 ms

創建索引

create index idx_t1_id on t1(id);
create index idx_t2_id on t2(id);
create index idx_t3_id on t3(id);
explain analyze select * from t1 left join t2 on t1.id=t2.id left join t3 on t1.id=t3.id;
             QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Hash Left Join (cost=2.14..3.25 rows=3 width=540) (actual time=0.045..0.047 rows=3 loops=1)
 Hash Cond: (t1.id = t3.id)
 -> Hash Left Join (cost=1.07..2.14 rows=3 width=532) (actual time=0.030..0.032 rows=3 loops=1)
   Hash Cond: (t1.id = t2.id)
   -> Seq Scan on t1 (cost=0.00..1.03 rows=3 width=524) (actual time=0.005..0.006 rows=3 loops=1)
   -> Hash (cost=1.03..1.03 rows=3 width=8) (actual time=0.007..0.007 rows=3 loops=1)
    Buckets: 1024 Batches: 1 Memory Usage: 9kB
    -> Seq Scan on t2 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=1)
 -> Hash (cost=1.03..1.03 rows=3 width=8) (actual time=0.005..0.005 rows=3 loops=1)
   Buckets: 1024 Batches: 1 Memory Usage: 9kB
   -> Seq Scan on t3 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.002 rows=3 loops=1)
 Planning time: 0.305 ms
 Execution time: 0.128 ms

4.2 強制走index scan

/*+ indexscan(t1 idx_d)
/*+ indexscan(t1 idx_t1_id)
explain (analyze,buffers) select * from t1 where id=2;
           QUERY PLAN
----------------------------------------------------------------------------------------------
 Seq Scan on t1 (cost=0.00..1.04 rows=1 width=524) (actual time=0.011..0.013 rows=1 loops=1)
 Filter: (id = 2)
 Rows Removed by Filter: 2
 Buffers: shared hit=1
 Planning time: 0.058 ms
 Execution time: 0.028 ms
explain (analyze,buffers) /*+ indexscan(t1) */select * from t1 where id=2;
             QUERY PLAN
----------------------------------------------------------------------------------------------------------------
 Index Scan using idx_t1_id on t1 (cost=0.13..8.15 rows=1 width=524) (actual time=0.044..0.046 rows=1 loops=1)
 Index Cond: (id = 2)
 Buffers: shared hit=1 read=1
 Planning time: 0.145 ms
 Execution time: 0.072 ms
explain (analyze,buffers) /*+ indexscan(t1 idx_t1_id) */select * from t1 where id=2;
             QUERY PLAN
----------------------------------------------------------------------------------------------------------------
 Index Scan using idx_t1_id on t1 (cost=0.13..8.15 rows=1 width=524) (actual time=0.016..0.017 rows=1 loops=1)
 Index Cond: (id = 2)
 Buffers: shared hit=2
 Planning time: 0.079 ms
 Execution time: 0.035 ms

4.3 強制多條件組合

/*+ indexscan(t2) indexscan(t1 idx_t1_id) */
/*+ seqscan(t2) indexscan(t1 idx_t1_id) */
explain analyze SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id);
            QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Hash Join (cost=1.07..2.14 rows=3 width=532) (actual time=0.018..0.020 rows=3 loops=1)
 Hash Cond: (t1.id = t2.id)
 -> Seq Scan on t1 (cost=0.00..1.03 rows=3 width=524) (actual time=0.006..0.007 rows=3 loops=1)
 -> Hash (cost=1.03..1.03 rows=3 width=8) (actual time=0.005..0.005 rows=3 loops=1)
   Buckets: 1024 Batches: 1 Memory Usage: 9kB
   -> Seq Scan on t2 (cost=0.00..1.03 rows=3 width=8) (actual time=0.001..0.003 rows=3 loops=1)
 Planning time: 0.114 ms
 Execution time: 0.055 ms
(8 rows)

組合兩個條件走indexscan

/*+ indexscan(t2) indexscan(t1 idx_t1_id) */explain analyze SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id);
              QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Merge Join (cost=0.26..24.40 rows=3 width=532) (actual time=0.047..0.053 rows=3 loops=1)
 Merge Cond: (t1.id = t2.id)
 -> Index Scan using idx_t1_id on t1 (cost=0.13..12.18 rows=3 width=524) (actual time=0.014..0.015 rows=3 loops=1)
 -> Index Scan using idx_t2_id on t2 (cost=0.13..12.18 rows=3 width=8) (actual time=0.026..0.028 rows=3 loops=1)

組合兩個條件走indexscan+seqscan

/*+ seqscan(t2) indexscan(t1 idx_t1_id) */explain analyze SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id);
              QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Nested Loop (cost=0.13..13.35 rows=3 width=532) (actual time=0.025..0.032 rows=3 loops=1)
 Join Filter: (t1.id = t2.id)
 Rows Removed by Join Filter: 6
 -> Index Scan using idx_t1_id on t1 (cost=0.13..12.18 rows=3 width=524) (actual time=0.016..0.018 rows=3 loops=1)
 -> Materialize (cost=0.00..1.04 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=3)
   -> Seq Scan on t2 (cost=0.00..1.03 rows=3 width=8) (actual time=0.004..0.005 rows=3 loops=1)

4.4 強制指定join method

/*+ NestLoop(t1 t2) MergeJoin(t1 t2 t3) Leading(t1 t2 t3) */
/*+ NestLoop(t1 t2 t3) MergeJoin(t2 t3) Leading(t1 (t2 t3)) */
explain analyze select * from t1 left join t2 on t1.id=t2.id left join t3 on t1.id=t3.id;
             QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Hash Left Join (cost=2.14..3.25 rows=3 width=540) (actual time=0.053..0.056 rows=3 loops=1)
 Hash Cond: (t1.id = t3.id)
 -> Hash Left Join (cost=1.07..2.14 rows=3 width=532) (actual time=0.036..0.038 rows=3 loops=1)
   Hash Cond: (t1.id = t2.id)
   -> Seq Scan on t1 (cost=0.00..1.03 rows=3 width=524) (actual time=0.007..0.007 rows=3 loops=1)
   -> Hash (cost=1.03..1.03 rows=3 width=8) (actual time=0.009..0.009 rows=3 loops=1)
    Buckets: 1024 Batches: 1 Memory Usage: 9kB
    -> Seq Scan on t2 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=1)
 -> Hash (cost=1.03..1.03 rows=3 width=8) (actual time=0.006..0.006 rows=3 loops=1)
   Buckets: 1024 Batches: 1 Memory Usage: 9kB
   -> Seq Scan on t3 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=1)

強制走循環嵌套連接

/*+ NestLoop(t1 t2) MergeJoin(t1 t2 t3) Leading(t1 t2 t3) */
explain analyze select * from t1 left join t2 on t1.id=t2.id left join t3 on t1.id=t3.id;
              QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
 Merge Left Join (cost=3.28..3.34 rows=3 width=540) (actual time=0.093..0.096 rows=3 loops=1)
 Merge Cond: (t1.id = t3.id)
 -> Sort (cost=2.23..2.23 rows=3 width=532) (actual time=0.077..0.078 rows=3 loops=1)
   Sort Key: t1.id
   Sort Method: quicksort Memory: 25kB
   -> Nested Loop Left Join (cost=0.00..2.20 rows=3 width=532) (actual time=0.015..0.020 rows=3 loops=1)
    Join Filter: (t1.id = t2.id)
    Rows Removed by Join Filter: 6
    -> Seq Scan on t1 (cost=0.00..1.03 rows=3 width=524) (actual time=0.005..0.005 rows=3 loops=1)
    -> Materialize (cost=0.00..1.04 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=3)
      -> Seq Scan on t2 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=1)
 -> Sort (cost=1.05..1.06 rows=3 width=8) (actual time=0.012..0.013 rows=3 loops=1)
   Sort Key: t3.id
   Sort Method: quicksort Memory: 25kB
   -> Seq Scan on t3 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=1)

控制連接順序

/*+ NestLoop(t1 t2 t3) MergeJoin(t2 t3) Leading(t1 (t2 t3)) */
explain analyze select * from t1 left join t2 on t1.id=t2.id left join t3 on t1.id=t3.id;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Nested Loop Left Join (cost=1.07..3.31 rows=3 width=540) (actual time=0.036..0.041 rows=3 loops=1)
 Join Filter: (t1.id = t3.id)
 Rows Removed by Join Filter: 6
 -> Hash Left Join (cost=1.07..2.14 rows=3 width=532) (actual time=0.030..0.032 rows=3 loops=1)
   Hash Cond: (t1.id = t2.id)
   -> Seq Scan on t1 (cost=0.00..1.03 rows=3 width=524) (actual time=0.008..0.009 rows=3 loops=1)
   -> Hash (cost=1.03..1.03 rows=3 width=8) (actual time=0.007..0.007 rows=3 loops=1)
    Buckets: 1024 Batches: 1 Memory Usage: 9kB
    -> Seq Scan on t2 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.004 rows=3 loops=1)
 -> Materialize (cost=0.00..1.04 rows=3 width=8) (actual time=0.001..0.002 rows=3 loops=3)
   -> Seq Scan on t3 (cost=0.00..1.03 rows=3 width=8) (actual time=0.002..0.003 rows=3 loops=1)

4.5 控制單條SQL的cost

/*+ set(seq_page_cost 20.0) seqscan(t1) */
/*+ set(seq_page_cost 20.0) seqscan(t1) */explain analyze select * from t1 where id > 1;
           QUERY PLAN
-----------------------------------------------------------------------------------------------
 Seq Scan on t1 (cost=0.00..20.04 rows=1 width=524) (actual time=0.011..0.013 rows=2 loops=1)
 Filter: (id > 1)
 Rows Removed by Filter: 1

set seq_page_cost 200,注意下面的cost已經變成了200.04

/*+ set(seq_page_cost 200.0) seqscan(t1) */explain analyze select * from t1 where id > 1;
           QUERY PLAN
------------------------------------------------------------------------------------------------
 Seq Scan on t1 (cost=0.00..200.04 rows=1 width=524) (actual time=0.010..0.011 rows=2 loops=1)
 Filter: (id > 1)
 Rows Removed by Filter: 1

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • PostgreSQL 慢查詢SQL跟蹤操作
  • CentOS PostgreSQL 12 主從復制(主從切換)操作
  • PostgreSQL 查看表的主外鍵等約束關系詳解
  • PostgreSQL 修改視圖的操作
  • PostgreSQL 更新視圖腳本的注意事項說明
  • postgreSQL中的row_number() 與distinct用法說明
  • Postgresql 動態統計某一列的某一值出現的次數實例
  • postgresql 計算兩點距離的2種方法小結

標簽:榆林 昭通 杭州 寶雞 海西 濮陽 辛集 溫州

巨人網絡通訊聲明:本文標題《淺談pg_hint_plan定制執行計劃》,本文關鍵詞  淺談,hint,plan,定制,執行,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《淺談pg_hint_plan定制執行計劃》相關的同類信息!
  • 本頁收集關于淺談pg_hint_plan定制執行計劃的相關信息資訊供網民參考!
  • 推薦文章
    欧美阿v视频在线大全_亚洲欧美中文日韩V在线观看_www性欧美日韩欧美91_亚洲欧美日韩久久精品
  • <rt id="w000q"><acronym id="w000q"></acronym></rt>
  • <abbr id="w000q"></abbr>
    <rt id="w000q"></rt>
    午夜一区二区三区免费| 亚洲午夜久久久久久久久电影网| 亚洲女女做受ⅹxx高潮| 日韩二区在线观看| 成人精品电影在线观看| 国产精品揄拍100视频| 国产67194| 欧美xingq一区二区| 一区二区久久久久久| 国产精品影音先锋| 久久久久国产精品区片区无码| 欧美日韩高清影院| 中文字幕中文字幕在线一区| 精品亚洲免费视频| 毛茸茸free性熟hd| 色老汉一区二区三区| 久久久久久麻豆| 日韩国产欧美在线播放| 韩国三级在线看| 色欧美片视频在线观看在线视频| 成人免费在线观看入口| 国产美女主播视频一区| 亚洲第一页av| 久久综合丝袜日本网| 免费精品视频在线| 中文字幕天堂av| 色域天天综合网| 亚洲精品综合在线| www.色综合.com| 999精品视频在线观看播放 | 91在线视频18| 国产老头老太做爰视频| 一区视频在线播放| 能看毛片的网站| 色婷婷综合视频在线观看| 亚洲精品日韩一| 国产视频久久久久久| 欧美日本韩国一区二区三区视频| 肉色丝袜一区二区| 日韩中文字幕有码| 欧美精品一区二区三区在线| 日韩av在线免费观看不卡| 人人人妻人人澡人人爽欧美一区| 日韩一级欧美一级| 日本成人在线看| 亚洲综合图片一区| 国产欧美一区二区精品性色| 国产麻豆视频一区| 在线精品视频免费观看| 亚洲你懂的在线视频| 喷水视频在线观看| 国产欧美日本一区视频| 91在线观看美女| 日韩欧美在线观看一区二区三区| 国产精品一级片在线观看| 欧美偷拍一区二区| 香蕉av福利精品导航| 人妻丰满熟妇aⅴ无码| 国产精品视频你懂的| 成人蜜臀av电影| 3d动漫精品啪啪| 日韩成人精品在线| 国产精品免费人成网站酒店| 亚洲超碰精品一区二区| 亚洲女人毛茸茸高潮| 亚洲国产精品久久艾草纯爱| 91成人在线免费视频| 亚洲日本欧美天堂| caopor在线| 精品国产一区久久| av资源网一区| 久久综合色8888| 亚洲熟女乱综合一区二区| 久久这里只有精品首页| 国产精品久久久久野外| 国产欧美日韩视频在线观看| 国产精九九网站漫画| 中日韩av电影| 国模大尺度视频| 国产欧美综合色| 国产精品久久久免费观看| 中文字幕一区二区不卡| 亚洲自拍偷拍图| 亚洲一区二区三区四区的| 国产麻豆天美果冻无码视频| 自拍av一区二区三区| 精品国产成人亚洲午夜福利| 亚洲成人精品影院| 老熟妇高潮一区二区三区| 美女一区二区久久| 国产真实乱在线更新| 免费在线欧美视频| 欧美日韩一区二区在线观看视频| 久久国产视频网| 老女人性淫交视频| 午夜精品久久一牛影视| 69xxx免费| 亚洲另类一区二区| sm捆绑调教视频| 精品一区二区三区久久| 3atv在线一区二区三区| 手机看片国产精品| 成人免费在线播放视频| 永久免费看mv网站入口| 国产一区二区毛片| 精品三级av在线| 欧美深性狂猛ⅹxxx深喉| 亚洲国产一区二区a毛片| 在线精品亚洲一区二区不卡| 成人黄色国产精品网站大全在线免费观看| 在线播放视频一区| 国产成人综合亚洲网站| 7777精品伊人久久久大香线蕉完整版| www.激情成人| 国产精品国产三级国产有无不卡| 国产精品理论在线| 午夜欧美大尺度福利影院在线看| 色婷婷综合久久久久中文| 风间由美中文字幕在线看视频国产欧美 | 美女一区二区视频| 日韩欧美国产系列| 极品人妻一区二区| 亚洲人xxxx| 色天使色偷偷av一区二区| 成人福利电影精品一区二区在线观看| 国产女人aaa级久久久级 | 欧美三级午夜理伦三级中视频| 99在线视频精品| 亚洲人午夜精品天堂一二香蕉| 可以直接看的黄色网址| 成人激情黄色小说| **欧美大码日韩| 91精彩视频在线观看| 国产精品18久久久久| 国产午夜精品久久久久久免费视| 人妻在线日韩免费视频| 日本欧美一区二区三区| 欧美成人免费网站| caopeng视频| 国产福利一区二区三区视频| 国产精品欧美一区二区三区| 中文字幕手机在线观看| 91蝌蚪porny九色| 一区在线播放视频| 91成人在线精品| 中国黄色片视频| 免费不卡在线视频| 久久精品亚洲精品国产欧美| 欧洲美女女同性互添| 99国产精品久久久| 亚洲一区二区三区四区不卡| 日韩一区二区三区三四区视频在线观看| 国产交换配乱淫视频免费| 日韩中文字幕麻豆| 精品国产伦一区二区三区免费| 99精品中文字幕| 99riav一区二区三区| 午夜一区二区三区在线观看| 精品国产一区二区亚洲人成毛片| 91制片厂在线| 粉嫩aⅴ一区二区三区四区| 亚洲人成影院在线观看| 337p亚洲精品色噜噜狠狠| 日本激情小视频| 不卡电影免费在线播放一区| 亚洲国产wwwccc36天堂| 久久综合九色综合欧美亚洲| 日本中文在线视频| 精人妻一区二区三区| 久久精品国产网站| 亚洲日韩欧美一区二区在线| 在线观看91精品国产麻豆| 高清国产在线观看| 97se亚洲国产综合自在线不卡| 天天综合色天天| 国产精品乱码久久久久久| 欧美日韩国产经典色站一区二区三区| 亚洲av无码一区二区三区人 | 久久久久97国产精华液好用吗| 色婷婷亚洲精品| 亚洲专区区免费| 国产精品白丝jk黑袜喷水| 亚洲一区二区三区免费视频| 欧美精品一区二区蜜臀亚洲| 日本韩国欧美一区二区三区| 国产精品扒开腿做爽爽| 91农村精品一区二区在线| 久久国产精品99久久人人澡| 亚洲欧美一区二区三区孕妇| 精品1区2区在线观看| 欧美性猛交xxxxxxxx| 东方伊人免费在线观看| caopor在线| 成人动漫一区二区三区| 欧美96一区二区免费视频| 亚洲免费色视频| 久久精品网站免费观看| 91麻豆精品国产91久久久久 | 亚洲激情 欧美| 成人高清免费在线播放|