stefan-luellmann.com
3 Common Mistakes Killing Your App's Speed - Stefan Lüllmann
Excerpt
Prisma makes it dangerously easy to write inefficient database queries. Because the abstraction layer is so smooth, developers often forget that a single line of JavaScript can trigger massive, resource-intensive SQL operations. I have seen production codebases where fetching a blog post title inadvertently loaded the entire database table into memory. This is highly inefficient. As developers, we need to think about the **resource cost** of our code. Bad performance doesn't just frustrate users, it inflates cloud bills and wastes infrastructure. This article breaks down the three most common Prisma performance mistakes I have encountered, and exactly how to fix them. ## Understanding the Cost ... Consider the seemingly harmless snippet: TypeScript ``` 1await db.user.findMany({ 2 include: { posts: true } 3}) ``` This snippet fetches **every user** and **every post** associated with them. That seems manageable if you have 10 users. But what if you have 100,000 users? And each has 10 posts? You are asking your database to return over a million rows of data. … ## Mistake 1: The "Select All" Trap (Over-fetching) The ``` include ``` keyword is one of the most commonly abused features in Prisma. By default, it acts like a ``` SELECT * ``` , fetching every column from the joined table. If you only need to display the titles of a user's posts, why fetch the … What exactly is happening here? First, we fetch all users (1 query). Then, the ``` for ``` loop runs. If there are 1,000 users, the code inside the loop runs 1,000 times. This results in **1,001 total requests** to your database. This will bring even a powerful server to a crawl. **The Solution:** Let Prisma handle the relation.
Related Pain Points
Prisma's query engine fetches entire tables inefficiently, causing expensive data reads and performance degradation
7Prisma's Rust-based query engine performs application-level joins by fetching entire tables and merging results in memory rather than using optimized database-level SQL joins. This approach wastes bandwidth and becomes prohibitively expensive for teams paying per-row-read to database providers like PlanetScale, and is especially problematic at scale.
Prisma enables N+1 query problems through implicit abstractions
7Prisma's smooth abstraction layer makes it easy to accidentally trigger N+1 queries where a simple loop over results causes thousands of database round-trips. A loop over 1,000 users results in 1,001 total database requests, crippling server performance.
Prisma always returns full objects even when not requested
4Prisma returns complete objects for all CRUD operations by default, even for delete operations where the return value is not needed, wasting bandwidth and computational resources.