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))

Aperta
#9,103 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@shu-chen-dot-ca ci sta già lavorando.

Dal 25/11/2026.

  • #9111 di @shu-chen-dot-ca — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
sqlite, typescript
Ambito
databases

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug proxy
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
  1. Use a routing-history.sqlite of about 12.6 GB with no sqlite_stat1 (ANALYZE never run).
  2. Call the request-history query with apiKeyId and from/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 ?
  3. EXPLAIN QUERY PLAN gives: SEARCH requests USING INDEX idx_requests_api_key (api_key_id=?) and USE 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 picks idx_requests_ts for 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.
Lingua principale
TypeScript
Stelle
16.9k
Fork
1.3k
Merge medio
1g 4h
PR unite (30g)
814

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di lidge-jun/opencodex

Tutte le issue di lidge-jun/opencodex

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.