|
| 1 | +# Prisma Optimize Usage Example: Applying the "Excessive number of rows returned" Recommendation |
| 2 | + |
| 3 | +This repository demonstrates how to use [Prisma Optimize](https://pris.ly/optimize) to improve query performance using the "Excessive number of rows returned" recommendation. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +To successfully run the project, you will need the following: |
| 8 | + |
| 9 | +1. A **database connection string** supported by Prisma Optimize. |
| 10 | +2. An Optimize API key, which you can obtain from your [Prisma Data Platform](https://pris.ly/pdp) account. |
| 11 | + |
| 12 | +## Getting started |
| 13 | + |
| 14 | +### 1. Clone the repository |
| 15 | + |
| 16 | +Clone the repository, navigate into it, and install the dependencies: |
| 17 | + |
| 18 | +```bash |
| 19 | +git clone [email protected]:prisma/prisma-examples.git --depth=1 |
| 20 | +cd prisma-examples/optimize/optimize-excessive-rows |
| 21 | +npm install |
| 22 | +``` |
| 23 | + |
| 24 | +### 2. Configure environment variables |
| 25 | + |
| 26 | +Create a `.env` file in the root of the project directory: |
| 27 | + |
| 28 | +```bash |
| 29 | +cp .env.example .env |
| 30 | +``` |
| 31 | + |
| 32 | +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: |
| 33 | + |
| 34 | +```env |
| 35 | +# .env |
| 36 | +DATABASE_URL="__YOUR_DATABASE_CONNECTION_STRING__" |
| 37 | +# Replace __YOUR_DATABASE_CONNECTION_STRING__ with your actual connection string. |
| 38 | +OPTIMIZE_API_KEY="your_secure_optimize_api_key" |
| 39 | +``` |
| 40 | + |
| 41 | +- `DATABASE_URL`: The connection string to your database. |
| 42 | +- `OPTIMIZE_API_KEY`: Reference the [Environment API Keys](https://www.prisma.io/docs/platform/about#environment) section in our documentation to learn how to obtain an API key for your project using Optimize. |
| 43 | + |
| 44 | +### 3. Set up the project |
| 45 | + |
| 46 | +Perform a database migration to prepare the project: |
| 47 | + |
| 48 | +```bash |
| 49 | +npx prisma migrate dev --name init |
| 50 | +``` |
| 51 | + |
| 52 | +### 4. Open the Optimize dashboard |
| 53 | + |
| 54 | +You can create [recordings](https://pris.ly/optimize-recordings) and view detailed insights into your queries, along with optimization [recommendations](https://pris.ly/optimize-recommendations), in the Optimize dashboard. To access the dashboard: |
| 55 | + |
| 56 | +1. Log in to your [Prisma Data Platform](https://console.prisma.io/optimize) account. If you haven't already, complete the onboarding process for Optimize by clicking the **Get Started** button. |
| 57 | +2. If Optimize hasn't been launched yet, click the **Launch Optimize** button. |
| 58 | +3. If you want to use a different workspace, navigate to your desired [Workspace](https://www.prisma.io/docs/platform/about#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. |
| 59 | + |
| 60 | +### 5. Run the script |
| 61 | + |
| 62 | +Let's run the [script with unoptimized Prisma queries](./script.ts): |
| 63 | + |
| 64 | +1. In the Optimize dashboard, click the **Start new recording** button. |
| 65 | +2. In the project terminal, run the project with: |
| 66 | + |
| 67 | + ```bash |
| 68 | + npm run dev |
| 69 | + ``` |
| 70 | + |
| 71 | +3. After the script completes, you'll see a log saying "Done." Then, in the Optimize dashboard, click the **Stop recording** button. |
| 72 | +4. Observe the queries with high latencies highlighted in red, and review the recommendations in the **Recommendations** tab. You should see the recommendation: |
| 73 | + - **Excessive number of rows returned** |
| 74 | + > For more insights on this recommendation, click the **Ask AI** button and interact with the [AI Explainer](https://pris.ly/optimize-ai-chatbot) chatbot. |
| 75 | +5. 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". |
| 76 | + |
| 77 | +  |
| 78 | + |
| 79 | +### Optimize example: applying the "Excessive number of rows returned" recommendation |
| 80 | + |
| 81 | +Next, let’s follow the recommendation provided by Optimize to improve the performance of the queries: |
| 82 | + |
| 83 | +1. To improve the performance of [**Query 1**](./script.ts) by addressing the [**Excessive number of rows returned**](https://pris.ly/optimize/r/excessive-rows) recommendation, add a `take` option to the query: |
| 84 | + |
| 85 | + ```typescript |
| 86 | + await prisma.user.findMany({ |
| 87 | + take: 10, |
| 88 | + }) |
| 89 | + ``` |
| 90 | + |
| 91 | +2. Click the **Start new recording** button to begin a new recording and check for any performance improvements. |
| 92 | +3. In the project terminal, run the project with: |
| 93 | + ```bash |
| 94 | + npm run dev |
| 95 | + ``` |
| 96 | +4. After the script completes, click the **Stop recording** button. |
| 97 | +5. Rename the recording to _Optimized queries_ by clicking the recording chip in the top left corner and typing "Optimized queries." |
| 98 | + |
| 99 | +You can now compare performance improvements by navigating to the "Optimized queries" and "Unoptimized queries" recording tabs and observing the query latency differences. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Next steps |
| 104 | + |
| 105 | +- Check out the [Optimize docs](https://pris.ly/d/optimize). |
| 106 | +- Share your feedback on the [Prisma Discord](https://pris.ly/discord/). |
| 107 | +- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/). |
0 commit comments