feat: add missing SQLite indexes for billing and API hot-path queries #44

Merged
hodlbod merged 1 commits from userAdityaa/caravel:database-indexes into master 2026-04-22 16:40:10 +00:00
Contributor

Summary

Add missing SQLite indexes for billing and API hot-path queries.

The initial schema (0001_init.sql) had no indexes beyond primary keys and the subdomain unique constraint. Four queries executed on every billing webhook and API request were performing full table scans.

Changes

backend/migrations/0002_hot_path_indexes.sql (new file)

Adds four indexes:

Index Table Columns Covers
idx_tenant_stripe_customer_id tenant stripe_customer_id get_tenant_by_stripe_customer_id — called on every Stripe webhook
idx_relay_tenant_id relay (tenant, id) list_relays_for_tenant — called in API + billing
idx_relay_tenant_status_plan relay (tenant, status, plan) has_active_paid_relays — called on every relay deactivation
idx_activity_resource_type_resource_id_created_at_id activity (resource_type, resource_id, created_at DESC, id DESC) list_activity_for_relay — eliminates full scan + temp sort

closes #36

### Summary Add missing SQLite indexes for billing and API hot-path queries. The initial schema (`0001_init.sql`) had no indexes beyond primary keys and the `subdomain` unique constraint. Four queries executed on every billing webhook and API request were performing full table scans. ### Changes **`backend/migrations/0002_hot_path_indexes.sql`** (new file) Adds four indexes: | Index | Table | Columns | Covers | |---|---|---|---| | `idx_tenant_stripe_customer_id` | `tenant` | `stripe_customer_id` | `get_tenant_by_stripe_customer_id` — called on every Stripe webhook | | `idx_relay_tenant_id` | `relay` | `(tenant, id)` | `list_relays_for_tenant` — called in API + billing | | `idx_relay_tenant_status_plan` | `relay` | `(tenant, status, plan)` | `has_active_paid_relays` — called on every relay deactivation | | `idx_activity_resource_type_resource_id_created_at_id` | `activity` | `(resource_type, resource_id, created_at DESC, id DESC)` | `list_activity_for_relay` — eliminates full scan + temp sort | closes #36
userAdityaa added 1 commit 2026-04-22 12:18:38 +00:00
Author
Contributor

This change is purely a performance optimization aimed at improving efficiency under higher scale. It’s intended to support smoother workflows and reduce query overhead, not to act as a blocker for current functionality or progress.

This change is purely a performance optimization aimed at improving efficiency under higher scale. It’s intended to support smoother workflows and reduce query overhead, not to act as a blocker for current functionality or progress.
hodlbod merged commit 21b36272b8 into master 2026-04-22 16:40:10 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: coracle/caravel#44