One person opens the first page of a conversation. Someone else wants to get to much older messages. Both receive 50 records. For the reader, the page is the same size. For the database, finding the second one can take much more work.
In Investigation 026, we tested a hypothesis. Deeper into an ordered history, skipping an increasing number of rows with OFFSET costs more. Restarting from the last key seen can keep query time more stable when a suitable index is available.
A bookmark in the index instead of a count
An index gives a database an ordered way to locate records. With OFFSET, the query asks to skip a number of rows before returning the page of 50. With a cursor, it gives the database the last key it saw and asks for the rows after it. In our trial, the index on account and identifier provided the starting position.
We verified that the two queries returned the same identifiers and contents at every position. Cursor pagination is suitable for going forwards or backwards through a consistently ordered history. Jumping straight to an arbitrary page number is a different operation and can require another structure or a point of reference.
Four positions and thirty repetitions
We created one million synthetic records belonging to one account, with 128 bytes of content per row. The queries ran against a warmed SQLite database in memory, with no concurrent writes. We measured four positions, with 30 repetitions for each strategy at each position. We alternated the order of the two queries to reduce the effect of always running one first.
In the initial trial with SQLite 3.46.1, position 900,000 gave medians of 43.5652 ms with OFFSET and 0.0394 ms with a cursor. In the reproduction with SQLite 3.53.1, the medians were 27.2169 ms and 0.0260 ms. The rerun preserved both equality of results and the growth pattern. Absolute times differed between the two environments.
| Position in the history | OFFSET / indexed cursor |
|---|---|
| 0 | 0.0158 ms / 0.0159 ms |
| 10,000 | 0.2818 ms / 0.0160 ms |
| 100,000 | 2.7858 ms / 0.0168 ms |
| 900,000 | 27.2169 ms / 0.0260 ms |
A history changes while we read it
In a real conversation, new messages can arrive while someone is changing pages. The order needs a key that breaks ties, such as a date paired with an identifier. A cursor carries a position in that order. Consistency between pages also depends on what people should see when records are inserted, changed or deleted. Reading a stable snapshot is one choice. Updating the list as messages arrive is another.
Memory, disk, distribution across accounts, concurrent writes and cache state can all change the result. This trial measures SQLite queries in one process without a network. A million rows in a test describes the size of that database. The capacity of an app with real users needs its own test.
Speed gained by doing less work
On the first page, the two strategies were close. Further into the history, the difference grew. Looking up a position and counting rows to skip require different amounts of work in this design.
When a person reads a history on a constrained device, reducing the work required by each page is one way to keep responses stable as the data grow. The right algorithm can be an improvement that lasts longer than a processor upgrade. It still calls for measurement under the intended conditions of use.
Sources and references
- SQLite · Row Values and scrolling windows of results
- SQLite · Query Planning, indexing and sorting
- SQLite · Transaction isolation and visibility
- ScopeLink Lab · Python code for the SQLite trial
- ScopeLink Lab · Reproduction data for Investigation 026
Sources reviewed on .


