request-history: per-key query uses idx_requests_api_key + temp B-tree sort; 0.4-0.9 s per page live, 22 s on a cold copy (needs (api_key_id, timestamp DESC, request_id DESC))
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- sqlite, typescript
- Domain
- databases
Research direction
Start with queryRows in src/routing/history/indexer.ts and the index definitions in src/routing/history/schema.ts, including HISTORY_SCHEMA_VERSION. Run EXPLAIN QUERY PLAN on the per-key query against a test database to confirm the current index and temp B-tree sort. Done when the plan uses a compound (api_key_id, timestamp DESC, request_id DESC) index with no temp sort and the rows come back in the same order. Whether a schema bump or rebuild is needed is a maintainer decision.
Written by the indexing model from the issue text.
Description
Client or integration
Direct HTTP/API client
Area
Proxy and routing
Summary
The request-history query filtered by API key is slow on a large history database: 0.4-0.9 s per page live, and 22 s on a cold copy. It plans on idx_requests_api_key plus a temporary B-tree sort, so every page reads and sorts all of that key's rows and then keeps limit+1. I expected a per-key page to cost about the same as an unfiltered page. A compound index (api_key_id, timestamp DESC, request_id DESC) makes it 0.0-0.1 ms for every key size I tested, with the same rows in the same order.
Reproduction
- Use a
routing-history.sqliteof about 12.6 GB with nosqlite_stat1(ANALYZE never run). - Call the request-history query with
apiKeyIdandfrom/to(src/routing/history/indexer.ts,queryRows):
SELECT row_json, timestamp, request_id FROM requests WHERE api_key_id = ? AND timestamp >= ? AND timestamp <= ? ORDER BY timestamp DESC, request_id DESC LIMIT ? EXPLAIN QUERY PLANgives:SEARCH requests USING INDEX idx_requests_api_key (api_key_id=?)andUSE TEMP B-TREE FOR ORDER BY.
Measured, best of 2-3 runs, with the same rows returned in every arm:
- A key with 32,777 rows in 7 days (214,279 in total): 392-454 ms per page on the live database.
- A key with 104,099 rows in 7 days (360,394 in total): 880 ms live, and 22,508 ms on a cold copy.
- Forcing
idx_requests_ts: 0.1-1.0 ms on those two keys, but 474-945 ms on a 1-row key. - After
ANALYZE, the planner picksidx_requests_tsfor every key, so sparse keys become slow. CREATE INDEX idx_requests_key_ts ON requests(api_key_id, timestamp DESC, request_id DESC): 0.0-0.1 ms for keys of 1, 15, 337, 2,290 and 104,099 rows. It took 28.8 s to build and added 245 MB to the 12.6 GB database.
Proposed change: add that compound index in src/routing/history/schema.ts, and bump HISTORY_SCHEMA_VERSION if a rebuild is the migration path. The same pattern probably applies to the conversation_id and profile_id filters.
Version
Measured on 2.100.6. As of 2.100.8 and dev today, schema.ts still defines only idx_requests_api_key ON requests(api_key_id).
Operating system
macOS
Provider and model
Not provider-specific.
Logs or error output
SEARCH requests USING INDEX idx_requests_api_key (api_key_id=?)
USE TEMP B-TREE FOR ORDER BY
Checks
- I searched existing issues and documentation.
- I removed secrets, tokens, account details, request credentials, and personal data.
- Dominant language
- TypeScript
- Stars
- 16.9k
- Forks
- 1.3k
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 814
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from lidge-jun/opencodex
-
bug gui platform
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
lidge-jun/opencodex#9172 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
[Feature]: Include provider, model and account label in the `upstream failure` service.log linePossibly taken @EnesKartal claimed this 4 days ago. Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
lidge-jun/opencodex#9057 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
[Provider] antigravity-acp catalog omits gemini-4-hydrogen and gemini-4-neon that agy acp offersPossibly taken @sedoll claimed this 6 days ago. Opencatalog provider provider-compatibility
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
lidge-jun/opencodex#9037 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
bug cli
Difficulty 4/5 3-5 days Newbie friendliness 52/100
lidge-jun/opencodex#9171 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
account-pool enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 1 day
All issues in lidge-jun/opencodex
Similar issues
-
triage
Difficulty 1/5 Under an hour Newbie friendliness 64/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
ranxianglei/billion-context#7860 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
agent-oc-find-bugs api complexity → simple payment-methods recurring contributions reviewed stripe
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
opencollective/opencollective#9177 ·