相当于 DB2 的 LIMIT

发布于 2024-09-26 17:31:41 字数 296 浏览 5 评论 0原文

如何在 DB2 for iSeries 中执行LIMIT

我有一个包含超过 50,000 条记录的表,我想返回 0 到 10,000 条记录,以及 10,000 到 20,000 条记录。

我知道在 SQL 中,您在 0 到 10,000 的查询末尾编写 LIMIT 0,10000 ,在 10000 到 20,000 的查询末尾编写 LIMIT 10000,10000

所以,这在 DB2 中是如何完成的?代码和语法是什么? (赞赏完整的查询示例)

How do you do LIMIT in DB2 for iSeries?

I have a table with more than 50,000 records and I want to return records 0 to 10,000, and records 10,000 to 20,000.

I know in SQL you write LIMIT 0,10000 at the end of the query for 0 to 10,000 and LIMIT 10000,10000 at the end of the query for 10000 to 20,000

So, how is this done in DB2? Whats the code and syntax?
(full query example is appreciated)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

赠佳期 2024-10-03 17:31:41

仅使用FETCH FIRST [n] ROWS

http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.perf /db2z_fetchfirstnrows.htm

SELECT LASTNAME, FIRSTNAME, EMPNO, SALARY
  FROM EMP
  ORDER BY SALARY DESC
  FETCH FIRST 20 ROWS ONLY;

要获取范围,您必须使用 ROW_NUMBER() (自 v5r4 起)并在 WHERE 子句中使用它:(从这里: http://www.justskins.com/forums/db2 -选择-how-to-123209.html

SELECT code, name, address
FROM ( 
  SELECT row_number() OVER ( ORDER BY code ) AS rid, code, name, address
  FROM contacts
  WHERE name LIKE '%Bob%' 
  ) AS t
WHERE t.rid BETWEEN 20 AND 25;

Using FETCH FIRST [n] ROWS ONLY:

http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.perf/db2z_fetchfirstnrows.htm

SELECT LASTNAME, FIRSTNAME, EMPNO, SALARY
  FROM EMP
  ORDER BY SALARY DESC
  FETCH FIRST 20 ROWS ONLY;

To get ranges, you'd have to use ROW_NUMBER() (since v5r4) and use that within the WHERE clause: (stolen from here: http://www.justskins.com/forums/db2-select-how-to-123209.html)

SELECT code, name, address
FROM ( 
  SELECT row_number() OVER ( ORDER BY code ) AS rid, code, name, address
  FROM contacts
  WHERE name LIKE '%Bob%' 
  ) AS t
WHERE t.rid BETWEEN 20 AND 25;
婴鹅 2024-10-03 17:31:41

开发了这种方法:

您需要一个具有可以订购的唯一值的表。

如果您想要 10,000 到 25,000 行,并且您的表有 40,000 行,首先您需要获取起点和总行数:

int start = 40000 - 10000;

int Total = 25000 - 10000;

然后通过代码将这些传递给查询:

SELECT * FROM 
(SELECT * FROM schema.mytable 
ORDER BY userId DESC fetch first {start} rows only ) AS mini 
ORDER BY mini.userId ASC fetch first {total} rows only

Developed this method:

You NEED a table that has an unique value that can be ordered.

If you want rows 10,000 to 25,000 and your Table has 40,000 rows, first you need to get the starting point and total rows:

int start = 40000 - 10000;

int total = 25000 - 10000;

And then pass these by code to the query:

SELECT * FROM 
(SELECT * FROM schema.mytable 
ORDER BY userId DESC fetch first {start} rows only ) AS mini 
ORDER BY mini.userId ASC fetch first {total} rows only
街道布景 2024-10-03 17:31:41

DB2 for i 7.1 和 7.2 最近添加了对 OFFSET 和 LIMIT 的支持。您需要以下 DB PTF 组级别才能获得此支持:

  • SF99702 level 9 for IBM i 7.2
  • SF99701 level 38 for IBM i 7.1

请参阅此处以获取更多信息:OFFSET 和 LIMIT 文档,DB2 for i 增强 < a href="https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/DB2%20for%20i%20Functional%20Enhancements" rel="noreferrer">维基

Support for OFFSET and LIMIT was recently added to DB2 for i 7.1 and 7.2. You need the following DB PTF group levels to get this support:

  • SF99702 level 9 for IBM i 7.2
  • SF99701 level 38 for IBM i 7.1

See here for more information: OFFSET and LIMIT documentation, DB2 for i Enhancement Wiki

柒夜笙歌凉 2024-10-03 17:31:41

这是我想出的解决方案:

select FIELD from TABLE where FIELD > LASTVAL order by FIELD fetch first N rows only;

通过将 LASTVAL 初始化为 0(或文本字段的 ''),然后将其设置为最新记录集中的最后一个值,这将以 N 条记录的块的形式逐步遍历表。

Here's the solution I came up with:

select FIELD from TABLE where FIELD > LASTVAL order by FIELD fetch first N rows only;

By initializing LASTVAL to 0 (or '' for a text field), then setting it to the last value in the most recent set of records, this will step through the table in chunks of N records.

︶葆Ⅱㄣ 2024-10-03 17:31:41

@elcool 的解决方案是一个聪明的主意,但是您需要知道总行数(甚至可以在执行时发生变化)查询!)。所以我提出了一个修改版本,不幸的是它需要 3 个子查询而不是 2 个:

