• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to "page" results from a database?

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Diego,

Nice one! I'm going to give it a go!
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic