运行以下代码时:
drop table if exists demo;
drop table if exists demo_test;
drop table if exists demo_result;
create table demo as select md5(v::text) from generate_series(1, 1000000) v;
create index on demo (md5 text_pattern_ops);
analyze demo;
create table demo_test
as select left(md5(v::text), 5) || '%' as "patt" from generate_series(2000000, 2000010) v;
create table demo_result (row text);
load 'auto_explain';
set auto_explain.log_min_duration to 0;
set auto_explain.log_analyze to true;
set auto_explain.log_nested_statements to true;
do $$
declare
row record;
pattern text;
begin
for row in select patt from demo_test loop
pattern = row.patt; -- <--- CRUCIAL LINE
insert into demo_result select * from demo where md5 like pattern;
end loop;
end$$;
PostgreSQL 生成以下查询计划:
2017-10-02 17:03:48 CEST [18038-23] app=psql barczynski@barczynski LOG: duration: 0.021 ms plan:
Query Text: insert into demo_result select * from demo where md5 like pattern
Insert on demo_result (cost=0.42..8.45 rows=100 width=33) (actual time=0.021..0.021 rows=0 loops=1)
-> Index Only Scan using demo_md5_idx on demo (cost=0.42..8.45 rows=100 width=33) (actual time=0.018..0.018 rows=1 loops=1)
Index Cond: ((md5 ~>=~ '791cc'::text) AND (md5 ~<~ '791cd'::text))
Filter: (md5 ~~ '791cc%'::text)
Heap Fetches: 1
但是在删除变量并在条件下pattern
内联之后:row.patt
where
insert into demo_result select * from demo where md5 like row.patt;
PostgreSQL 将参数视为绑定:
2017-10-02 17:03:02 CEST [17901-23] app=psql barczynski@barczynski LOG: duration: 89.636 ms plan:
Query Text: insert into demo_result select * from demo where md5 like row.patt
Insert on demo_result (cost=0.00..20834.00 rows=5000 width=33) (actual time=89.636..89.636 rows=0 loops=1)
-> Seq Scan on demo (cost=0.00..20834.00 rows=5000 width=33) (actual time=47.255..89.628 rows=1 loops=1)
Filter: (md5 ~~ $4)
Rows Removed by Filter: 999999
我知道后一种计划采用顺序扫描,因为 PostgreSQL 假定绑定参数以通配符开头。
我的问题是为什么额外的分配会打开和关闭绑定参数?