Oracle Database / Intermediate: application SQL and evidence / Lesson 14
Pagination, Ordering, and Consistency
Compare offset and continuation-key pagination with a deterministic order and an explicit consistency contract.
On this page
Lesson goals
- Build a total order for paginated rows.
- Explain offset and continuation-key approaches.
- Recognize changes between separate page queries.
Define a stable order
A page is meaningful only relative to a defined ordering and population. If several orders share the same creation time, order by a unique non-null key as a tie-breaker. For this Oracle Database 19c lab, assume synthetic LAB_ORDERS rows with non-null CREATED_AT and unique ORDER_ID, and an ascending order on both columns.
The examples require an Oracle session, the synthetic table, and typed bind values supplied by the client. They are illustrative and runnable:false; no Oracle execution or performance result is claimed. Define timezone and null rules separately if the real schema differs from the lab.
Request a page by offset
The first query skips a supplied number of rows and fetches the requested page size after applying the explicit order. It is convenient for arbitrary page numbers, but changes before the offset between requests can shift later results. Evaluate performance with the actual indexes, data, and execution plan rather than assuming a universal cost.
SELECT order_id, created_at, amount
FROM lab_orders
ORDER BY created_at, order_id
OFFSET :skip_rows ROWS FETCH NEXT :page_size ROWS ONLY;
The bound offset and page size control the slice of rows ordered by creation time and unique order identifier.
Continue after the last key
The second query requests rows strictly after the last creation time and identifier returned by the prior page. The pair handles tied creation times under the stated non-null ascending contract. The first page omits this continuation predicate. Bind the saved values with matching data types and validate the page-size limit in the application.
SELECT order_id, created_at, amount
FROM lab_orders
WHERE created_at > :last_created
OR (created_at = :last_created AND order_id > :last_id)
ORDER BY created_at, order_id
FETCH NEXT :page_size ROWS ONLY;
The predicate advances lexicographically beyond the last pair while the ORDER BY uses the same key order.
Account for concurrent changes
Neither pagination method alone freezes a multi-request dataset. Under ordinary statement-level read consistency, separate statements may observe different committed states. Inserts, deletes, or changes to ordering keys can affect later pages. If a consistent historical export is required, design and test an appropriate snapshot or transaction strategy with database owners and operational limits.
Test pages and boundaries
Use synthetic rows with tied timestamps, a final partial page, and an inserted or updated row between requests. Check for duplicate and missing identifiers relative to the stated consistency contract, not an assumed frozen snapshot. Review the query plan and indexes in a representative environment and protect continuation tokens from misuse.
Common errors and fixes
Rows with the same timestamp appear in an unpredictable order.
- Why it happens
- The order did not include a unique tie-breaker.
- How to fix it
- Add the unique ORDER_ID to ordering and continuation keys.
- Prevent it next time
- Define a total order before pagination.
A multi-page export changes when records are inserted.
- Why it happens
- Separate statements observed different committed states.
- How to fix it
- Use an appropriate tested consistency strategy for the export.
- Prevent it next time
- Document snapshot requirements and concurrent-change tests.
The next keyset page repeats a boundary row.
- Why it happens
- The predicate used an inclusive comparison or mismatched key order.
- How to fix it
- Use a strict continuation predicate consistent with ORDER BY.
- Prevent it next time
- Test ties and page boundaries with known identifiers.
2 practice exercises
1. Two rows have the same CREATED_AT and ORDER_ID values 10 and 11. What key follows the first row?
Show worked solution
The continuation pair uses that timestamp and ID 10. The strict predicate permits ID 11 at the same timestamp, then later timestamps, under the stated ascending order.
2. Does keyset pagination alone guarantee a frozen report across requests?
Show worked solution
No. It defines where to continue in the order, but separate statements may observe changes. Use and test a suitable snapshot or transaction strategy when a consistent export is required.
Key takeaways
- Use a deterministic total order with a unique tie-breaker.
- Match continuation predicates to the ordering.
- Specify consistency separately from page navigation.