Conversation
…ing its sort queryL0Paginated filters sessions with (session_key = ? OR session_id = ?), which cannot drive ordered access on its own. The planner was picking (user_id, agent_id, session_id) as an equality scan and then rebuilding a TEMP B-TREE for every ORDER BY timestamp DESC LIMIT/OFFSET — so each page and each count got slower as history grew. Add idx_l0_user_agent_ts(user_id, agent_id, timestamp DESC): equality prefix + timestamp order lets the planner walk the index in ORDER BY order and stop at LIMIT+OFFSET. Measured on a 3.2k-row standalone DB: paginated fetch + count 21.5ms → 5.9ms; deep page (offset=3000) 40.6ms → 5.5ms; index build 23ms (IF NOT EXISTS, idempotent at init). Also add l0-paginated.test.ts pinning the query semantics that must not change with the index: session OR filtering (both key and id paths), new→old ordering, limit/offset pagination (disjoint pages, union = all, out-of-range offset = empty page with total intact), time-window filter, and the index's existence after init. Signed-off-by: Weijian He <230282609+Madin-H23@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
queryL0Paginated(v2/conversation/query) filters sessions with(session_key = ? OR session_id = ?), which cannot drive ordered access by itself. The planner was picking(user_id, agent_id, session_id)as an equality scan and then rebuilding a TEMP B-TREE for everyORDER BY timestamp DESC LIMIT/OFFSET— so each page fetch and each count got slower as history grew.idx_l0_user_agent_ts(user_id, agent_id, timestamp DESC): equality prefix + timestamp order lets the planner walk the index in ORDER BY order and stop at LIMIT+OFFSET — no per-page sort rebuild.CREATE INDEX IF NOT EXISTSat init: idempotent, 23ms on a 3.2k-row DB.Measured (standalone DB, 3,258 L0 rows)
Tests
src/core/store/sqlite/l0-paginated.test.ts(4 cases) pins the query semantics that must not change with the index: session OR filtering (bothsession_keyandsession_idhit paths + non-match exclusion), new→old ordering, limit/offset pagination (disjoint pages, union = all, out-of-range offset = empty page withtotalintact), time-window filter, and index existence afterinit().tsc --noEmitclean.