我期待在postgresql 9.3的varchar
数组列中设置一个索引。我被告知使用array_to_string(col)
来设置它,但我不太明白这是如何工作的。我想出了以下声明:如何使用array_to_string在Postgresql中为varchar数组建立索引?
ERROR: functions in index expression must be marked IMMUTABLE
我期待在postgresql 9.3的varchar
数组列中设置一个索引。我被告知使用array_to_string(col)
来设置它,但我不太明白这是如何工作的。我想出了以下声明:如何使用array_to_string在Postgresql中为varchar数组建立索引?
ERROR: functions in index expression must be marked IMMUTABLE
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);
这是创建索引的方式。使用的功能需要标记为不可变。
你会加速哪些操作:
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)
这适用于在数组列上创建'gin_trgm_ops'索引。然而,我无法让Postgres对我尝试的任何查询使用此索引:( –