gpt4 book ai didi

json - Postgres : Range Query on nested jsonb column

转载 作者:行者123 更新时间:2023-11-29 12:10:13 27 4
gpt4 key购买 nike

我正在使用 postgres 9.5.4。

从下面存储在 jsonb 列中的示例 json 数据,我想搜索匹配 o['mid'] > 7000

的记录

这是一个用户记录的样本。将有数百万这样的用户。

{
"uid": 14105529,
"o": [
{
"mid": 6551,
"ac": 1913,
"ip": "144.36.233.44",
"adw": 5,
"at": 133000,
"ad": 151015,
"aid": 0
},
{
"mid": 7552,
"ac": 1913,
"ip": "144.36.233.44",
"adw": 5,
"at": 133000,
"ad": 151015,
"aid": 0
},
{
"mid": 7553,
"ac": 1913,
"ip": "144.36.233.44",
"adw": 5,
"at": 133000,
"ad": 151015,
"aid": 0
}
]
}

最佳答案

with a_table(jdata) as ( 
values (
'{
"uid":14105529,
"o":[
{"mid":6551,"ac":1913,"ip":"144.36.233.44","adw":5,"at":133000,"ad":151015,"aid":0},
{"mid":7552,"ac":1913,"ip":"144.36.233.44","adw":5,"at":133000,"ad":151015,"aid":0},
{"mid":7553,"ac":1913,"ip":"144.36.233.44","adw":5,"at":133000,"ad":151015,"aid":0}
] }'::jsonb
)
)

select jdata->'uid' as uid, value
from a_table, jsonb_array_elements(jdata->'o')
where (value->>'mid')::int > 7000;

uid | value
----------+--------------------------------------------------------------------------------------------------
14105529 | {"ac": 1913, "ad": 151015, "at": 133000, "ip": "144.36.233.44", "adw": 5, "aid": 0, "mid": 7552}
14105529 | {"ac": 1913, "ad": 151015, "at": 133000, "ip": "144.36.233.44", "adw": 5, "aid": 0, "mid": 7553}
(2 rows)

由于需要使用 jsonb_array_elements() 解除 json 数组的嵌套,因此对于大型数据集而言,查询的开销会非常大。没有可用于加速的索引。

关于json - Postgres : Range Query on nested jsonb column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40041426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com