Generalized Inverted Index,通用倒排索引,是一个存储对(key, posting list)集合的索引结构,其中key是一个键值,而posting list 是一组出现过key的位置。如:'hello', '14:2 23:4' ,表示hello在14:2和23:4这两个元祖出现过。在KingbaseES 中这些位置实际上就是元组的 tid ,类似 Oracle Rowid。GIN 索引字段entry构造在末端posting tree 里面存储的是entry对应的行号,无其他信息。RUM 索引,结构与GIN类似,但是在posting tree的每一个ctid (itempoint) 后面会追加一些属性值。因此,有些场景,使用 RUM 索引,性能会优很多。以下举两个例子比较下。
Note: KingbaseES v8r6c5b0023 版本,附带了rum插件。
一、构造数据
create table t1 as select name,short_desc from pg_settings;
alter table t1 add column tsv tsvector;
update t1 set tsv=to_tsvector(short_desc);
--number for 紧邻的只有一条
test=# select short_desc from t1 where to_tsvector(short_desc) @@ to_tsquery('number <-> for');
short_desc
-------------------------
Top SQL number for kddm
(1 row)
--同时包含number and for 的有7条
test=# select short_desc from t1 where to_tsvector(short_desc) @@ to_tsquery('number & for');
short_desc
-------------------------------------------------------------------------------
Sets the number of digits displayed for floating-point values.
Sets the maximum number of simultaneously open files for each server process.
Sets the number of connection slots reserved for superusers.
Top SQL number for kddm
Sets the number of disk-page buffers in shared memory for WAL.
Sets the number of WAL files held for standby servers.
Sets the number of locks used for concurrent xlog insertions.
(7 rows)二、例子1:距离查询
1、gin 索引
创建gin 索引:
create index ind_t1_gin on t1 using gin(tsv);查看gin索引执行计划:通过索引返回7条记录,也就是索引没有包含位置的信息,需要访问表数据。
test=# explain analyze select short_desc from t1 where tsv @@ to_tsquery('number <-> for'); QUERY PLAN -------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on t1 (cost=12.32..30.68 rows=9 width=55) (actual time=0.111..0.195 rows=1 loops=1) Recheck Cond: (tsv @@ to_tsquery('number <-> for'::text)) Rows Removed by Index Recheck: 6 Heap Blocks: exact=4 -> Bitmap Index Scan on ind_t1_gin (cost=0.00..12.32 rows=9 width=0) (actual time=0.058..0.058 rows=7 loops=1) Index Cond: (tsv @@ to_tsquery('number <-> for'::text)) Planning Time: 0.199 ms Execution Time: 0.227 ms (8 rows)
2、rum 索引
创建索引:
test=# create extension rum; CREATE EXTENSION test=# create index ind_t1_rum on t1 using rum(tsv); CREATE INDEX查看执行计划:可以看到索引返回的记录就一条,也就是索引包含有位置信息。
test=# explain analyze select short_desc from t1 where tsv @@ to_tsquery('number <-> for'); QUERY PLAN -------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on t1 (cost=12.32..30.68 rows=9 width=55) (actual time=0.041..0.042 rows=1 loops=1) Recheck Cond: (tsv @@ to_tsquery('number <-> for'::text)) Heap Blocks: exact=1 -> Bitmap Index Scan on ind_t1_rum (cost=0.00..12.32 rows=9 width=0) (actual time=0.038..0.039 rows=1 loops=1) Index Cond: (tsv @@ to_tsquery('number <-> for'::text)) Planning Time: 0.297 ms Execution Time: 0.068 ms (7 rows)
三、例子2:相关性排序
1、gin 索引
需要所有符合的数据,再进行排序
test=# create index ind_t1_gin on t1 using gin(tsv);
CREATE INDEX
test=# explain analyze select short_desc from t1 where tsv @@ to_tsquery('number & for') order by tsv <=> to_tsquery('number & for') limit 1;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Limit (cost=33.00..33.00 rows=1 width=59) (actual time=0.134..0.135 rows=1 loops=1)
-> Sort (cost=33.00..33.02 rows=9 width=59) (actual time=0.133..0.134 rows=1 loops=1)
Sort Key: ((tsv <=> to_tsquery('number & for'::text)))
Sort Method: top-N heapsort Memory: 25kB
-> Bitmap Heap Scan on t1 (cost=12.32..32.95 rows=9 width=59) (actual time=0.102..0.125 rows=7 loops=1)
Recheck Cond: (tsv @@ to_tsquery('number & for'::text))
Heap Blocks: exact=4
-> Bitmap Index Scan on ind_t1_gin (cost=0.00..12.32 rows=9 width=0) (actual time=0.084..0.084 rows=7 loops=1)
Index Cond: (tsv @@ to_tsquery('number & for'::text))
Planning Time: 0.237 ms
Execution Time: 0.177 ms
(11 rows)2、rum 索引
test=# explain analyze select short_desc from t1 where tsv @@ to_tsquery('number & for') order by tsv <=> to_tsquery('number & for') limit 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Limit (cost=8.25..12.52 rows=1 width=59) (actual time=0.077..0.077 rows=1 loops=1)
-> Index Scan using ind_t1_rum on t1 (cost=8.25..46.68 rows=9 width=59) (actual time=0.076..0.076 rows=1 loops=1)
Index Cond: (tsv @@ to_tsquery('number & for'::text))
Order By: (tsv <=> to_tsquery('number & for'::text))
Planning Time: 0.236 ms
Execution Time: 0.101 ms
(6 rows)最后修改时间:2024-08-20 16:45:17
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




