简体   繁体   English

如何索引 JSONB 字段中数组内的对象?

[英]How to index objects inside array in JSONB field?

I want to index the JSONB field with GIN indexing.我想用GIN索引index JSONB字段。 Inside this field, I have an array of objects .在这个字段中,我有一个objects array Precisely, this is how it looks (shortened second object with three dots):准确地说,它看起来是这样的(用三个点缩短了第二个object ):

[
    {
        "tags": ["student work", "fast apply"],
        "intensity": [
            {
                "shift": "fulltime",
                "period": "hours",
                "duration": "9"
            },
            {
                "shift": "parttime",
                "period": "hours",
                "duration": "4"
            }
        ]
    },
    { ... }
]

This is how I filter this table in WHERE clause:这就是我在WHERE子句中过滤此表的方式:

items.intensity @? '$[*] ? (@.tags == "student work" || @.tags == "undefined" || @.tags.size() == 0) ? (@.intensity[*].shift == "fulltime")'

This is the index I tried but didn't work:这是我试过但没有用的索引:

CREATE INDEX idxginintensitytags ON items USING GIN (intensity jsonb_path_ops);

Explain analyze:解释分析:

    #   Node    Rows    Loops
Actual
1.  Bitmap Heap Scan on items as items (rows=154922 loops=1)
Recheck Cond: (intensity @? '$[*]?(@."intensity"[*]."shift" == "fulltime")'::jsonpath)
Heap Blocks: exact=33478
154922  1
2.  Bitmap Index Scan using idxginintensitytags (rows=154922 loops=1)
Index Cond: (intensity @? '$[*]?(@."intensity"[*]."shift" == "fulltime")'::jsonpath)
154922  1

I want to filter my table by tags , shifts , periods , and durations .我想按tagsshiftsperiodsdurations过滤我的table I have 200,000 rows in that table .我在该table中有 200,000 rows

How can I index this field ?我如何index这个field

I am using the latest version - PostgreSQL 13 .我正在使用最新版本 - PostgreSQL 13

In my hands, a problem is that it does use the index, even though doing so is slower.在我手中,一个问题是它确实使用了索引,尽管这样做速度较慢。 And the reason for that is that @.tags.size() == 0 cannot be determined by the index, so it ends ups returning all table rows to be rechecked, but the planner evidently doesn't realize this will happen.原因是@.tags.size() == 0不能由索引确定,所以它最终返回所有要重新检查的表行,但计划者显然没有意识到会发生这种情况。

Can you express this concept in a different way?你能用不同的方式表达这个概念吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM