Optimize default and attached thread listings with targeted indexes (#61727)

## Why

Default thread listings should stay in timestamp order without scanning subagent threads. Attached-thread listings should use both parent and archive filters even without planner statistics.

## What changed

- Add partial indexes for visible interactive threads sorted by creation, update, or recency timestamps.
- Use literal source predicates for default interactive listings so SQLite can select the partial indexes.
- Extend the attached-parent index to include archive status.

## Testing

Add a query-plan test verifying that the default sidebar listing uses the interactive update-time index. Extend app-server coverage to include `chatgpt` custom-source threads in default interactive listings.

GitOrigin-RevId: 5844622b7af629cad71e15ec95a8960739d6d447
This commit is contained in:
Dmitriy Kovalenko
2026-11-16 02:53:45 +00:00
committed by copyberry
parent eff3036bf4
commit ca0bbbdf0c
3 changed files with 123 additions and 3 deletions
@@ -0,0 +1,22 @@
-- Let attached-thread listings seek by both filters, even without planner statistics.
DROP INDEX idx_threads_attached_parent;
CREATE INDEX idx_threads_attached_parent
ON threads(attached_thread_parent_id, archived)
WHERE attached_thread_parent_id IS NOT NULL;
-- Keep the default visible chat listing in timestamp order without scanning subagents.
-- The literal source predicates match the default-source branch of push_list_threads_query.
CREATE INDEX idx_threads_interactive_created_at_ms
ON threads(archived, created_at_ms DESC, id DESC)
WHERE archived = 0 AND preview <> '' AND attached_thread_parent_id IS NULL
AND source IN ('cli', 'vscode', '{"custom":"atlas"}', '{"custom":"chatgpt"}');
CREATE INDEX idx_threads_interactive_updated_at_ms
ON threads(archived, updated_at_ms DESC, id DESC)
WHERE archived = 0 AND preview <> '' AND attached_thread_parent_id IS NULL
AND source IN ('cli', 'vscode', '{"custom":"atlas"}', '{"custom":"chatgpt"}');
CREATE INDEX idx_threads_interactive_recency_at_ms
ON threads(archived, recency_at_ms DESC, id DESC)
WHERE archived = 0 AND preview <> '' AND attached_thread_parent_id IS NULL
AND source IN ('cli', 'vscode', '{"custom":"atlas"}', '{"custom":"chatgpt"}');