Query Insights
Inspect slow queries, connect Prisma calls to SQL, and apply focused fixes with Prisma Postgres.
Query Insights is built into Prisma Postgres and helps you understand which queries are slow, why they are expensive, and what to change next. It does not automatically rewrite your queries or schema.
Query Insights replaces Prisma Optimize and is now included with Prisma Postgres at no extra cost. You can try it today in the Prisma Console.
Dashboard
The main Query Insights view gives you a live summary of query activity for your database.
At the top of the page, you can inspect:
- Average latency over the selected period
- Queries per second
- A time-based chart for each metric
- Hover values for exact timestamps and measurements
- Playback controls for stepping through captured activity
This makes it easier to see whether a problem is steady, bursty, or tied to a short window of activity.
Query list
Below the charts, Query Insights shows a list of grouped queries.
Each row includes:
- Latency
- Executions
- Reads
- Last seen
- The SQL statement shape
You can use the controls above the table to:
- Filter results by table
- Sort the list to surface the most important queries first
- Focus on repeated, high-read, or recently executed statements
This view is the fastest way to identify which query patterns deserve investigation first.
Query detail
Selecting a query opens a detail view for that statement.
The detail view shows:
- A stat summary describing the query's table, execution count, average latency, and reads per call
- The full SQL statement
- An AI-generated analysis explaining whether the query needs optimization and why
- A copyable prompt you can paste directly into your editor or an AI coding assistant to apply the suggested fix
The AI analysis describes the likely cause of the performance issue, the specific change it recommends, and the expected impact. The copyable prompt includes your actual query along with context, so you can paste it into your editor or a tool like Cursor, Copilot, or Claude and get a concrete code change without switching context.
Treat the AI analysis as a starting point, not a final answer. Review any suggested change before shipping it.
Prisma ORM attribution
When using Prisma ORM, Query Insights can trace the full chain from your application code to the SQL it generates. This means you can see which prisma.* call produced a slow query, even when a single Prisma call expands into multiple SQL statements.
For raw SQL or queries issued outside Prisma ORM, Query Insights still shows full SQL behavior, but ORM-level attribution requires the steps below.
Setup
To enable ORM attribution, install the @prisma/sqlcommenter-query-insights package:
npm install @prisma/sqlcommenter-query-insightsThen pass it to the comments option in your PrismaClient constructor:
import "dotenv/config";
import { PrismaClient } from "../generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import { prismaQueryInsights } from "@prisma/sqlcommenter-query-insights";
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
export const prisma = new PrismaClient({
adapter: adapter,
comments: [prismaQueryInsights()],
})This adds SQL comment annotations to queries so Query Insights can map SQL statements back to the Prisma calls that generated them. It is built on top of the SQL comments feature in Prisma Client.
Availability
Query Insights is included with Prisma Postgres at no extra cost. You can try it today in the Prisma Console.
Typical issues
Query Insights is most useful when it connects a database symptom to a concrete code change.
| Issue | What you might see | Typical fix |
|---|---|---|
| N+1 queries | High query count for one request | Use nested reads, batching, or joins |
| Missing indexes | High reads relative to rows returned | Add the right index for the filter pattern |
| Over-fetching | Wide rows or large payloads | Use select to fetch fewer fields |
| Offset pagination | Reads grow on deeper pages | Switch to cursor pagination |
| Large nested reads | High reads and large payloads | Limit fields, limit depth, or split queries |
| Repeated queries | The same statement shape runs often | Cache or reuse results when appropriate |
How to use it
When an endpoint gets slow, Query Insights gives you a practical workflow:
- Open Query Insights and scan the latency and queries-per-second charts.
- Sort or filter the query list to isolate the expensive statement.
- Open the query detail view.
- Read the AI analysis and inspect the SQL.
- Copy the suggested prompt and paste it into your editor.
- Review the suggested change, then apply it in code or schema.
- Re-run the workload and compare the same signals again.
In most cases, the next change falls into one of these buckets:
- Change the Prisma query shape
- Add or adjust an index
- Return fewer fields or fewer rows
- Cache repeated work
Example
A common example is an N+1 pattern:
const users = await prisma.user.findMany({
select: { id: true, name: true, email: true },
});
for (const user of users) {
await prisma.post.findMany({
where: { authorId: user.id },
select: { id: true, title: true },
});
}Query Insights would typically show:
- One query to load users
- Many repeated queries to load posts
- A high execution count for the same statement shape
- More reads and latency than the route should need
In this case, the likely fix is to load the related posts in one nested read:
const usersWithPosts = await prisma.user.findMany({
select: {
id: true,
name: true,
email: true,
posts: {
select: {
id: true,
title: true,
},
},
},
});The same pattern applies to other issues. Query Insights helps you identify the expensive query shape, understand why it is expensive, and choose the next change to verify.
Next steps
- Review Connection pooling for high-concurrency workloads
- Use Direct connections when connecting Prisma Postgres from other tools
- See Prisma Client query optimization for related Prisma ORM patterns