| title | Setup |
|---|---|
| metaTitle | Getting started with optimizing queries in Prisma Postgres |
| metaDescription | Learn how to quickly set up and start optimizing Prisma Postgres queries. |
| tocDepth | 3 |
| toc | true |
Before you begin with Prisma Optimize for Prisma Postgres, ensure you have the following:
- A Prisma Data Platform account.
- A project using Prisma Client version
5.0.0or higher (we recommend using the latest version). - A Prisma Postgres database.
:::note
Prisma Optimize is intended for use in local environments. Learn more in the FAQ.
:::
- Log in to your Prisma Data Platform account.
- Click the Optimize tab on the left navigation.
- Click the Generate API key button.
- Copy the API key that appears and paste it somewhere safe, like a password manager.
- Click the copy icons to continue through each setup screen until you see the Finish & optimize button. Click that to complete the setup.
- Once you're done, Optimize will automatically begin a new recording session in the background.
Run the following command in your terminal to install the necessary dependencies:
npm install @prisma/extension-optimizeEnabling tracing in older versions of Prisma ORM
For versions of Prisma ORM between 4.2.0 and 6.1.0, you need to enable the tracing preview feature in your Prisma schema file.
generator client {
provider = "prisma-client"
output = "./generated"
previewFeatures = ["tracing"]
}Copy the Prisma Optimize API key and add it to your .env file:
OPTIMIZE_API_KEY="YOUR_OPTIMIZE_API_KEY"Extend your existing Prisma Client instance with the Optimize extension:
import { PrismaClient } from "../prisma/generated/client";
import { withAccelerate } from "@prisma/extension-optimize";
import { withOptimize } from "@prisma/extension-optimize";
const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }),
).$extends(withAccelerate());Since extensions are applied one after another, make sure you apply them in the correct order. Extensions cannot share behavior and the last extension applied takes precedence.
const prisma = new PrismaClient().$extends(withOptimize()).$extends(withAccelerate())If you are using Prisma Middleware in your application, make sure they are added before any Prisma Client extensions (like Optimize). For example:
const prisma = new PrismaClient().$use(middleware).$extends(withOptimize()).$extends(withAccelerate())Follow these steps to start generating query insights with Prisma Optimize:
-
Run your app and execute some Prisma queries while recording is active.
-
After your app runs and generates insights based on the executed Prisma queries, click the red Recording button.
-
Explore individual query details by clicking on them, and check the Recommendations tab for any suggested improvements to enhance query performance.
:::info Use Prisma AI to understand recommendations and apply them within your Prisma model context. :::
If you need assistance, reach out in the #help-and-questions channel on our Discord, or connect with our community to see how others are using Optimize.