问题
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
- retrieve all rows in resultset - not possiblefor me coz of the size
- retrieve 1000 rows, store in temp table and create an iterator for it and page through it - too many queries - too many inserts!!
- 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