在Postgres中索引jsonb数组 [英] Indexing a jsonb array in Postgres

查看:139
本文介绍了在Postgres中索引jsonb数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置GIN索引,但无论运行运算符还是函数,我都认为在运行请求时不使用索引.

I try to set up a GIN index but I do not think my index is used when I run the request, whether I use an operator or a function.

在我们的表中,我们有一个JSONB字段(json_aip),其中包含如下所示的Json:

In our table we have a JSONB field (json_aip) containing a Json that looks like that:

{
    "properties": {
        "pdi": {
            "contextInformation": {
                "tags": ["SOME_TAG"]
            },
    },
}

表创建:

create table t_aip (
    json_aip jsonb,
    [...]
);

CREATE INDEX idx_aip_tags 
ON t_aip 
USING gin ((json_aip -> 'properties' -> 'pdi' -> 'contextInformation' -> 'tags'));

操作员查询

因为使用JDBC,所以不能使用运算符?|.但是谣言表明,当我运行这种类型的查询时,我应该看到我的索引.

Operator query

We can't use the operator ?| as we use JDBC. But rumors indicate I should see my index when I run that type of query.

EXPLAIN ANALYZE SELECT count(*)  
FROM storage.t_aip 
WHERE json_aip#>'{properties,pdi,contextInformation,tags}' ?| array['SOME_TAG']

结果:

  Aggregate

  (cost=27052.16..27052.17 rows=1 width=8) (actual time=488.085..488.087 rows=1 loops=1)
  ->  Seq Scan on t_aip  (cost=0.00..27052.06 rows=42 width=0) (actual time=0.134..456.978 rows=16502 loops=1)
        Filter: ((json_aip #> '{properties,pdi,contextInformation,tags}'::text[]) ?| '{SOME_TAG}'::text[])
        Rows Removed by Filter: 17511
Planning time: 23.202 ms
Execution

time: 488.449 ms

功能查询

EXPLAIN ANALYZE SELECT count(*)  
FROM storage.t_aip 
WHERE jsonb_exists_any(
    json_aip#>'{properties,pdi,contextInformation,tags}', 
    array['SOME_TAG']
)

结果:

QUERY PLAN
Aggregate  (cost=27087.00..27087.01 rows=1 width=8) (actual time=369.931..369.933 rows=1 loops=1)
  ->  Seq Scan on t_aip  (cost=0.00..27052.06 rows=13979 width=0) (actual time=0.173..350.437 rows=16502 loops=1)
        Filter: jsonb_exists_any((json_aip #> '{properties,pdi,contextInformation,tags}'::text[]), '{SOME_TAG}'::text[])
        Rows Removed by Filter: 17511
Planning time: 56.021 ms
Execution time: 370.252 ms

完全没有关于索引的信息.任何帮助将非常感激 !

There is nothing about the index at all. Any help would be much appreciated !

我认为我的索引是错误的,因为它认为在路径json_aip -> 'properties' -> 'pdi' -> 'contextInformation' -> 'tags'的末尾它为一个字符串索引是否是数组.那是我的看法.

I think my index is wrong because it considers that at the end of the path json_aip -> 'properties' -> 'pdi' -> 'contextInformation' -> 'tags' it index a String whether that's an array. That's my opinion.

推荐答案

有一个通用规则,您必须在索引和查询中使用完全相同的表达式才能使用该索引.有了这个索引:

There is a general rule that you have to use exactly the same expression both in an index and a query to use the index. With this index:

CREATE INDEX idx_aip_tags 
ON t_aip 
USING gin ((json_aip#>'{properties,pdi,contextInformation,tags}'));

查询将使用索引

EXPLAIN ANALYZE 
SELECT count(*)  
FROM t_aip 
WHERE json_aip#>'{properties,pdi,contextInformation,tags}' ?| array['SOME_TAG']

                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=149.97..149.98 rows=1 width=0) (actual time=27.783..27.783 rows=1 loops=1)
   ->  Bitmap Heap Scan on t_aip  (cost=20.31..149.87 rows=40 width=0) (actual time=1.504..25.726 rows=20000 loops=1)
         Recheck Cond: ((json_aip #> '{properties,pdi,contextInformation,tags}'::text[]) ?| '{SOME_TAG}'::text[])
         Heap Blocks: exact=345
         ->  Bitmap Index Scan on idx_aip_tags  (cost=0.00..20.30 rows=40 width=0) (actual time=1.455..1.455 rows=20000 loops=1)
               Index Cond: ((json_aip #> '{properties,pdi,contextInformation,tags}'::text[]) ?| '{SOME_TAG}'::text[])

请注意,GIN索引还支持@>运算符:

Note that the GIN index supports also @> operator:

SELECT count(*)  
FROM t_aip 
WHERE json_aip#>'{properties,pdi,contextInformation,tags}' @> '["SOME_TAG"]'

但是搜索多个标签时要小心:

but be careful when searching for more than one tag:

SELECT count(*)  
FROM t_aip 
-- this gives objects containing both tags:
-- WHERE json_aip#>'{properties,pdi,contextInformation,tags}' @> '["SOME_TAG", "ANOTHER_TAG"]'
-- and this gives objects with any of two tags:
WHERE json_aip#>'{properties,pdi,contextInformation,tags}' @> ANY(ARRAY['["SOME_TAG"]', '["ANOTHER_TAG"]']::jsonb[])

这篇关于在Postgres中索引jsonb数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