是否有任何索引可帮助此类查询?
select p.name
from person p
where 'abjohncde' like '%'||p.name||'%'
可将gin与gin_trgm_ops一起使用的索引可用于此查询:
select p.name from person p where p.name like '%john%'
但是杜松子酒不适用于第一个查询。
据我所知,没有现成的解决方案。您可以尝试手动制作类似Trigram索引的内容:
create or replace function my_trgm(x text)
returns text[]
language sql
immutable strict
as $$
select
case when length(x) > 3 then
(select array_agg(distinct lower(substr(x, i, 3))) from generate_series(1, length(x)-2) as i)
else array[x]
end
$$;
create or replace function public.my_trgm_1(x text)
returns text[]
language sql
immutable strict
as $$
select
(select array_agg(distinct lower(substr(x, i, 3))) from generate_series(1, length(x)-2) as i) ||
(select array_agg(distinct lower(substr(x, i, 2))) from generate_series(1, length(x)-1) as i) ||
(select array_agg(distinct lower(substr(x, i, 1))) from generate_series(1, length(x)) as i)
$$;
并测试:
postgres=# create table t as select ((random()*1000)::int)::text as x from generate_series(1, 1000000);
SELECT 1000000
postgres=# create index idx_t_x on t using gin (my_trgm(x));
CREATE INDEX
postgres=# explain (analyze, verbose) select * from t where '16662' like '%'||x||'%';
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..13216.67 rows=5000 width=3) (actual time=0.338..65.335 rows=8971 loops=1)
Output: x
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on public.t (cost=0.00..11716.67 rows=2083 width=3) (actual time=0.106..59.276 rows=2990 loops=3)
Output: x
Filter: ('16662'::text ~~ (('%'::text || t.x) || '%'::text))
Rows Removed by Filter: 330343
Worker 0: actual time=0.089..57.952 rows=3126 loops=1
Worker 1: actual time=0.124..58.830 rows=3308 loops=1
Planning Time: 0.061 ms
Execution Time: 65.803 ms
postgres=# explain (analyze, verbose) select * from t where my_trgm(x) <@ my_trgm_1('16662') and '16662' ~ x;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on public.t (cost=184.40..6916.58 rows=43 width=3) (actual time=4.655..70.851 rows=8971 loops=1)
Output: x
Recheck Cond: (my_trgm(t.x) <@ '{166,666,662,16,66,62,1,6,2}'::text[])
Filter: ('16662'::text ~ t.x)
Heap Blocks: exact=3876
-> Bitmap Index Scan on idx_t_x (cost=0.00..184.39 rows=8586 width=0) (actual time=3.534..3.534 rows=8971 loops=1)
Index Cond: (my_trgm(t.x) <@ '{166,666,662,16,66,62,1,6,2}'::text[])
Planning Time: 0.578 ms
Execution Time: 71.228 ms
请注意,它是在单列表上执行的,因此在此特定测试用例中扫描整个表可能更有效地扫描GIN索引。
PS:使用诸如plpythonu
或plperl
之类的PL语言可以更有效地插入/更新数据。