0

我想要索引indexJSONB字段。GIN在这个领域内,我有一个arrayof objects。确切地说,这就是它的外观(object用三个点缩短第二个):

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

这就是我在WHERE子句中过滤此表的方式:

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

这是我试过但没有用的索引:

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

解释分析:

    #   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

我想tabletagsshiftsperiods和过滤我的durations。我有200,000 rowstable

我怎么能index这样field

我正在使用最新版本 - PostgreSQL 13.

4

1 回答 1

1

在我的手中,一个问题是它确实使用了索引,尽管这样做比较慢。其原因是@.tags.size() == 0无法由索引确定,因此最终会返回所有要重新检查的表行,但规划者显然没有意识到会发生这种情况。

你能用不同的方式表达这个概念吗?

于 2021-08-20T23:49:30.883 回答