简体   繁体   English

在 postgres 的 FROM 子句中使用 FETCH

[英]Using FETCH inside a FROM clause in postgres

So I had a statement:所以我有一个声明:

SELECT jsonb_build_object(
            'type',       'Feature',
            'geometry',   ST_AsGeoJSON(line)::jsonb,
            'properties', to_jsonb(inputs) - 'line'
        ) as feature from (select line, line_types from table_a) inputs;

which worked fine except the select statement inside the FROM clause was returning too many rows.除了 FROM 子句中的 select 语句返回太多行之外,它工作正常。 So I want to just FETCH to just do x rows at a time.所以我只想 FETCH 一次只做 x 行。 I declared a cursor on select line, line_types from table_a and when I did FETCH FORWARD 5 FROM mycur;我在选择行上声明了一个游标,来自 table_a 的 line_types 以及当我从 mycur 执行 FETCH FORWARD 5; it returned the first 5 rows.它返回了前 5 行。 Yet when I try to replace the select with fetch, it won't let me:然而,当我尝试用 fetch 替换 select 时,它不会让我:

SELECT jsonb_build_object(
            'type',       'Feature',
            'geometry',   ST_AsGeoJSON(line)::jsonb,
            'properties', to_jsonb(inputs) - 'line'
        ) from (FETCH 1 FROM mycur) inputs;
ERROR:  syntax error at or near "FETCH"
LINE 5:         ) from (FETCH 1 FROM mycur) inputs;
                        ^

i assume you can't use fetch in a subquery?我假设您不能在子查询中使用 fetch ? I tried我试过

SELECT jsonb_build_object(
            'type',       'Feature',
            'geometry',   ST_AsGeoJSON(line)::jsonb,
            'properties', to_jsonb(inputs) - 'line'
        ) as feature from (select FETCH 1 FROM mycur) inputs;
ERROR:  syntax error at or near "1"
LINE 5:         ) as feature from (select FETCH 1 FROM mycur) inputs...
                                                ^

I do see that you can use just the FETCH statement in SELECT but thats not really what I want like:我确实看到您可以在 SELECT 中只使用 FETCH 语句,但这并不是我真正想要的:

SELECT jsonb_build_object(
            'type',       'Feature',
            'geometry',   ST_AsGeoJSON(line)::jsonb,
            'properties', to_jsonb(inputs) - 'line'
        ) as feature from (select line, line_type from table_a fetch first 10 rows only) inputs;

is there now way to use FETCH FROM Cursor as a subquery?现在有没有办法使用 FETCH FROM Cursor 作为子查询?

No, using FETCH in a subquery is not possible.不,在子查询中使用FETCH是不可能的。

You should look into using keyset pagination :您应该考虑使用键集分页

Let's assume that the primary key of table_a is id .我们假设table_a的主键是id

Then you start with然后你开始

SELECT jsonb_build_object(...) AS feature,
       id
FROM (SELECT line, line_type
      FROM table_a
      ORDER BY id
      FETCH FIRST 10 ROWS ONLY) AS inputs;

similar to what you have in mind.类似于你的想法。

Now remember the highest id returned from that query;现在记住该查询返回的最高id I'll call it last_id in the following.我将在下文中称其为last_id

Fetch the next ten rows with获取接下来的十行

SELECT jsonb_build_object(...) AS feature,
       id
FROM (SELECT line, line_type
      FROM table_a
      WHERE id > last_id
      ORDER BY id
      FETCH FIRST 10 ROWS ONLY) AS inputs;

This can be repeated to fetch more pages of 10 rows as needed.可以根据需要重复此操作以获取更多 10 行的页面。

The beauty of the method is that it uses the primary key index for high efficiency.该方法的美妙之处在于它使用主键索引以提高效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM