我有一个包含这样的表的postgres数据库:
CREATE TABLE state
(
id SERIAL,
keys JSONB NOT NULL
);
CREATE INDEX keys_idx ON state (keys);
使用这样的数据(大约2.5M行):
1, ['one', 'two', 'three']
2, ['one', 'three']
3, ['three']
4, ['other', 'keys', 'more']
...
但是当我尝试搜索状态时,我得到了seq扫描(1-10秒)
EXPLAIN ( ANALYSE )
SELECT *
FROM state s
WHERE
s.keys <@ '["one", "two"]'
OR s.keys @> '["one", "two"]';
结果:
Seq Scan on state s (cost=0.00..148096.55 rows=7809 width=136) (actual time=0.544..2524.739 rows=4627 loops=1)
Filter: ((keys <@ '["one", "two"]'::jsonb) OR (keys @> '["one", "two"]'::jsonb))
Rows Removed by Filter: 3814316
Planning time: 0.080 ms
Execution time: 2525.585 ms
如何提高此查询的效果?谢谢。