Postgres 递归 json 限制

Postgres recursive json limiting

引用

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;

最后添加 limit 确实限制了 return 但似乎里面的每条记录都被检查了。

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

此查询在大型数据集上受到严重影响。 但是我确实看到我可以限制单位 select.

中的时间戳范围

如果您要限制行数,您应该在初始查询中添加 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 子句(在初始查询中)根据条件过滤行。