Query Optimization in Prisma
From the Prisma ORM cheat sheet ยท Error Handling & Optimization ยท verified Nov 2026
Query Optimization
Optimize database queries for better performance and efficiency
typescript
// Use select instead of include
const users = await prisma.user.findMany({
select: {
id: true,
email: true,
posts: {
select: {
id: true,
title: true
}
}
}
})
// Pagination with cursor
const posts = await prisma.post.findMany({
take: 20,
skip: 1, // Skip the cursor
cursor: { id: lastPostId },
orderBy: { id: 'desc' }
})
// Connection pooling configuration (Prisma 7: on the driver adapter)
const prisma = new PrismaClient({
adapter: new PrismaPg({
connectionString: process.env.DATABASE_URL,
max: 10, // pool size
connectionTimeoutMillis: 10_000
})
})๐ก Use select over include for better performance
โก Add database indexes for frequently queried fields
๐ Use cursor pagination for large datasets
โ
Log slow queries with the log option or a client extension (Prisma metrics were removed in 7)
Continue with Prisma ORM
Save the full cheat sheet or work through every related task.