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.

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.

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-insights

Then 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.

IssueWhat you might seeTypical fix
N+1 queriesHigh query count for one requestUse nested reads, batching, or joins
Missing indexesHigh reads relative to rows returnedAdd the right index for the filter pattern
Over-fetchingWide rows or large payloadsUse select to fetch fewer fields
Offset paginationReads grow on deeper pagesSwitch to cursor pagination
Large nested readsHigh reads and large payloadsLimit fields, limit depth, or split queries
Repeated queriesThe same statement shape runs oftenCache or reuse results when appropriate

How to use it

When an endpoint gets slow, Query Insights gives you a practical workflow:

  1. Open Query Insights and scan the latency and queries-per-second charts.
  2. Sort or filter the query list to isolate the expensive statement.
  3. Open the query detail view.
  4. Read the AI analysis and inspect the SQL.
  5. Copy the suggested prompt and paste it into your editor.
  6. Review the suggested change, then apply it in code or schema.
  7. 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

On this page