Home > Ask the Oracle Experts > Questions & Answers > Paging through SQL query results
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Paging through SQL query results

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 14 February 2005
What is the best way to do pagination on SQL query results? I used LIMIT M,N in MySQL. Is there something like that in SQL?

>
EXPERT RESPONSE

The best way is to query only those rows that you need, and send only those rows back from the database to the front end.

MySQL's LIMIT M,N actually assembles the entire result set (imagine if there are a million rows!), but sends only N rows back. This less than optimal.

When you ask "Is there something like that in SQL?" you are presumably asking about Microsoft SQL Server, and the answer is no, it's even messier in SQL Server. Your first attempt to reproduce the M,N functionality might involve nesting a derived table, something like this:

select top N 
       col1, col2, sortcol
  from (
       select top M+N
              col1, col2, sortcol
         from yourtable
       order by sortcol desc

       ) as derivedtable
order 
    by sortcol asc

However, as you near the end of the table, you can see that this is quite inefficient.

Another solution involves keeping track of the value of the sort column from the last time the query was run, so that you don't need a derived table:

select top N 
       col1, col2, sortcol
  from yourtable
 where sortcol > savedvalue       
order 
    by sortcol asc

Here, savedvalue is the value of the last sortcol from the previous execution. Of course, you need to modify the query on the first execution.

Database pagination is not a simple problem, and will have many types of solution. When evaluating strategies, remember that you want to:

  1. minimize query complexity and execution time

  2. minimize the number of rows transferred back to the front end

These objectives often conflict with each other.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts