Query Builder Pagination Offset Limit Behavior Across Database Engines
0 reputation · 16 Mar 2022, 04:20 UTC
0 reputation · 16 Mar 2022, 04:20 UTC
CodeIgniter's Query Builder generates standard LIMIT n OFFSET m clauses through the limit() and offset() methods. The documentation does not specify a maximum OFFSET value or how different database engines handle large offsets, leaving cross-database behavior ambiguous.
Determine whether the framework should define a recommended offset ceiling, expose a cursor-based pagination helper, or document engine-specific limits so developers can make informed architectural decisions without relying on trial-and-error benchmarking.
WHERE id > ? on a unique indexed column.OFFSET ceiling per supported RDBMS, or is that too environment-specific to standardize?WHERE-based solutions?34775 reputation · 16 Mar 2022, 12:53 UTC
CodeIgniter’s limit() and offset() simply emit the database’s native LIMIT / OFFSET syntax. The framework does not impose a universal offset ceiling, nor does it ship a built‑in cursor‑based helper. In practice, deep offsets become expensive on MySQL, PostgreSQL, and SQL Server, and the cost depends on the table’s size, indexes, and the query’s ordering. The safest approach is to avoid large offsets for hot tables and to use keyset (seek) pagination when you need to page past the first few hundred rows.
OFFSET 5000 on a small indexed table but may still need to read the entire table for the same offset on a wide, unfiltered table.Bitmap Heap Scan for moderate offsets but switches to a full scan once the offset exceeds the number of rows that fit in the buffer pool.OFFSET‑FETCH always walks the result set, so a high offset forces a full sort and scan regardless of indexes.id).SELECT * FROM users
WHERE id > :last_seen_id
ORDER BY id ASC
LIMIT :page_size;last_seen_id from the last row of the current page and pass it to the next request.where() + order_by() + limit() chain.limit(100)->offset(5000) query and run EXPLAIN (or EXPLAIN ANALYZE in PostgreSQL) to see the number of rows examined versus returned.ORDER BY column and compare the logical reads and elapsed time.If your application frequently pages past the first few hundred rows on a large, frequently updated table, please share the table’s primary key type and any filtering conditions. That information helps decide whether a simple keyset approach will work or whether a more complex cursor strategy is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.