为什么不使用 GIN 索引?
Why GIN index is not used?
我有一个 jsonb 字段,其中包含如下数据:
{"state": "initialize_done_state", ... }
该字段的 GIN 索引:
CREATE INDEX index_activities_on_data ON activities USING gin (data)
并执行查询:
select count(*)
from activities a
where a.created_at >= (date_trunc('month', current_date)::timestamp AT TIME ZONE 'MSK') and
--a.data ->> 'state' = 'issued_success_state';
a.data @> '{ "state": "issued_success_state" }';
当我在 WHERE 子句中使用 @> 运算符时,使用了 gin 索引:
Aggregate (cost=406.91..406.92 rows=1 width=8)
-> Bitmap Heap Scan on activities a (cost=32.75..406.67 rows=95 width=0)
Recheck Cond: (data @> '{"state": "issued_success_state"}'::jsonb)
Filter: (created_at >= timezone('MSK'::text, (date_trunc('month'::text, (('now'::cstring)::date)::timestamp with time zone))::timestamp without time zone))
-> Bitmap Index Scan on index_activities_on_data (cost=0.00..32.73 rows=364 width=0)
Index Cond: (data @> '{"state": "issued_success_state"}'::jsonb)
并且未使用,当我使用 ->> 运算符时:
Aggregate (cost=59093.42..59093.43 rows=1 width=8)
-> Seq Scan on activities a (cost=0.00..59092.23 rows=477 width=0)
Filter: (((data ->> 'state'::text) = 'issued_success_state'::text) AND (created_at >= timezone('MSK'::text, (date_trunc('month'::text, (('now'::cstring)::date)::timestamp with time zone))::timestamp without time zone)))
请解释原因?
The default GIN operator class for jsonb supports queries with top-level key-exists operators ?, ?& and ?| operators and path/value-exists operator @>.
您可以创建一个 B 树索引,它可以与 ->>
运算符一起使用,例如
CREATE INDEX ON activities ((data->>'state'));
我有一个 jsonb 字段,其中包含如下数据:
{"state": "initialize_done_state", ... }
该字段的 GIN 索引:
CREATE INDEX index_activities_on_data ON activities USING gin (data)
并执行查询:
select count(*)
from activities a
where a.created_at >= (date_trunc('month', current_date)::timestamp AT TIME ZONE 'MSK') and
--a.data ->> 'state' = 'issued_success_state';
a.data @> '{ "state": "issued_success_state" }';
当我在 WHERE 子句中使用 @> 运算符时,使用了 gin 索引:
Aggregate (cost=406.91..406.92 rows=1 width=8)
-> Bitmap Heap Scan on activities a (cost=32.75..406.67 rows=95 width=0)
Recheck Cond: (data @> '{"state": "issued_success_state"}'::jsonb)
Filter: (created_at >= timezone('MSK'::text, (date_trunc('month'::text, (('now'::cstring)::date)::timestamp with time zone))::timestamp without time zone))
-> Bitmap Index Scan on index_activities_on_data (cost=0.00..32.73 rows=364 width=0)
Index Cond: (data @> '{"state": "issued_success_state"}'::jsonb)
并且未使用,当我使用 ->> 运算符时:
Aggregate (cost=59093.42..59093.43 rows=1 width=8)
-> Seq Scan on activities a (cost=0.00..59092.23 rows=477 width=0)
Filter: (((data ->> 'state'::text) = 'issued_success_state'::text) AND (created_at >= timezone('MSK'::text, (date_trunc('month'::text, (('now'::cstring)::date)::timestamp with time zone))::timestamp without time zone)))
请解释原因?
The default GIN operator class for jsonb supports queries with top-level key-exists operators ?, ?& and ?| operators and path/value-exists operator @>.
您可以创建一个 B 树索引,它可以与 ->>
运算符一起使用,例如
CREATE INDEX ON activities ((data->>'state'));