postgresql - Postgres 递归 json 限制

标签 postgresql recursive-query jsonb

引用Postgresql jsonb traversal

with recursive flat (id, timestamp, path, value) 
as (select id, timestamp, key, value 
    from trending_snapshot, jsonb_each(snapshot)
union select f.id, f.timestamp, j.key, j.value 
  from flat f, jsonb_each(f.value) j 
  where jsonb_typeof(f.value) = 'object' )
select timestamp, path, (value->>'value')::float AS value 
from flat 
where path like any(array['%Run01TubingPressure'])
limit 12;

在末尾添加限制确实限制了返回,但似乎在每个记录都被检查了。

是否可以在 with union 内部进行限制?

此查询在大型数据集上会受到严重影响。 不过,我确实看到我可以限制平面选择中的时间戳范围。

最佳答案

如果您要限制行数,则应在初始查询中添加 order bylimit,例如:

with recursive flat (id, timestamp, path, value) as (
    (select id, timestamp, key, value 
    from trending_snapshot, 
    jsonb_each(snapshot)
    order by id
    limit 12)
union all
    select f.id, f.timestamp, j.key, j.value 
    from flat f, 
    jsonb_each(f.value) j 
    where jsonb_typeof(f.value) = 'object' )
select timestamp, path, (value->>'value')::float AS value 
from flat 
where path like any(array['%Run01TubingPressure'])

或额外的 where 子句(在初始查询中)根据条件过滤行。

关于postgresql - Postgres 递归 json 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51448628/

相关文章:

sql-server - 一旦结果集包含某个值,就退出递归公用表表达式

sql - postgresQL 中子产品的递归列表

json - Oracle JSON_TABLE 到 PostgreSQL - 如何从 JSON 列中的第二个分层键进行搜索

postgresql - 在 Pl/pgSQL 函数中创建非冲突临时表

hibernate - JDBC 连接是否应该与 netstat 网络连接相匹配?

postgresql - psql: FATAL: 数据库 "<user>"不存在

postgresql - Ecto embeds_many 关联中的查询

sql - SQL差异的原因

mysql - 递归连接sql中的列

sql - jsonb LIKE 查询数组中的嵌套对象