gin索引字段entry构造的TREE,在末端posting tree|list 里面存储的是entry对应的行号. 别无其他信息。rum索引,与GIN类似,但是在posting list|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)

  

最新文章

  1. MongoDB win安装后无法远程连接访问
  2. Excel 导入 导出 Microsoft
  3. 07_XPath_02_常用语法
  4. codevs 3336 电话网络
  5. EntityFramework 插件之EntityFramework.Extended (批量处理)
  6. 配置Windows下编译运行C/C++过程
  7. JAVA 探究NIO
  8. 关于pom.xml文件中引入net.sf.json-lib出错问题
  9. 【git】git撤销与回滚
  10. python pyMysql 自定义异常 函数重载
  11. GNU make简介
  12. Oracle 空间查询, 数据类型为 sdo_geometry
  13. Tinkoff Challenge - Elimination Round D. Presents in Bankopolis(区间DP)
  14. 每天一个linux命令集
  15. wifi免密码登录认证流程
  16. linux系统调用是通过软中断实现的吗
  17. mysql的空闲8小时问题
  18. Oracle判断两个时间段是否相交
  19. vuex中filter的使用 &amp;&amp; 快速判断一个数是否在一个数组中
  20. 怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优

热门文章

  1. SAP string 转 number 类型
  2. mybatis查询的三种方式
  3. Fiddler开启调试模式
  4. 渗透测试(PenTest)基础指南
  5. 09 MySQL_SQL日期函数和聚合函数
  6. 造!又有新的生产力语言了「GitHub 热点速览 v.22.30」
  7. async和await详解
  8. InvalidClassException异常_原理和解决方案和练习_序列化集合
  9. flex 我所理解不够深刻的内容
  10. YII学习总结3(session)