gpt4 book ai didi

postgresql - 在 ILIKE postgres 查询上创建 GIN 索引

转载 作者:行者123 更新时间:2023-11-29 12:08:50 27 4
gpt4 key购买 nike

我正在使用 postgres 9.6。我将 JSONB 数据存储在一列中,我会提高一个查询的性能。在我的 json 数据中,我有 name 列,它是文本。我正在使用以下查询查询此数据。

SELECT "id", "body", "created_at", "updated_at" 
FROM "read_models"
WHERE ((body ->> 'name') ILIKE '%asd%')
LIMIT 40 OFFSET 0;

下面是这个查询的分析结果:

Limit  (cost=0.00..33.58 rows=40 width=72) (actual 
time=112.428..4071.757 rows=11 loops=1)
|
| -> Seq Scan on read_models
(cost=0.00..2636.90 rows=3141 width=72) (actual time=112.416..4071.646
rows=11 loops=1) |
| Filter: ((body ->> 'name'::text) ~~* '%asd%'::text)
|
| Rows Removed by Filter: 78516
|
| Planning time: 1.658 ms
|
| Execution time: 4071.847 ms

我为此属性创建了以下索引:

CREATE EXTENSION pg_trgm;
CREATE INDEX idx_name ON read_models USING gin ((body ->> 'name') gin_trgm_ops);

我创建了不同的索引类型,但结果始终相同。查询时间和没有任何索引一样。当我查询该数据时,我看到 PG 没有使用这个索引。我看到很多关于如何在 postgres 中索引文本数据的信息,但我不明白为什么它在我的情况下不起作用。感谢您的帮助。

最佳答案

问题是你的 where 条件不是 SARGABLE

What makes a SQL statement sargable?

您可以为 LIKE 'ABC%' 使用索引,但不能为 LIKE '%ABC%'

检查那些提示,即使对于 MySQL,大多数人也适用于 PostgresMySQL 索引 TIPS

关于postgresql - 在 ILIKE postgres 查询上创建 GIN 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47539068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com