Skip to content

Conversation

@kentcdodds
Copy link
Member

@kentcdodds kentcdodds commented Feb 9, 2026

Optimized read performance by adding database indexes for common query patterns. Specifically, added indexes to improve recipient lookups by phone number and past message pagination.

Test Plan

  1. Verify Recipient lookup by phoneNumber uses recipient_by_phone index:
    sqlite3 "prisma/sqlite.db" "EXPLAIN QUERY PLAN SELECT id, name FROM Recipient WHERE phoneNumber = '+15551234567' LIMIT 1;"
  2. Verify Message history pagination uses message_history_by_recipient index:
    sqlite3 "prisma/sqlite.db" "EXPLAIN QUERY PLAN SELECT id, content, sentAt FROM Message WHERE recipientId = 'r1' AND sentAt IS NOT NULL ORDER BY sentAt DESC, id DESC LIMIT 31;"
  3. Ensure existing Message unsent query remains optimized:
    sqlite3 "prisma/sqlite.db" "EXPLAIN QUERY PLAN SELECT id, updatedAt FROM Message WHERE recipientId='r1' AND sentAt IS NULL ORDER BY \"order\" ASC LIMIT 1; EXPLAIN QUERY PLAN SELECT recipientId, COUNT(*) FROM Message INDEXED BY message_unsent_by_recipient WHERE sentAt IS NULL AND recipientId IN ('r1','r2') GROUP BY recipientId;"
  4. Run Prisma and type validation:
    npx prisma validate && npx prisma migrate status
    npx prisma generate --sql
    npm run typecheck

Checklist

  • Tests updated (N/A, verified via query plans)
  • Docs updated (N/A)

Screenshots

N/A


Open in Cursor Open in Web


Note

Low Risk
Low risk schema migration adding read-only indexes; primary concern is migration time/lock during index creation on large tables.

Overview
Improves read performance by adding two new database indexes via Prisma migration.

Adds recipient_by_phone on Recipient(phoneNumber) to speed up phone-based recipient lookups, and adds message_history_by_recipient on Message(recipientId, sentAt, id) to optimize per-recipient message history pagination/order-by queries.

Written by Cursor Bugbot for commit e7fcbd4. This will update automatically on new commits. Configure here.

Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
@cursor
Copy link

cursor bot commented Feb 9, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@kentcdodds kentcdodds marked this pull request as ready for review February 9, 2026 21:38
@kentcdodds kentcdodds merged commit 94e0d9e into main Feb 9, 2026
7 checks passed
@kentcdodds kentcdodds deleted the cursor/query-read-optimization-abb7 branch February 9, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants