• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to "page" results from a database?

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Suppose I need to query a database and the result contains 100K of records. Can someone please show me a code example of how to "page" the result for, say, 1K of records at a time?
 
John Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you are using a relational database and the data is ordered then you can do the paging in the SQL statement, on many DBs anyway.
Something like:



The fetch statement controls the number of rows returned. Set lastKey to the key of the last record in the previous page.
There's probably a better way to do it, but that should work.

John
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Interesting solution, the only problem is that they key is not something numeric that I can run on, it's random numbers.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So? What does that have to do with anything? The whole concept of paging ties in with the order with which rows are to be returned. You can specify whatever sorting you like with an ORDER BY clause.

There's a JSP FAQ entry on this subject, by the way.
 
Diego Pedrosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think you can solve this issue with the 'LIMIT' SQL keyword.
Read this: Paging in SQL
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Diego,

Nice one! I'm going to give it a go!
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roy Cohen:


Interesting solution, the only problem is that they key is not something numeric that I can run on, it's random numbers.


sounds like a good reason to have an autoincrement field in the table. Key it, and you are ready to go.

If you don't index the column, this approach can get really slow.
 
Jan Cumps
Bartender
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the link to this nice JavaRanch Howto from the JSP FAQ:
PaginationOrPaging : How do I limit the number of rows displayed in the browser?
Regards, Jan
[ November 13, 2007: Message edited by: Jan Cumps ]
 
John Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm probably missing something here, but I can't see a solution that will work with un-ordered data.
If the data is not ordered, then can you guarantee that rows will be returned in the same relative position over multiple queries?
Or is the data ordered but not keyed?

John
[ November 13, 2007: Message edited by: John Simpson ]
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Simpson:
I'm probably missing something here, but I can't see a solution that will work with un-ordered data.
If the data is not ordered, then can you guarantee that rows will be returned in the same relative position over multiple queries?
Or is the data ordered but not keyed?

John

[ November 13, 2007: Message edited by: John Simpson ]


you include an order by clause in your query.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic