代码之家  ›  专栏  ›  技术社区  ›  Michaela Ervin

Postgres递归json限制

  •  2
  • Michaela Ervin  · 技术社区  · 6 年前

    引用 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 EVERY记录中进行了检查。

    有可能限制在一个有工会的内部吗?

    这个查询在大数据集上非常困难。 不过,我确实看到可以在平面选择中限制时间戳范围。

    1 回复  |  直到 6 年前
        1
  •  2
  •   klin    6 年前

    如果要限制应该添加的行数 order by limit 在初始查询中,例如:

    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 子句(在初始查询中)根据条件筛选行。