我正在使用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
}
]
}
答案 0 :(得分:2)
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数组。
没有可用于加速的索引。