This repository demonstrates how to use Prisma Optimize to improve query performance using the "Excessive number of rows returned" recommendation.
To successfully run the project, you will need the following:
- A database connection string supported by Prisma Optimize.
- An Optimize API key, which you can obtain from your Prisma Data Platform account.
Clone the repository, navigate into it, and install the dependencies:
git clone [email protected]:prisma/prisma-examples.git --depth=1
cd prisma-examples/optimize/optimize-excessive-rows
npm install
Create a .env
file in the root of the project directory:
cp .env.example .env
Next, open the .env
file and update the DATABASE_URL
with your database connection string and the OPTIMIZE_API_KEY
with your Optimize API key:
# .env
DATABASE_URL="__YOUR_DATABASE_CONNECTION_STRING__"
# Replace __YOUR_DATABASE_CONNECTION_STRING__ with your actual connection string.
OPTIMIZE_API_KEY="your_secure_optimize_api_key"
DATABASE_URL
: The connection string to your database.OPTIMIZE_API_KEY
: Reference the Environment API Keys section in our documentation to learn how to obtain an API key for your project using Optimize.
Perform a database migration to prepare the project:
npx prisma migrate dev --name init
If the database isn't seeded, run the following command to seed it:
npx prisma db seed
You can create recordings and view detailed insights into your queries, along with optimization recommendations, in the Optimize dashboard. To access the dashboard:
- Log in to your Prisma Data Platform account. If you haven't already, complete the onboarding process for Optimize by clicking the Get Started button.
- If Optimize hasn't been launched yet, click the Launch Optimize button.
- If you want to use a different workspace, navigate to your desired Workspace, click the Optimize tab on the left sidebar to open the Optimize dashboard. Then, if Optimize is not yet launched, click the Launch Optimize button.
Let's run the script with unoptimized Prisma queries:
-
In the Optimize dashboard, click the Start new recording button.
-
In the project terminal, run the project with:
npm run dev
-
After the script completes, you'll see a log saying "Done." Then, in the Optimize dashboard, click the Stop recording button.
-
Observe the queries with high latencies highlighted in red, and review the recommendations in the Recommendations tab. You should see the recommendation:
- Excessive number of rows returned
For more insights on this recommendation, click the Ask AI button and interact with the AI Explainer chatbot.
- Excessive number of rows returned
-
To create a reference for comparison with other recordings, rename the recording to Unoptimized queries by clicking the green recording label chip in the top left corner and typing "Unoptimized queries".
Next, let’s follow the recommendation provided by Optimize to improve the performance of the queries:
-
To improve the performance of Query 1 by addressing the Excessive number of rows returned recommendation, add a
take
option to the query:await prisma.user.findMany({ take: 10, })
-
Click the Start new recording button to begin a new recording and check for any performance improvements.
-
In the project terminal, run the project with:
npm run dev
-
After the script completes, click the Stop recording button.
-
Rename the recording to Optimized queries by clicking the recording chip in the top left corner and typing "Optimized queries."
You can now compare performance improvements by navigating to the "Optimized queries" and "Unoptimized queries" recording tabs and observing the query latency differences.
- Check out the Optimize docs.
- Share your feedback on the Prisma Discord.
- Create issues and ask questions on GitHub.