甲杜松子酒索引可用于:
CREATE TABLE users (
name VARCHAR(100),
groups text[]
);
CREATE INDEX idx_users ON users USING GIN(groups);
-- disable sequential scan in this test:
SET enable_seqscan TO off;
EXPLAIN ANALYZE
SELECT name FROM users WHERE groups @> (ARRAY['Engineering']);
结果:
"Bitmap Heap Scan on users (cost=4.26..8.27 rows=1 width=218) (actual time=0.021..0.021 rows=0 loops=1)"
" Recheck Cond: (groups @> '{Engineering}'::text[])"
" -> Bitmap Index Scan on idx_users (cost=0.00..4.26 rows=1 width=0) (actual time=0.016..0.016 rows=0 loops=1)"
" Index Cond: (groups @> '{Engineering}'::text[])"
"Total runtime: 0.074 ms"
在阵列上使用聚集函数,这将是另一个问题。函数unnest()可能会有所帮助。
为什么你不正常化你的数据?这将解决所有问题,包括许多问题,你还没有介入。
什么阻止你正常化? – 2011-02-09 15:19:07
实际表格有多个多值列。对于这些列,Db是相对正常化的期望,因为它会降低性能,所以我想避免过多的连接。我使用的'用户'表只是一个例子。 – Anoop 2011-02-09 15:41:34