下载
https://hub.fastgit.org/ossc-db/pg_hint_plan/releases
安装
tar zxvf pg_hint_plan-REL12_1_3_7.tar.gz
cd pg_hint_plan-REL12_1_3_7
export PATH=/home/postgres/pgsql/bin:$PATH
make
make install
安装过程如下:
[root@node_205 pg_hint_plan-REL12_1_3_7]# export PATH=/home/postgres/pgsql/bin:$PATH
You have new mail in /var/spool/mail/root
[root@node_205 pg_hint_plan-REL12_1_3_7]# make
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I. -I./ -I/home/postgres/pgsql/include/server -I/home/postgres/pgsql/include/internal -D_GNU_SOURCE -c -o pg_hint_plan.o pg_hint_plan.c
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC pg_hint_plan.o -L/home/postgres/pgsql/lib -Wl,--as-needed -Wl,-rpath,'/home/postgres/pgsql/lib',--enable-new-dtags -Wl,--build-id -shared -o pg_hint_plan.so
[root@node_205 pg_hint_plan-REL12_1_3_7]# make install
/usr/bin/mkdir -p '/home/postgres/pgsql/share/extension'
/usr/bin/mkdir -p '/home/postgres/pgsql/share/extension'
/usr/bin/mkdir -p '/home/postgres/pgsql/lib'
/usr/bin/install -c -m 644 .//pg_hint_plan.control '/home/postgres/pgsql/share/extension/'
/usr/bin/install -c -m 644 .//pg_hint_plan--*.sql '/home/postgres/pgsql/share/extension/'
/usr/bin/install -c -m 755 pg_hint_plan.so '/home/postgres/pgsql/lib/'
基本配置
vi $PGDATA/postgresql.conf
shared_preload_libraries = 'pg_hint_plan'
pg_hint_plan.enable_hint = on
pg_hint_plan.debug_print = on
pg_hint_plan.message_level = log
postgres=# create extension pg_hint_plan;
使用
create table a(id int primary key, info text, crt_time timestamp);
create table b(id int primary key, info text, crt_time timestamp);
insert into a select generate_series(1,100000), 'a_'||md5(random()::text), clock_timestamp();
insert into b select generate_series(1,100000), 'b_'||md5(random()::text), clock_timestamp();
analyze a;
analyze b;
- 不使用 pg_hint_plan
test=# explain select a.*,b.* from a,b where a.id=b.id and a.id<10;
QUERY PLAN
-----------------------------------------------------------------------
Nested Loop (cost=0.58..26.31 rows=8 width=94)
-> Index Scan using a_pkey on a (cost=0.29..3.03 rows=8 width=47)
Index Cond: (id < 10)
-> Index Scan using b_pkey on b (cost=0.29..2.91 rows=1 width=47)
Index Cond: (id = a.id)
(5 rows)
- 使用 pg_hint_plan
test=# /*+
HashJoin(a b)
SeqScan(b)
*/ explain(verbose, analyse, costs, buffers) select a.*,b.* from a,b where a.id=b.id and a.id<10;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=3.13..2200.64 rows=8 width=94) (actual time=1.460..49.296 rows=9 loops=1)
Output: a.id, a.info, a.crt_time, b.id, b.info, b.crt_time
Inner Unique: true
Hash Cond: (b.id = a.id)
Buffers: shared hit=6 read=935 dirtied=540
-> Seq Scan on public.b (cost=0.00..1935.00 rows=100000 width=47) (actual time=0.478..36.353 rows=100000 loops=1)
Output: b.id, b.info, b.crt_time
Buffers: shared read=935 dirtied=540
-> Hash (cost=3.03..3.03 rows=8 width=47) (actual time=0.155..0.157 rows=9 loops=1)
Output: a.id, a.info, a.crt_time
Buckets: 1024 Batches: 1 Memory Usage: 9kB
Buffers: shared hit=3
-> Index Scan using a_pkey on public.a (cost=0.29..3.03 rows=8 width=47) (actual time=0.056..0.071 rows=9 loops=1)
Output: a.id, a.info, a.crt_time
Index Cond: (a.id < 10)
Buffers: shared hit=3
Planning Time: 1.683 ms
Execution Time: 52.194 ms
(18 rows)
- 参考如下
https://github.com/digoal/blog/blob/master/201602/20160203_01.md?spm=a2c4e.11153940.blogcont68244.70.29035625nnzqPA&file=20160203_01.md