MySQL Keyset Pagination
Use keyset pagination when the caller can carry the last seen ordering values.
How it works
Offset pagination can skip many earlier rows and shift when records change. Keyset pagination resumes after the last complete sort key instead. The example’s first page ends at cost 2.50 and ID one; the next page compares both cost and ID. A unique tie-breaker is essential when several items share a cost. This is a query-level continuation rule, not a guarantee of a frozen multi-request snapshot.
Run in a disposable MySQL lab
Use Oracle MySQL 8.4 with InnoDB, not Bhylu’s SQLite playground. The examples were reviewed on MySQL 8.4.8. XAMPP may provide MariaDB, which has different feature and collation behaviour. Check your actual server before running the script. These lessons do not require a change to Bhylu’s PHP, Apache or normal database service.
- On a separate lab instance, create the empty bhylu_mysql_lab schema. Stop if it already exists; do not overwrite an existing database.
- Select that schema and import the complete repair-parts seed once. It sets the documented session mode, utf8mb4 collation, UTC and English date-name locale.
- Run this lesson in a fresh copy of the seed, not after another lesson’s writes. Use the command-line client or a compatible editor. DELIMITER lines are client instructions for stored-program examples.
Download this complete lesson script. Its setup is the linked seed; review the dataset before changing it. Administrative and destructive examples are restricted to the specifically named disposable objects. Every script repeats the session settings because each new connection has its own session. No MySQL browser runner is currently available.
Example
SET NAMES utf8mb4 COLLATE utf8mb4_0900_ai_ci;
SET SESSION sql_mode = 'STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
SET SESSION time_zone = '+00:00';
SET SESSION lc_time_names = 'en_US';
SELECT id,name,unit_cost FROM parts
WHERE unit_cost > 2.50 OR (unit_cost = 2.50 AND id > 1)
ORDER BY unit_cost,id
LIMIT 2;
Expected result
4 | Wood knob | 3.75 2 | Brass hinge | 6
The continuation returns the next two items in the same complete order. The client must preserve the relevant filter and ordering contract with the cursor. Concurrent updates to a sort key can still move records, so decide whether the product accepts a live list or needs a stronger snapshot/export workflow. An opaque cursor may protect implementation details but still needs validation.
Use the official MySQL 8.4 reference for statement contracts and version details.