数据库 jsonb字段gin索引对比GiST索引

原文: https://www.postgresql.org/docs/9.4/textsearch-indexes.html

  • GIN index lookups are about three times faster than GiST

  • GIN indexes take about three times longer to build than GiST

  • GIN indexes are moderately slower to update than GiST indexes, but about 10 times slower if fast-update support was disabled (see Section 58.4.1 for details)

  • GIN indexes are two-to-three times larger than GiST indexes

As a rule of thumb, GIN indexes are best for static data because lookups are faster. For dynamic data, GiST indexes are faster to update. Specifically, GiST indexes are very good for dynamic data and fast if the number of unique words (lexemes) is under 100,000, while GIN indexes will handle 100,000+ lexemes better but are slower to update.

Note that GIN index build time can often be improved by increasing (https://www.postgresql.org/docs/9.4/runtime-config-resource.html#GUC-MAINTENANCE-WORK-MEM), while GiST index build time is not sensitive to that parameter.

Partitioning of big collections and the proper use of GiST and GIN indexes allows the implementation of very fast searches with online update. Partitioning can be done at the database level using table inheritance, or by distributing documents over servers and collecting search results using the dblink module. The latter is possible because ranking functions use only local information.


在 PostgreSQL 13 中创建 GIN 索引非常简单。GIN 索引是一种广泛用于全文搜索和文档相似度匹配的索引类型。 要创建 GIN 索引,可以使用 `CREATE INDEX` 语句并指定要创建的索引的名称和表的名称。在括号内,需要指定要创建 GIN 索引的列。例如,假设我们要在名为 "books" 的表的 "title" 列上创建 GIN 索引,我们可以执行以下命令: ``` CREATE INDEX gin_index_title ON books USING gin (title); ``` 这将创建一个名为 "gin_index_title" 的 GIN 索引,它将加快在 "books" 表的 "title" 列上的查询速度。 当创建 GIN 索引时,还可以指定其他选项以进一步自定义索引的行为。例如,您可以使用 `WITH (fastupdate = off)` 选项禁用快速更新模式以提高索引构建性能。这样可以加快索引的构建速度,但在插入或更新操作时可能会降低性能。 ``` CREATE INDEX gin_index_title ON books USING gin (title) WITH (fastupdate = off); ``` 除了单列索引外,还可以在多列上创建 GIN 索引。在括号内,指定要创建 GIN 索引的多个列,并使用逗号分隔它们。例如,我们要在 "books" 表的 "title" 和 "author" 列上创建 GIN 索引,可以执行以下命令: ``` CREATE INDEX gin_index_title_author ON books USING gin (title, author); ``` 总而言之,使用 `CREATE INDEX` 命令和 `USING gin` 子句,可以在 PostgreSQL 13 中轻松创建 GIN 索引。根据需要自定义选项,可以创建单列或多列的 GIN 索引来提高查询性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值