GIN 索引
GIN(Generalized Inverted Index, 通用倒排索引) 是一个存储对(key, posting list)集合的索引结构,其中key是一个键值,而posting list 是一组出现过key的位置。如(‘hello', '14:2 23:4')中,表示hello在14:2和23:4这两个元祖出现过,在PG中这些位置实际上就是元组的tid(行号,包括数据块ID(32bit),以及item point(16 bit) )。
对于表中的每一个属性,在建立对应 gin 索引时,都可能会被解析为多个键值,所以同一个元组的tid可能会出现在多个key的posting list中。
一、索引的逻辑结构
GIN索引在逻辑上可以看成一个relation,该relation有两种结构:
1. 只索引基表的一列的情况:
| key | value |
|---|---|
| Key1 | Posting list( or posting tree) |
| Key2 | Posting list( or posting tree) |
| … | … |
2. 索引基表的多列(复合、多列索引):
| column_id | key | value |
|---|---|---|
| Column1 num | Key1 | Posting list( or posting tree) |
| Column2 num | Key1 | Posting list( or posting tree) |
| Column3 num | Key1 | Posting list( or posting tree) |
| ... | ... | ... |
这种结构,对于基表中不同列的相同的key,在GIN索引中也会当作不同的key来处理。
二、索引的物理结构
GIN索引在物理存储上包含如下内容:
1. Entry:GIN索引中的一个元素,可以认为是一个词位,也可以理解为一个key
2. Entry tree:在Entry上构建的B树
3. posting list:一个Entry出现的物理位置(heap ctid, 堆表行号)的链表
4. posting tree:在一个Entry出现的物理位置链表(heap ctid, 堆表行号)上构建的B树,所以posting tree的KEY是ctid,而entry tree的KEY是被索引的列的值
5. pending list:索引元组的临时存储链表,用于fastupdate模式的插入操作
具体结构如下:

索引实际例子如下:

三、GIN索引使用例子
1、前后模糊查询
创建测试数据:
test=# create extension sys_trgm;
CREATE EXTENSION test=# create table t1_text(doc text);
CREATE TABLE
test=# insert into t1_text select short_desc from pg_settings;
INSERT 0 410 test=# create index ind_t1_text on t1_text using gin(doc gin_trgm_ops);
CREATE INDEX
查看执行计划:
test=# explain select * from t1_text where doc like '%mod%';
QUERY PLAN
---------------------------------------------------------------------------
Bitmap Heap Scan on t1_text (cost=12.06..17.16 rows=8 width=55)
Recheck Cond: (doc ~~ '%mod%'::text)
-> Bitmap Index Scan on ind_t1_text (cost=0.00..12.06 rows=8 width=0)
Index Cond: (doc ~~ '%mod%'::text)
(4 rows)
结论:可以看到,GIN 索引支持前后模糊查询。
注意:要使用gin索引,必须至少要有三个字符,如以上例子 mod 是三个字符。
2、全文检索
GIN 索引实际上更多的用于全文检索的情景。
准备数据:
alter table t1_text add (doc_ts tsvector);
update t1_text set doc_ts=to_tsvector(doc);
create index ind_t1_ts on t1_text using gin(doc_ts);
查看执行结果:
test=# explain select * from t1_text where doc_ts @@ to_tsquery('command');
QUERY PLAN
------------------------------------------------------------------------
Bitmap Heap Scan on t1_text (cost=8.32..25.71 rows=9 width=179)
Recheck Cond: (doc_ts @@ to_tsquery('command'::text))
-> Bitmap Index Scan on ind_t1_ts (cost=0.00..8.32 rows=9 width=0)
Index Cond: (doc_ts @@ to_tsquery('command'::text))
(4 rows)
test=# select doc from t1_text where doc_ts @@ to_tsquery('command');
doc
---------------------------------------------------------------------------
Sets the shell command that will be executed at every restart point.
Sets the shell command that will be called to archive a WAL file.
Allows archiving of WAL files using archive_command.
Logs each replication command.
Sets the shell command that will be executed once at the end of recovery.
Sets the shell command that will retrieve an archived WAL file.
Command to obtain passphrases for SSL.
Also use ssl_passphrase_command during server reload.
Updates the process title to show the active SQL command.
(9 rows)
test=# select doc from t1_text where doc_ts @@ to_tsquery('comman');
doc
-----
(0 rows)
四、gin 索引可用于超长的字段
test=# create table tab1(id1 text,id2 text );
CREATE TABLE test=# alter table tab1 alter column id2 set storage external;
ALTER TABLE
test=# insert into tab1 select *,repeat(id1,10000) from generate_series(1,10000) id1;
INSERT 0 10000
test=# create index ind_tab1 on tab1(id2);
ERROR: index row requires 10016 bytes, maximum size is 8191
test=# create index ind_tab1 on tab1 using gin(id2 gin_trgm_ops);
CREATE INDEX
gin 索引之所以支持超长数据,这是因为gin 索引的 key 是关键词位,而非整条记录。
GIN 索引的更多相关文章
- 浅谈postgresql的GIN索引(通用倒排索引)
1.倒排索引原理 倒排索引来源于搜索引擎的技术,可以说是搜索引擎的基石.正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作.在详细说明倒排索引之前,我们说一下与之相关的正排索引并与之 ...
- postgresql 创建gin索引
1.创建gin类型的索引 postgresql 创建gin索引遇到的问题:1.ERROR: operator class "gin_trgm_ops" does not exist ...
- gin索引优化实例1
GIN(Generalized Inverted Index, 通用倒排索引) 是一个存储对(key, posting list)集合的索引结构,其中key是一个键值,而posting list 是一 ...
- postgresql gin索引使用
由于属于老项目,postgresql使用版本9.6,主要解决‘%name%"查询无法使用索引问题.pg_trgm模块提供函数和操作符测定字母,数字,文本基于三元模型匹配的相似性, 还有支持快 ...
- GIN and RUM 索引性能比较
gin索引字段entry构造的TREE,在末端posting tree|list 里面存储的是entry对应的行号. 别无其他信息.rum索引,与GIN类似,但是在posting list|tree的 ...
- psql-09表:视图和索引
视图 由查询语句定义的虚拟表;从视图中看到的数据可能来自数据库中的一张或多张表,也可能来自外部; 使用视图的原因一般有: 使复制的查询易于理解和使用; 安全原因; 表一些函数返回的结果映射成视图; 一 ...
- PostgreSQL自学笔记:9 索引
9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...
- PostgreSQL索引介绍
h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...
- postgres 索引
索引是一种特殊的查询表,可以使用搜索引擎的数据库以加快数据检索.简单地说,索引是表中的数据的一个指针,在一个数据库中的索引是非常相似,如:一本书的目录. 例如,如果想在一本书中引用的所有页面讨论某个话 ...
随机推荐
- ERROR: manifest for elasticsearch:latest not found: manifest unknown: manife
当我们用docker下载 elasticsearch 的时候出现如下错误: 这里错误的原因是没有发现最新版,需要我们指定版本. docker pull elasticsearch:7.12.0 那我们 ...
- HelloWord程序代码的编写和HelloWord程序的编译运行
1.新建文件夹,存放代码 2.新建一个Java文件 文件后缀名.java(Hello.java) 3.编写代码public class Hello{public static void main(St ...
- java---数组(重点概念)
一.什么是数组 程序=算法+数据结构 数据结构:把数据按照某种特定的结构保存,设计一个合理的数据是解决问题的关键: 数组:是一种用于存储多个相同类型数据类型 的存储模型: 数组的特定结构:相同类型组成 ...
- Java面向对象(下)作业
首先我把题目先列到这里,可以仔细看一下题. (1)设计一个名为Geometric的几何图形的抽象类,该类包括: ①两个名为color.filled属性分别表示图形颜色和是否填充. ②一个无参的构造方法 ...
- CMP0065警告问题
参考链接: https://cmake.org/cmake/help/latest/policy/CMP0065.html https://cmake-developers.cmake.narkive ...
- Solution -「HDU」Professor Ben
Description 有 \(Q\) 个询问.每次给定一个正整数 \(n\),求它的所有因数的质因数个数的和. Solution 就讲中间的一个 Trick. 我们定义正整数 \(x\) 有 \(f ...
- [javaweb]javaweb中HttpServletResponse实现文件下载,验证码和请求重定向功能
HttpServletResponse web服务器接受到客户端的http请求之后,针对这个请求,分别创建一个代表请求的httpServletRequest和代表响应的HttpServletRespo ...
- python 生成Windows快捷方式
此处以虚拟机镜像快捷方式为例 link_filepath:快捷方式的生成路径 win32_cmd:需要执行的应用程序 arg_str:exe的参数 快捷方式的目标:由win32_cmd + arg_s ...
- 【AcWing】第 62 场周赛 【2022.07.30】
AcWing 4500. 三个元素 题目描述 给定一个长度为 \(n\) 的数组 \(r\_1,r\_2,-,r\_n\). 请你找到其中的三个元素 \(r\_a,r\_b,r\_c\),使得 \(r ...
- Javaweb05-Ajax
1.基于jQuery的Ajax 1.1 基本Ajax 参数 说明 url 请求地址 type 请求类型 data 请求参数 dataType 返回参数 success 成功处理函数 error 错误处 ...