甲杜松子酒索引可用於:
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