select * from (
    select * from (
        select * from MYLIB.MYTABLE
        order by MYID asc 
        fetch first {last} rows only 
        ) I 
    order by MYID desc
    fetch first {length} rows only
    ) II
order by MYID asc

其中 {last} 应该替换为我需要的最后一条记录的行号和 {length}应替换为我需要的行数,计算方式为最后一行 - 第一行 + 1

例如,如果我想要从 10 到 25 行(总共 16 行),则 {last} 将是 25,{length} 将是 25-10+1=16。

@elcool's solution is a smart idea, but you need to know total number of rows (which can even change while you are executing the query!). So I propose a modified version, which unfortunately needs 3 subqueries instead of 2:

select * from (
    select * from (
        select * from MYLIB.MYTABLE
        order by MYID asc 
        fetch first {last} rows only 
        ) I 
    order by MYID desc
    fetch first {length} rows only
    ) II
order by MYID asc

where {last} should be replaced with row number of the last record I need and {length} should be replaced with the number of rows I need, calculated as last row - first row + 1.

E.g. if I want rows from 10 to 25 (totally 16 rows), {last} will be 25 and {length} will be 25-10+1=16.

挽你眉间 2024-10-03 17:31:41

试试这个

SELECT * FROM
    (
        SELECT T.*, ROW_NUMBER() OVER() R FROM TABLE T
    )
    WHERE R BETWEEN 10000 AND 20000

Try this

SELECT * FROM
    (
        SELECT T.*, ROW_NUMBER() OVER() R FROM TABLE T
    )
    WHERE R BETWEEN 10000 AND 20000
油焖大侠 2024-10-03 17:31:41

LIMIT 子句允许您限制查询返回的行数。 LIMIT 子句是 SELECT 语句的扩展,具有以下语法:

SELECT select_list
FROM table_name
ORDER BY sort_expression
LIMIT n [OFFSET m];

在此语法中:

  • n 是要返回的行数。
  • m 是返回 n 行之前要跳过的行数。

LIMIT 子句的另一个较短版本如下:

LIMIT m, n;

此语法意味着跳过 m 行并从结果集中返回接下来的 n 行。

表可以按未指定的顺序存储行。如果您不将 ORDER BY 子句与 LIMIT 子句一起使用,则返回的行也是未指定的。因此,最好始终将 ORDER BY 子句与 LIMIT 子句一起使用。

有关更多详细信息,请参阅 Db2 LIMIT

The LIMIT clause allows you to limit the number of rows returned by the query. The LIMIT clause is an extension of the SELECT statement that has the following syntax:

