Question

Paging very large result sets.

Asked by: randyd

Hey experts!

I need to page extremely large result sets through to a java client.  if my select statement returns say millions of rows, how can i get teh first set back to the gui without blowing up the client with out of memory errors...

TOAD seems to do this by pagin the results on the server - maybe using snapshots?  can i access the snapshot directly (in 9.2) or is there some other magic i should explore?

thanks for pointers.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-04-26 at 11:39:59ID21403219
Tags

paging

Topic

Oracle Database

Participating Experts
5
Points
500
Comments
11

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Pagination
    Hi, I would like to get some alogrithm from you guys about pagination. Right now, I have a hugh data set in the database (About 8000 rows), and it takes ages to load to the front end as a HTML table, and I am using servlet to work in between. So I would be very appreciate i...
  2. Pagination query with MSSQL Server
    Hii, I am using the following for doing the pagination in Oracle. SELECT * FROM (SELECT tt.*, ROWNUM ROWNO FROM ( SELECT blah...blah.. FROM table WHERE some=some ) tt WHERE ROWNUM <= 20) WHERE ROWNO >0 How can i emulate it for MS SQL Server? Any ideas pleas...
  3. TOAD implementation
    Hey experts! I have a general question - IF your Oracle (9.2 or 10g) database has say, a terabyte of data in a single table, and you say select * from that table; TOAD somehow manages to return the first rows of the query, and let you page through the rest... If you drag t...
  4. Pagination problem
    I have a pagination issue due to a very large database. The following code has been working fine for me up until now; function pageturn($sqlcall,$pagelimit) { global $query, $num_rows, $prev_page, $next_page, $num_pages, $page; if (!$_GET[page]) { $_GET[page] = 1; }...
  5. pagination
    looking for a ajax based pagination plugin in jquery or javascript

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: jrb1Posted on 2005-04-26 at 12:14:52ID: 13869871

Normally what you do (and what TOAD is doing) is open a cursor and then fetch a certain number of rows--and display them.  TOAD keeps the CURSOR open, and if you page through the results, it fetches more rows and adds them to the display.  Depending on your program, you may be able to do that as well.

 

by: earthman2Posted on 2005-04-26 at 15:30:54ID: 13871386

By default Oracle JDBC has a FetchSize of TEN rows.  That should not break your java program.

void setFetchSize(int rows) throws SQLException is available in all Statement, PreparedStatement, CallableStatement, and ResultSet

 

by: randydPosted on 2005-04-26 at 19:17:58ID: 13872627

The Ten at a time is not a problem... i just cant expect the client to have enough ram to hold the entire set at once.  but i still want to be able to scroll around the result set in a list for instance...

there is a cursor that lets you move to PREVIOUS as well as NEXT values.  this is what i need to do i think.  I know this is available in OCI calls and I believe it is available in JDBC2.0 ...   to make my GUI work (nicely) i think i need to know the number of records the cursor contains... but i dont think i will until i page through to the end...  

any comments on an approach here?

 

by: schwertnerPosted on 2005-04-27 at 00:00:53ID: 13873624

This seems to be a design error.
"if my select statement returns say millions of rows".
This is bad design. By no means your select statement should return
"if my select statement returns say millions of rows".

This is not possible for frontend application - the only reader of these rows
is a human being. And no numan being can observe, read and accept milions of rows.

Also the load on the server, application server and net channel is to high.

So speak with the designer and project lead and sort out why you have
to select milions of rows.

 

by: grim_toasterPosted on 2005-04-27 at 01:28:09ID: 13873955

You will get the out of memory errors if you attempt to cache all of the results!  You will at some level even if you use scrollable cursors with Oracle.  See http://download-west.oracle.com/docs/cd/B10501_01/java.920/a96654/resltset.htm In particular read the section "Because the underlying server does not support scrollable cursors, Oracle JDBC must implement scrollability in a separate layer."

As to the solution, I would recommend looking into something like Hibernate that has quite a good ability at handling paging using a technique similar to below.

Anyway, to actually implement paging efficiently in Oracle, it would be preferable to re-call the query each new page the client views (as schwertner pointed out users rarely ever go past the first page if there are too many results, but sometimes they do).  So, in relation to that, here's how to wrap you query using pagination, but there is one slight problem, you cannot guarantee that no-one will change the underlying data between calls and possibly miss entries with each call.

Required parameters: page & no_per_page, obviously should have some constraints set to ensure page >= 1 and no_per_page >= 1.

SELECT t2.object_name
FROM   (
        SELECT t1.object_name,
               rownum AS row_num
        FROM   (
                -- This is where your actual query will go...
                SELECT object_name
                FROM   user_objects
                ORDER by object_name
               ) t1
        WHERE  rownum <= (:page * :no_per_page)
       ) t2
WHERE  row_num > ((:page - 1) * :no_per_page);

 

by: plamen73Posted on 2005-04-27 at 02:04:15ID: 13874115

Just to add to grim_toaster answer and your assumption:
....to make my GUI work (nicely) i think i need to know the number of records the cursor contains... but i dont think i will until i page through to the end...  

yes, it is absolutely sure that you can not retrieve the number of all records, without actually read them, which compromises the paging.
So, you have two approaches:
1. Either forget about total number of records (pages respectively) and provide "Previous" and "Next" buttons only, or
2. Get the total number of pages and make it very slowly, especially the first time, when you have to execute
select count(1) from  ... at least

I prefer approach 1, but very often some Product Managers promises the 2nd to the customers.

something about:

>>>but there is one slight problem, you cannot guarantee that no-one will change the underlying data between calls and possibly miss entries with each call.
<<<
Actually the "paging" query avoids this problem.
Imagine you are at the 2nd page. Somebody inserts a record which gois in the 1st page. when you click previous, you will start counting from the 1st record and will get exactly the first no_per_page records, including the new one....and so on.

 

by: grim_toasterPosted on 2005-04-27 at 02:38:05ID: 13874254

Two points to that...

--> Get the total number of pages and make it very slowly, especially the first time...
Not necessarily, you can do as Google and many other paging operations do, and only see if there are at least X number of pages left, for example:

SELECT CEIL(count(*) / :no_per_page) AS remaining_pages, count(*) FROM (
SELECT rownum AS row_num
FROM   all_objects
WHERE  rownum <= (:page * :no_per_page) + (:no_per_page * :max_no_pages)
) WHERE row_num > (:page * :no_per_page)

Seeing as you will only be able to show the user that there are X number of available pages, it makes sense to only see if there are those X number of pages left.  And then you can stretch the available pages with each different page viewed.


-->Actually the "paging" query avoids this problem.
I was thinking more of if you were viewing page 2, someone deleted an entry on page 1, you then go to page 3 and you will not see what would have otherwise been the first entry on page 3.  To be honest, no-one would really notice, but it's just one of the things that needs to be considered.

 

by: randydPosted on 2005-04-27 at 06:55:55ID: 13876085

hey thanks guys for the lively discussion :)

