【发布时间】:2018-05-13 05:38:09
【问题描述】:
我有一个表people,其中body 列为jsonb 类型。
Table "public.people"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-----------------+-----------------------------+-----------+----------+--------------------+----------+--------------+-------------
id | uuid | | not null | uuid_generate_v4() | plain | |
body | jsonb | | not null | | extended | |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
"idx_name" gin ((body ->> 'name'::text) gin_trgm_ops)
我的索引如下:
CREATE INDEX idx_name ON people USING gin ((body ->> 'name') gin_trgm_ops);
但是,当我这样做时:
EXPLAIN ANALYZE SELECT * FROM "people" WHERE ((body ->> 'name') ILIKE '%asd%') LIMIT 40 OFFSET 0;
我明白了:
Limit (cost=0.00..33.58 rows=40 width=104) (actual time=100.037..4066.964 rows=11 loops=1)
-> Seq Scan on people (cost=0.00..2636.90 rows=3141 width=104) (actual time=99.980..4066.782 rows=11 loops=1)
Filter: ((body ->> 'name'::text) ~~* '%asd%'::text)
Rows Removed by Filter: 78516
Planning time: 0.716 ms
Execution time: 4067.038 ms
为什么那里没有使用索引?
【问题讨论】:
-
你找到答案了吗?我面临同样的问题:JSONB 值中的字符串无法将三元组索引与 ILIKE 一起使用。
标签: sql json postgresql full-text-search sql-like