Finora's dashboard was fine with 50 transactions. At 2,000, it felt broken. Three Prisma changes fixed it — no caching layer, no rewrite.
1. A composite index that matches the query
Every dashboard query looked like this: this user's transactions, newest first, in a date range. Without an index, PostgreSQL scans the whole table. The fix is one line in the Prisma schema:
model Transaction {
id String @id @default(cuid())
userId String
date DateTime
amount Decimal
// ...
@@index([userId, date(sort: Desc)])
}
Column order matters: userId first because every query filters by user (equality), then date descending because every query sorts by it. An index on (date, userId) would be nearly useless here. Verify with EXPLAIN ANALYZE — look for "Index Scan" instead of "Seq Scan".
2. select instead of include
My dashboard cards needed four fields. I was fetching entire rows plus relations:
// before: fetches everything, including the category relation
const txs = await prisma.transaction.findMany({ where, include: { category: true } });
// after: fetches exactly what the UI renders
const txs = await prisma.transaction.findMany({
where,
select: { id: true, amount: true, date: true, category: { select: { name: true } } },
});
Less data over the wire, less memory, faster serialization. Boring — and effective.
3. Cursor pagination for infinite lists
Offset pagination (skip: page * 20) gets slower the deeper you go — the database still walks every skipped row. Cursor pagination doesn't:
const txs = await prisma.transaction.findMany({
where,
take: 20,
...(cursor ? { cursor: { id: cursor }, skip: 1 } : {}),
orderBy: { date: "desc" },
});
Each page starts exactly where the last one ended. Constant-time pages, no matter how far you scroll.
The lesson: performance work is mostly about matching the database to the query pattern. Indexes, projections, pagination — in that order.