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 索引的更多相关文章

  1. 浅谈postgresql的GIN索引(通用倒排索引)

    1.倒排索引原理 倒排索引来源于搜索引擎的技术,可以说是搜索引擎的基石.正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作.在详细说明倒排索引之前,我们说一下与之相关的正排索引并与之 ...

  2. postgresql 创建gin索引

    1.创建gin类型的索引 postgresql 创建gin索引遇到的问题:1.ERROR: operator class "gin_trgm_ops" does not exist ...

  3. gin索引优化实例1

    GIN(Generalized Inverted Index, 通用倒排索引) 是一个存储对(key, posting list)集合的索引结构,其中key是一个键值,而posting list 是一 ...

  4. postgresql gin索引使用

    由于属于老项目,postgresql使用版本9.6,主要解决‘%name%"查询无法使用索引问题.pg_trgm模块提供函数和操作符测定字母,数字,文本基于三元模型匹配的相似性, 还有支持快 ...

  5. GIN and RUM 索引性能比较

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

  6. psql-09表:视图和索引

    视图 由查询语句定义的虚拟表;从视图中看到的数据可能来自数据库中的一张或多张表,也可能来自外部; 使用视图的原因一般有: 使复制的查询易于理解和使用; 安全原因; 表一些函数返回的结果映射成视图; 一 ...

  7. PostgreSQL自学笔记:9 索引

    9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...

  8. PostgreSQL索引介绍

    h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...

  9. postgres 索引

    索引是一种特殊的查询表,可以使用搜索引擎的数据库以加快数据检索.简单地说,索引是表中的数据的一个指针,在一个数据库中的索引是非常相似,如:一本书的目录. 例如,如果想在一本书中引用的所有页面讨论某个话 ...

随机推荐

  1. python新建一个目录

    源码部分 import os # 创建目录 def mkdir(path): isExists = os.path.exists(path) if not isExists: os.makedirs( ...

  2. cve_2019_0708_bluekeep漏洞

    一.环境说明 kali linux windows 7 sp1 二.cve_2019_0708_bluekeep漏洞利用 msf5 auxiliary(dos/windows/rdp/ms12_020 ...

  3. Python简单实现自动评论、自动点赞、自动关注脚本

    一些哔哔: 今天的这个脚本,是一个别人发的外包,交互界面的代码就不在这里说了,但是可以分享下自动评论.自动点赞.自动关注.采集评论和视频的数据是如何实现的 开发环境 python 3.8 运行代码py ...

  4. 【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示

    前言: MQTT广泛应用于工业物联网.智能家居.各类智能制造或各类自动化场景等.MQTT是一个基于客户端-服务器的消息发布/订阅传输协议,在很多受限的环境下,比如说机器与机器通信.机器与物联网通信等. ...

  5. identity server4 授权成功页面跳转时遇到错误:Exception: Correlation failed. Unknown location的解决方法

    一.异常信息描述 错误信息,看到这个页面是否耳熟能详担又不知道怎么解决 ,坑死个人不偿命,,,,,,,, 二.处理方法 1.在web项目中增加类SameSiteCookiesServiceCollec ...

  6. 【Unity基础知识】基础游戏单位GameObject中常用的属性和API

    一.GameObject中的成员变量 主要思想:得到该脚本依附的GameObject的相关信息 现有: Lesson4的代码: using System.Collections; using Syst ...

  7. 如何给selenium.chrome写扩展拦截或转发请求

    Selenium.WebDriver Selenium WebDriver 是一组开源 API,用于自动测试 Web 应用程序,利用它可以通过代码来控制chrome浏览器! 有时候我们需要mock接口 ...

  8. 2022-7-12 第五组 pan小堂 js

    JavaScript Switch 语句 (熟悉掌握) 请使用 switch 语句来选择多个需被执行的代码块之一. 语法: switch(表达式){ case n: 代码块 break; case n ...

  9. vue 数据更新了但视图没改变?试试 $set

    场景 编辑表格中某行数据时,需要把它赋值给对话框表单 this.form,如果直接用 = 赋值,会导致:表单的输入框内容无法二次编辑. 使用 Vue-dev-tool 的 Components 功能测 ...

  10. Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上

    概述 定义 dolphinscheduler 官网地址 https://dolphinscheduler.apache.org/ dolphinscheduler GitHub地址 https://g ...