Raw capture â raw/fetch-next-page.md
url: https://use-the-index-luke.com/sql/partial-results/fetch-next-page date_fetched: 2026-08-08
After implementing a pipelined top-N query to retrieve the first page efficiently, you will often also need another query to fetch the next pages. The resulting challenge is that it has to skip the rows from the previous pages. There are two different methods to meet this challenge: firstly the offset method, which numbers the rows from the beginning and uses a filter on this row number to discard the rows before the requested page. The second method, which I call the seek method, searches the last entry of the previous page and fetches only the following rows.
The following examples show the more widely used offset method. Its main advantage is that it is very easy to handleâespecially with databases that have a dedicated keyword for it (offset). This keyword was even taken into the SQL standard as part of the fetch first extension.
offsetsince release 11.1. The standard conforming alternative using- ROW_NUMBER()window function (see next section) works in earlier releases. There are two other ways to get offset functionality, none of them recommendable: (1) using- db2set DB2_COMPATIBILITY_VECTOR=MYSto enable- limitand- offsetlike MySQL supports it. This does, however, not allow to combine- fetch firstwith- offset; (2) using- db2set DB2_COMPATIBILITY_VECTOR=ORAto get Oracleâs- ROWNUMpseudo column (see Oracle example).offsetclause for discarding the specified number of rows from the beginning of a top-N query. The- limitclause is applied afterwards.- SELECT * FROM sales ORDER BY sale_date DESC LIMIT 10- OFFSET 10offsetsince release 12c. Earlier releases provide the pseudo column- ROWNUMthat numbers the rows in the result set automatically. It is, however, not possible to apply a greater than or equal to (- >=- SELECT * FROM ( SELECT tmp.*,- rownum rnFROM ( SELECT * FROM sales ORDER BY sale_date DESC ) tmp WHERE- rownum <= 20) WHERE- rn > 10- Note the use of the alias - RNfor the lower bound and the- ROWNUMpseudo column itself for the upper bound (thanks to Tom Kyte).fetch firstextension defines an- offset ... rowsclause as well. PostgreSQL, however, only accepts- offsetwithout the- rowskeyword. The previously used- limit/offsetsyntax still works as shown in the MySQL example.- SELECT * FROM sales ORDER BY sale_date DESC- OFFSET 10 FETCH NEXT 10 ROWS ONLYtopclause but introduced the- fetch firstextension with SQL Server 2012. The- offsetclause is mandatory although the standard defines it as an optional addendum.- SELECT * FROM sales ORDER BY sale_date DESC- OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLYBesides the simplicity, another advantage of this method is that you just need the row offset to fetch an arbitrary page. Nevertheless, the database must count all rows from the beginning until it reaches the requested page. Figure 7.2 shows that the scanned index range becomes greater when fetching more pages.
This has two disadvantages: (1) the pages drift when inserting new sales because the numbering is always done from scratch; (2) the response time increases when browsing further back.
⌠to subscribe my mailing lists, get free stickers, buy my book or join a training.
The seek method avoids both problems because it uses the values of the previous page as a delimiter. That means it searches for the values that must come behind the last entry from the previous page. This can be expressed with a simple where clause. To put it the other way around: the seek method simply doesnât select already shown values.
The next example shows the seek method. For the sake of demonstration, we will start with the assumption that there is only one sale per day. This makes the SALE_DATE a unique key. To select the sales that must come behind a particular date you must use a less than condition (<) because of the descending sort order. For an ascending order, you would have to use a greater than (>) condition. The fetch first clause is just used to limit the result to ten rows.
SELECT *
FROM sales
WHERE sale_date < ?
ORDER BY sale_date DESC
FETCH FIRST 10 ROWS ONLYInstead of a row number, you use the last value of the previous page to specify the lower bound. This has a huge benefit in terms of performance because the database can use the SALE_DATE < ? condition for index access. That means that the database can truly skip the rows from the previous pages. On top of that, you will also get stable results if new rows are inserted.
Nevertheless, this method does not work if there is more than one sale per dayâas shown in Figure 7.2âbecause using the last date from the first page (âyesterdayâ) skips all results from yesterdayânot just the ones already shown on the first page. The problem is that the order by clause does not establish a deterministic row sequence. That is, however, prerequisite to using a simple range condition for the page breaks.
Without a deterministic order by clause, the database by definition does not deliver a deterministic row sequence. The only reason you usually get a consistent row sequence is that the database usually executes the query in the same way. Nevertheless, the database could in fact shuffle the rows having the same SALE_DATE and still fulfill the order by clause. In recent releases it might indeed happen that you get the result in a different order every time you run the query, not because the database shuffles the result intentionally but because the database might utilize parallel query execution. That means that the same execution plan can result in a different row sequence because the executing threads finish in a non-deterministic order.
Paging requires a deterministic sort order.
Even if the functional specifications only require sorting âby date, latest firstâ, we as the developers must make sure the order by clause yields a deterministic row sequence. For this purpose, we might need to extend the order by clause with arbitrary columns just to make sure we get a deterministic row sequence. If the index that is used for the pipelined order by has additional columns, it is a good start to add them to the order by clause so we can continue using this index for the pipelined order by. If this still does not yield a deterministic sort order, just add any unique column(s) and extend the index accordingly.
In the following example, we extend the order by clause and the index with the primary key SALE_ID to get a deterministic row sequence. Furthermore, we must apply the âcomes afterâ logic to both columns together to get the desired result:
ATE INDEX sl_dtid ON sales (sale_date, sale_id)
SELECT *
FROM sales
** WHERE (sale_date, sale_id) < (?, ?)**
ORDER BY sale_date DESC, **sale_id DESC**
FETCH FIRST 10 ROWS ONLYThe `where` clause uses the little-known ârow valuesâ syntax (see the box entitled â*SQL Row Values*â). It combines multiple values into a logical unit that is applicable to the regular comparison operators. As with scalar values, the less-than condition corresponds to âcomes afterâ when sorting in descending order. That means the query considers only the sales that come after the given `SALE_DATE`, `SALE_ID` pair.
Even though the row values syntax is part of the SQL standard, only a few databases support it. SQL Server 2017 does not support row values at all. The Oracle database supports row values in principle, but cannot apply range operators on them (ORA-01796). MySQL evaluates row value expressions correctly but cannot use them as access predicate during an index access. Db2 (only LUW, since 10.1) and PostgreSQL (since 8.4), however, have a proper support of row value predicates *and* uses them to access the index if there is a corresponding index available.
Nevertheless it is possible to use an approximated variant of the seek method with databases that do not properly support the row valuesâeven though the approximation is not as elegant and efficient as row values in PostgreSQL. For this approximation, we must use âregularâ comparisons to express the required logic as shown in this Oracle example:
SELECT * FROM ( SELECT * FROM sales WHERE
**sale_date <= ?
AND NOT (sale_date = ? AND sale_id >= ?)**
ORDER BY sale_date DESC, sale_id DESC
)
WHERE rownum <= 10The `where` clause consists of two parts. The first part considers the `SALE_DATE` only and uses a less than or equal to (`<=`) conditionâit selects more rows as needed. This part of the `where` clause is simple enough so that all databases can use it to access the index. The second part of the `where` clause removes the excess rows that were already shown on the previous page. The box entitled â*Indexing Equivalent Logic*â explains why the `where` clause is expressed this way.
The execution plan shows that the database uses the first part of the `where` clause as access predicate.
| 0 | SELECT STATEMENT | | 10 | 4 | |1 | COUNT STOPKEY | | | | | 2 | VIEW | | 10 | 4 | | 3 | TABLE ACCESS BY INDEX ROWID | SALES | 50218 | 4 | |4 | INDEX RANGE SCAN DESCENDING| SL_DTIT | 2 | 3 |
1 - filter(ROWNUM<=10)
``
**4 - access(**"SALE_DATE"<=:SALE_DATE)
**filter**("SALE_DATE"<>:SALE_DATE
OR "SALE_ID"<TO_NUMBER(:SALE_ID))The access predicates onSALE_DATEenables the database to skip over the days that were fully shown on previous pages. The second part of thewhere` clause is a filter predicate only. That means that the database inspects a few entries from the previous page again, but drops them immediately. Figure 7.3 shows the respective access path.
Figure 7.4 compares the performance characteristics of the offset and the seek methods. The accuracy of measurement is insufficient to see the difference on the left hand side of the chart, however the difference is clearly visible from about page 20 onwards.
where clause very carefullyâyou also cannot fetch arbitrary pages. Moreover you need to reverse all comparison and sort operations to change the browsing direction. Precisely these two functionsâskipping pages and browsing backwardsâare not needed when using an infinite scrolling mechanism for the user interface.