Ano*_*oop 6 database postgresql
是否可以在类型为字符串数组的列上创建索引.尝试使用GIN索引.但查询似乎没有使用这些索引.
Example
CREATE TABLE users (
name VARCHAR(100),
groups text[],
);
Query: SELECT name FROM users WHERE ANY(groups) = 'Engineering'.
Run Code Online (Sandbox Code Playgroud)
另外,在'groups'列上有效执行GROUP BY的最佳方法是什么,以便它可以给'组'和计数.
可以使用杜松子酒指数:
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']);
Run Code Online (Sandbox Code Playgroud)
结果:
"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"
Run Code Online (Sandbox Code Playgroud)
在数组上使用聚合函数,这将是另一个问题。函数unnest () 可能会有所帮助。
为什么不规范化你的数据?这将解决所有问题,包括许多您尚未遇到的问题。