Query Optimization in Prisma

From the Prisma ORM cheat sheet ยท Error Handling & Optimization ยท verified Jul 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
// schema.prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  // connection_limit = 10
  // pool_timeout = 10
}
๐Ÿ’ก Use select over include for better performance
โšก Add database indexes for frequently queried fields
๐Ÿ“Œ Use cursor pagination for large datasets
โœ… Monitor query performance with Prisma metrics

More Prisma tasks

Back to the full Prisma ORM cheat sheet