首页 > 解决方案 > 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 亿行,那么可能的分辨率/设计更改是什么来克服这个问题。

问候普拉文

标签: postgresql

解决方案


您正在检索表的近 2%。使用索引是最快的方法并不明显。

如果你想强制它使用索引,你可以set enable_seqscan=off并且它可能会使用索引然后

或者,如果您想要做的只是查看索引工作“自己拥有”,也许您可​​以选择更具选择性的搜索内容。


推荐阅读