2015-10-05 35 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) 
相關問題