Pagination query(SQL) in AS400/DB2

落爺英雄遲暮 提交于 2021-02-08 08:58:22

问题


I have been working on try to create php web-based paging for our tables which have over a million rows.

Based on what I have read, I have 3 options

  1. retrieve all rows in resultset - not possiblefor me coz of the size
  2. retrieve 1000 rows, store in temp table and create an iterator for it and page through it - too many queries - too many inserts!!
  3. run a query each time if someone opts page forward or backwards

Right now I am trying to get option 3 working. I have the first page showing up as "select * from accout order by acct fetch first 10 rows only" Page next "select * from account where acct>(last record) order by acct fetch first 10 only" page last record "select * from account where acct=(select max(acct) from account)"

The problem is showing the previous page and i really would appreciate help in this.


回答1:


SELECT *
  FROM (
    SELECT 
      *, 
      ROW_NUMBER() OVER (ORDER BY acct) AS RowNum
    FROM 
      account
  ) AS Data
WHERE 
  RowNum BETWEEN 100 AND 110;

First, you should get rid of the SELECT *. Select only the fields you need.

Place an index on acct, this will help the ROW_NUMBER() OVER (ORDER BY acct) construct.

Use a SELECT COUNT(*) FROM account to determine how many pages you will have.

Also read Fastest most/efficient way to do pagination with SQL searching DB2




回答2:


The LIMIT..OFFSET solution is supported in DB2 10+.. For older versions, you have to enable the MySQL compatibility with:

$ db2set DB2_COMPATIBILITY_VECTOR=MYS
$ db2stop
$ db2start

in db2cmd in order to use that syntax.




回答3:


SELECT * FROM foo LIMIT 10, 1;

try the limit and offset in mysql..

this mostly use in creating pagination



来源:https://stackoverflow.com/questions/14036838/pagination-querysql-in-as400-db2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!