schwertner - i realize that it is not practical to return mega-result sets - not usable - not digestable etc...  but unfortunately, I, like TOAD and other apps, am not able (or willing) to restrict the SQL that the user may attempt to execute on the system.  I must beleive that they will say select * from somewhere that has too many rows - whether people do this becuase they are lazy and dont want to think about restricting with a where clause - or dont know the DESC command so they want to see columns, or they simply dont know that there will be a million rows in that pile..  not sure.  but in the end it doesn't amtter - they will issue that query and i need to handle it gracefully... (i don't call that a design error)

grim_toaster - exactly! the out of memory is a killer - and super-tricky (impossible?) to predict it is about to happen - so can't be avoided - and as an added benefit, app behavior stinks exceptions aren't thrown well etc.  

plamen73 - i thought about the select count(*) from () method - if that runs in a background thread while the first rows are being returned then the GUI might be ok...

I was thinking that read-consistent view was important - i might have to give that up in favor of making this thing work...  you might guess - but my other wrinkle is i do not know the SQL statements ahead of time and i don't want to get into parsing random SQL and inserting the paging stuff in the middle...

thanks so far... still noodling..

 

by: grim_toasterPosted on 2005-04-27 at 07:41:22ID: 13876613

As a question for you, how is the SQL provided to you?  

If you are actually passed a select statement to execute, where are you executing it?  In a stored procedure via dbms_sql of nds or on the java side via a prepared statement?  Although I must admit I don't like the sounds of it, so many security holes etc.  But if it is a requirement...

Anyway, if I can attempt to persuade you to use hibernate (which has paging support already provided) to send your custom SQL to would be the simplest way, or if you can download the code and look at the Oracle9Dialect class to give you some ideas as to how to do this.

But in a nutshell, the only way to do what you are after (from what I understand) is to wrap whatever query they provide with the pagination code, thankfully it can fully wrap the SQL passed (see above example) and just ignore the extra row_num column that would be provided.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...