Files
clawmates/migrations/0029_hot_query_indexes.sql
T
Omar Sobh 0c57f52502 migrations: 0029 — targeted indexes for hot queries
Each index added after reading its call site; no just-in-case coverage.
- outbox_queued_idx: partial (created_at) WHERE status='queued'. The
  drainer pops the oldest queued row workspace-agnostically; the existing
  (workspace_id, created_at DESC) index doesn't help.
- audit_log_workspace_event_idx: (workspace_id, event_type, created_at DESC).
  Rate limiting fires on every door tool call and A2A invocation; counts
  scan the recent tail.

Tables whose only access pattern is a PK lookup were left alone.
2026-07-05 18:48:55 -07:00

27 lines
1.3 KiB
SQL

-- Targeted indexes for hot queries whose current indexes don't match the
-- filter. Each one was chosen after reading the actual sqlx call sites; we
-- didn't add "just in case" indexes on tables whose only access pattern is
-- a PK lookup.
-- Outbox drainer: `WHERE status = 'queued' ORDER BY created_at LIMIT $1`
-- (crates/cm-db/src/repo/outbox.rs). The existing outbox_workspace_idx is
-- `(workspace_id, created_at DESC)` — no help for the workspace-agnostic
-- drainer that pops the oldest queued row. Partial index keeps it tiny.
CREATE INDEX IF NOT EXISTS outbox_queued_idx
ON outbox (created_at)
WHERE status = 'queued';
-- Audit log rate limiting: `WHERE workspace_id = $1 AND event_type = $2
-- AND created_at > now() - interval '1 hour'` fires on every door tool
-- call (cm-api/src/mcp_door.rs) and every A2A invocation. Descending
-- created_at because count()s scan the recent tail.
CREATE INDEX IF NOT EXISTS audit_log_workspace_event_idx
ON audit_log (workspace_id, event_type, created_at DESC);
-- Node rules eval loop: `SELECT ... FROM node_rules WHERE enabled`
-- (crates/cm-db/src/repo/node_rules.rs:90) runs periodically. Partial
-- index avoids indexing disabled rules.
CREATE INDEX IF NOT EXISTS node_rules_enabled_idx
ON node_rules (workspace_id)
WHERE enabled;