postgresql - GIN Index 大型数据集的问题
问题描述
我正在对 Jsonb 类型数据的 GIN 索引进行实验。直到我在一个表中有 500K 行,GIN 索引正在工作,但是当我将数据增加到表中的 5000 万行时。GIN 索引被创建,但是解释分析似乎没有考虑索引工作。
postgres=# \d users
Table "public.users"
Column | Type | Collation | Nullable | Default
-----------+---------+-----------+----------+------------------------------------
_id | integer | | not null | nextval('users__id_seq'::regclass)
id | integer | | not null |
attribute | integer | | not null |
value | jsonb | | not null |
Indexes:
"users_pkey" PRIMARY KEY, btree (_id)
"user_value_gin" gin (value)
Foreign-key constraints:
"users_attribute_fkey" FOREIGN KEY (attribute) REFERENCES attribute(id)
=======================================
postgres=# explain analyze select * from users where value @> '{"v": "Rahul"}'::jsonb;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..1017776.57 rows=897059 width=70) (actual time=880.003..23596.085 rows=913446 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on users (cost=0.00..927070.67 rows=373775 width=70) (actual time=451.196..22468.458 rows=304482 loops=3)
Filter: (value @> '{"v": "Rahul"}'::jsonb)
Rows Removed by Filter: 16362185
Planning Time: 105.326 ms
JIT:
Functions: 6
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 502.947 ms, Inlining 676.053 ms, Optimization 560.960 ms, Emission 94.573 ms, Total 1834.533 ms
Execution Time: 24099.549 ms
(12 rows)
我的问题是 Postgres 对 GIN 索引有什么限制吗?
如果是的话,如果我的要求是在一个表中有大约 500 亿行,那么可能的分辨率/设计更改是什么来克服这个问题。
问候普拉文
解决方案
您正在检索表的近 2%。使用索引是最快的方法并不明显。
如果你想强制它使用索引,你可以set enable_seqscan=off
并且它可能会使用索引然后
或者,如果您想要做的只是查看索引工作“自己拥有”,也许您可以选择更具选择性的搜索内容。
推荐阅读
- javascript - 在本机 iOS 上似乎没有调用 this.setState
- c# - 如何设置背景无颜色 ASP.NET C#
- android - 使用 SpongyCastle 在某些操作系统版本上生成非常慢的密钥
- typescript - 带有 rxjs 的打字稿
- python - 将 python 导入语句添加到 Pyspark 代码
- tsql - 具有空值的相关子查询
- validation - 验证后如何在 Laravel 控制器中捕获并返回错误?
- nginx - Ubuntu 18.04 Nginx - 总是只显示欢迎页面
- python - 自定义小部件作为 ListView 中的元素
- node.js - {"msg":"invalid_request"} 当我在 Nodejs 中使用 nodemailer 从 Gmail 发送邮件时出现错误