Skip to content

perf(grid): page table tabs by seeking on the key instead of offset #4526

Done

Description

@datlechin

Problem

Every page of a table tab after the first skips rows with OFFSET. The server produces and discards every skipped row, so page N costs about N times page 1. Last Page on a 10M-row table walks about 10M rows. The first page, which the 228-275 ms table-open measurement covers, is not affected.

Cause

  • buildPaginationClause always emits LIMIT n OFFSET k, or OFFSET k ROWS FETCH NEXT n ROWS ONLY (apple/TablePro/Core/Services/Query/TableQueryBuilder.swift:306-314).
  • Next, previous, last page and go-to-page only change currentOffset (apple/TablePro/Core/Coordinators/PaginationCoordinator.swift:25-46; apple/TablePro/Models/Query/QueryTabState.swift:381-411).
  • A table tab already orders by its primary key by default. defaultSortBehavior is .primaryKey, ascending (apple/TablePro/Models/Settings/AppSettings.swift:131-132), and DefaultSortResolver turns it into a sort on the key columns (apple/TablePro/Core/Services/Query/DefaultSortResolver.swift:19-35). So the page query carries ORDER BY <pk>, which is all a seek needs. It is never used to seek.
  • With the setting at None, or a plugin hint of suppress, a LIMIT dialect gets no ORDER BY at all (TableQueryBuilder.swift:287-322). The PostgreSQL plugin turns off synchronize_seqscans to keep unordered pages stable (apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift:335-347).
  • The core already pages this way for Windows and Linux. paging_mode picks Keyset for a table with a unique key and no user sort, CtidWindow for a PostgreSQL table with no key, and Offset otherwise (core/crates/core/src/browse/paging_mode.rs:31-45). keyset_walk seeks past the edge row's key and reverses the order for the last page (core/crates/core/src/browse/build_page.rs:122-170). The Mac's Swift builder does not follow that rule.

Change

  • The same rule on both sides. Follow the core's paging rule in the Mac's TableQueryBuilder, until each engine's page read moves to the core and the Swift builder goes.
  • Next and previous. With an order that ends in a unique key, seek: WHERE (k1, k2) > (:last1, :last2) ORDER BY k1, k2 LIMIT n for the next page, and the mirrored comparison in reverse order for the previous page.
    • The page keeps the key of its first and last rows.
    • The app's default primary-key sort counts as the key order, not as a user sort.
  • Last page. Read it in reverse key order with LIMIT, then flip it.
  • A user's own sort. Seek on the tuple (sort columns..., key), with the key as the tiebreaker, using row-value comparison where the engine has it (PostgreSQL, MySQL 8, SQLite 3.15 and later). Nullable sort columns need an explicit NULLS rule, so the seek and the ORDER BY agree.
    • The core's paging_mode sends every user sort to Offset today. Change it the same way, so the rule stays one rule.
  • Where OFFSET stays. A direct jump to page N in the middle, a table without a unique key, and views keep OFFSET. PostgreSQL tables without a key can use the core's ctid window.

Measure

  • A 10M-row table on PostgreSQL, MySQL and SQLite: time page 1, page 5,000 and the last page, before and after, with EXPLAIN (ANALYZE, BUFFERS) on PostgreSQL and rows examined in the MySQL slow log. Expected: every keyed page costs about what page 1 costs.
  • The same with a user sort on a non-unique column: the seek returns the same rows, in the same order, as the offset query it replaces (a test per dialect, with NULLs in the sort column and ties across a page edge).

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    rust
    on Nov 25, 2026
  2. datlechin commented on Nov 26, 2026

    @datlechin
    MemberAuthor

    The first part of this ships in #4604: Next, Previous and Last page seek on the primary key when the page order is that key, on MySQL, MariaDB, PostgreSQL and SQLite. What is left:

    • A user's own sort. Seek on (sort columns..., key) with the key as the tiebreaker, using row-value comparison, and an explicit NULLS rule on nullable sort columns so the seek and the ORDER BY agree. Until then a sort on any column other than the key pages by OFFSET.
    • The core. paging_mode in core/crates/core/src/browse/paging_mode.rs still sends every user sort to Offset. Change it with the item above, so the Mac and the core keep one rule.
    • PostgreSQL tables without a key. The core's ctid window is not on the Mac yet; those tables page by OFFSET.
    • Edge cases the seek does not cover yet. A PostgreSQL parent whose inheritance children repeat its keys, a SQLite INTEGER PRIMARY KEY DESC column holding NULL, a Last page read with an exact count gone stale, and Previous after another session deleted the rows before the page, which can give a short page and then an empty one.
    • The measurement. The 10M-row timings on PostgreSQL, MySQL and SQLite for page 1, page 5,000 and the last page, before and after, are not taken yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macosrust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions