Introduction
In addition to quick query execution, high-performance applications need efficient and customized data retrieval. Prisma Optimize offers insights and suggestions to assist developers in finding bottlenecks, optimizing queries, and simplifying database interactions.
Building on our , navigate your existing workspace, and click on optimize at the top left corner bar.
- Click on the
settingsicon to generate API keys
Run Queries in the Application such as loading posts, creating new users, or fetching categories. These interactions will be logged in the recording session for analysis.
Stop the Recording Session once you’ve captured enough queries, in the Prisma Optimize dashboard.
Analyze the Results and explore query metrics in Prisma Optimize because the dashboard displays query types, execution times, and recommended improvements.
From the recorded session, the current query to fetch posts has Excessive number of rows returned and Prisma optimize recommends:
We recommend using the take option to limit the number of entries returned by your queries:
Applying Prisma Optimize Recommendations
Let’s apply a few common recommendations Prisma Optimize suggests for our CMS project.
Excessive number of rows returned
Solution: take option to limit the number of entries returned.
prisma.post.findMany({
take: 10, // Limits the number of results to 10
// other query parameters
})
Excessive Data Retrieval
Solution: Use select to fetch only required fields:
const posts = await prisma.post.findMany({
select: { id: true, title: true, content: true },
});
Insights on AI-Powered Recommendations
I asked questions I needed clarification on based on recommendations and the above was the response from AI.
Benchmarking Improvements
After applying Prisma Optimize’s recommendations, we measure the impact on query performance using benchmarking techniques.
export async function GET(req) {
const posts = await prisma.post.findMany({ take: 10, select: { id: true, title: true } , cacheStrategy: { ttl: 3600 },})
return new Response(JSON.stringify(posts), {
headers: { 'Content-Type': 'application/json' },
});
}
Initial Query Performance (Before Optimization)
## Performance Comparison Before and After Optimization
| Request | Response Time Before Optimization | Response Time After Optimization |
|-------------------|-----------------------------------|----------------------------------|
| GET /api/posts | 3975 ms | 265 ms |
| GET /api/posts | 3975 ms | 154 ms |
| GET /api/posts | 3975 ms | 106 ms |
| GET /api/posts | 3975 ms | 158 ms |
| GET /api/posts | 3975 ms | 118 ms |
| GET /api/posts | 3975 ms | 128 ms |
This table reflects the improvement in response times after optimization.
All the codes can be found in this repository.
Conclusion
Including Prisma Optimize in our Prisma Accelerate CMS project has shown how well-focused query optimizations improve application speed. Providing insightful analysis, also helps developers lower database load and increase response times.
Prisma Optimize is best for applications that:
- Handle large datasets and require efficient data retrieval.
- Operate in serverless environments where database connections need to be managed effectively.
- Experience high traffic and need scalable solutions to maintain performance under load.
SOCIAL SHARE CARD GENERATOR