2015-10-05 102 views

回答

0
create function string_array_to_string(text[], text, text) returns text as $$ 
    select array_to_string($1, $2, $3) 
$$ language sql cost 1 immutable; 

create index concurrently sites_rtb_ids on sites using gin (string_array_to_string(rtb_ids, ' ', ' ') gin_trgm_ops); 

这是创建索引的方式。使用的功能需要标记为不可变。

+0

这适用于在数组列上创建'gin_trgm_ops'索引。然而,我无法让Postgres对我尝试的任何查询使用此索引:( –

2

你会加速哪些操作:

CREATE INDEX CONCURRENTLY rtb_id_search ON sites USING GIN(array_to_string(rtb_id, '')); 

不过,PostgreSQL与抱怨? GIN indes直接支持array:

create table foo(a text[]); 
create index on foo using gin (a); 
set enable_seqscan to off; 

可能会有一些问题,因为不是所有的数组运算符都被索引支持。但几乎是这样。

 
postgres=# explain select * from foo where a @> ARRAY['a']; 
┌────────────────────────────────────────────────────────────────────────┐ 
│        QUERY PLAN        │ 
╞════════════════════════════════════════════════════════════════════════╡ 
│ Bitmap Heap Scan on foo (cost=8.05..18.20 rows=7 width=32)   │ 
│ Recheck Cond: (a @> '{a}'::text[])         │ 
│ -> Bitmap Index Scan on foo_a_idx (cost=0.00..8.05 rows=7 width=0) │ 
│   Index Cond: (a @> '{a}'::text[])        │ 
└────────────────────────────────────────────────────────────────────────┘ 
(4 rows) 
相关问题