小编Ser*_*Ser的帖子

在 Postgres 中索引 jsonb 数组

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

环境

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

{
    "properties": {
        "pdi": {
            "contextInformation": {
                "tags": ["SOME_TAG"]
            },
    },
}
Run Code Online (Sandbox Code Playgroud)

表创建:

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

CREATE INDEX idx_aip_tags 
ON t_aip 
USING gin ((json_aip -> 'properties' -> 'pdi' -> 'contextInformation' -> 'tags'));
Run Code Online (Sandbox Code Playgroud)

运营商查询

我们不能?|像使用 JDBC 一样使用运算符。但是有传言说当我运行这种类型的查询时我应该看到我的索引。

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

结果:

  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 …
Run Code Online (Sandbox Code Playgroud)

postgresql indexing jsonb

5
推荐指数
1
解决办法
2258
查看次数

标签 统计

indexing ×1

jsonb ×1

postgresql ×1