SELECT select_list
FROM table_name
ORDER BY sort_expression
LIMIT n [OFFSET m];

In this syntax:

  • n is the number of rows to be returned.
  • m is the number of rows to skip before returning the n rows.

Another shorter version of LIMIT clause is as follows:

LIMIT m, n;

This syntax means skipping m rows and returning the next n rows from the result set.

A table may store rows in an unspecified order. If you don’t use the ORDER BY clause with the LIMIT clause, the returned rows are also unspecified. Therefore, it is a good practice to always use the ORDER BY clause with the LIMIT clause.

See Db2 LIMIT for more details.

久随 2024-10-03 17:31:41

您还应该考虑 OPTIMIZE FOR n ROWS 子句。有关所有这些的更多详细信息,请参阅 限制 SELECT 语句的准则 主题:

  • OPTIMIZE FOR 子句声明仅检索结果的子集或优先检索仅前几个结果的意图行。然后,优化器可以选择访问计划,最大限度地缩短检索前几行的响应时间。

You should also consider the OPTIMIZE FOR n ROWS clause. More details on all of this in the DB2 LUW documentation in the Guidelines for restricting SELECT statements topic:

  • The OPTIMIZE FOR clause declares the intent to retrieve only a subset of the result or to give priority to retrieving only the first few rows. The optimizer can then choose access plans that minimize the response time for retrieving the first few rows.
幸福丶如此 2024-10-03 17:31:41

在 DB2 表上有效分页有 2 种解决方案:

1 - 使用函数 row_number() 和子句 OVER 的技术,该技术已在另一篇文章中介绍过(“SELECT row_number() OVER ( ORDER BY ...)”)。在一些大桌子上,我注意到有时性能会下降。

2 - 使用可滚动光标的技术。实现取决于所使用的语言。这种技术在大桌子上似乎更稳健。

我在明年的研讨会上介绍了用 PHP 实现的两种技术。该幻灯片可通过以下链接获取:
http://gregphplab.com/serendipity/uploads/slides/DB2_PHP_Best_practices.pdf

抱歉,该文件只有法文版。

There are 2 solutions to paginate efficiently on a DB2 table :

1 - the technique using the function row_number() and the clause OVER which has been presented on another post ("SELECT row_number() OVER ( ORDER BY ... )"). On some big tables, I noticed sometimes a degradation of performances.

2 - the technique using a scrollable cursor. The implementation depends of the language used. That technique seems more robust on big tables.

I presented the 2 techniques implemented in PHP during a seminar next year. The slide is available on this link :
http://gregphplab.com/serendipity/uploads/slides/DB2_PHP_Best_practices.pdf

Sorry but this document is only in french.

话少心凉 2024-10-03 17:31:41

有这些可用的选项:-

DB2 has several strategies to cope with this problem.
You can use the "scrollable cursor" in feature.
In this case you can open a cursor and, instead of re-issuing a query you can FETCH forward and backward.
This works great if your application can hold state since it doesn't require DB2 to rerun the query every time.
You can use the ROW_NUMBER() OLAP function to number rows and then return the subset you want.
This is ANSI SQL 
You can use the ROWNUM pseudo columns which does the same as ROW_NUMBER() but is suitable if you have Oracle skills.
You can use LIMIT and OFFSET if you are more leaning to a mySQL or PostgreSQL dialect.  

Theres these available options:-

DB2 has several strategies to cope with this problem.
You can use the "scrollable cursor" in feature.
In this case you can open a cursor and, instead of re-issuing a query you can FETCH forward and backward.
This works great if your application can hold state since it doesn't require DB2 to rerun the query every time.
You can use the ROW_NUMBER() OLAP function to number rows and then return the subset you want.
This is ANSI SQL 
You can use the ROWNUM pseudo columns which does the same as ROW_NUMBER() but is suitable if you have Oracle skills.
You can use LIMIT and OFFSET if you are more leaning to a mySQL or PostgreSQL dialect.  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文