Prisma Production Best Practices
Optimizing Prisma queries with select vs include, indexing strategy, transaction timeouts, schema organization, and query logging.
Prisma Production Best Practices
1. Always Prefer select Over Broad Queries
By default, querying a model fetches every single column. When fetching user lists, never fetch password hashes, internal tokens, or large text columns:
// β INEFFICIENT: Returns all columns including passwordHash
const users = await prisma.user.findMany();
// β
OPTIMIZED: Fetches only needed columns (Faster SQL, lower bandwidth)
const users = await prisma.user.findMany({
select: {
id: true,
name: true,
email: true,
createdAt: true,
},
});2. Add Indexes on Frequently Filtered & Sorted Columns
Every column used inside where or orderBy should be indexed in schema.prisma:
model Post {
id Int @id @default(autoincrement())
authorId String
status String
createdAt DateTime @default(now())
// Add compound index for frequent author + status queries
@@index([authorId, status])
@@index([createdAt(sort: Desc)])
}3. Enable Query Logging in Development
Log executed SQL queries and timings to diagnose N+1 query patterns:
export const prisma = new PrismaClient({
log: [
{ emit: "event", level: "query" },
{ emit: "stdout", level: "error" },
],
});
prisma.$on("query", (e) => {
console.log(`[SQL Query] ${e.query} (${e.duration}ms)`);
});Related Topics
Prisma Common Errors & Troubleshooting
Diagnose and fix the most common Prisma error codes: P2002 (Unique constraint), P2025 (Record not found), P1001 (Unreachable database), and P2003 (Foreign key constraint).
Introduction to Linux
Understand the Linux operating system, kernel architecture, distributions (Ubuntu, Debian, Alpine, RHEL), and CLI workflows.