Skip to main content

入门

先决条件

¥Prerequisites

在开始使用 Prisma Optimize 之前,请确保你已准备好以下内容:

¥Before you begin with Prisma Optimize, ensure you have the following:

  • A .

    ¥A .

  • 一个使用 Prisma 客户端 5.0.0 或更高版本的项目(我们建议使用最新版本)。

    ¥A project using Prisma Client version 5.0.0 or higher (we recommend using the latest version).

  • PostgreSQL、MySQL/MariaDB、CockroachDB 或 MS SQL Server 数据库。

    ¥A PostgreSQL, MySQL/MariaDB, CockroachDB, or MS SQL Server database.

注意

Prisma Optimize 旨在用于本地环境。在 常见问题 中了解更多信息。

¥Prisma Optimize is intended for use in local environments. Learn more in the FAQ.

1. 启动 Optimize

¥ Launch Optimize

  1. 登录你的

    ¥Log in to your .

  2. Follow the instructions 用于访问和启动 Prisma Optimize。

    ¥Follow the instructions to access and launch Prisma Optimize.

2. 将 Optimize 添加到你的应用

¥ Add Optimize to your application

2.1.安装 Optimize Prisma 客户端扩展

¥2.1. Install the Optimize Prisma Client extension

安装 Prisma 客户端和 Optimize 扩展:

¥Install Prisma Client and the Optimize extension:

npm install @prisma/client@latest @prisma/extension-optimize
Enabling tracing in older versions of Prisma ORM

对于 4.2.06.1.0 之间的 Prisma ORM 版本,你需要在 Prisma 模式文件中启用 tracing 预览功能。

¥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-js"
previewFeatures = ["tracing"]
}

2.2.将 Optimize API 密钥添加到你的 .env 文件

¥2.2. Add the Optimize API Key to your .env file

Generate a Prisma Optimize API key 并将其添加到你的 .env 文件中:

¥Generate a Prisma Optimize API key and add it to your .env file:

OPTIMIZE_API_KEY="YOUR_OPTIMIZE_API_KEY"

2.3.扩展你的 Prisma 客户端实例

¥2.3. Extend your Prisma Client instance

使用 Optimize 扩展扩展你现有的 Prisma 客户端实例:

¥Extend your existing Prisma Client instance with the Optimize extension:

import { PrismaClient } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";

const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }),
);

将 Optimize 扩展与其他扩展或中间件一起使用

¥Using the Optimize extension with other extensions or middleware

扩展程序逐个应用 开始,请确保按正确的顺序应用它们。扩展无法共享行为,最后应用的扩展具有优先权。

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

如果你在应用中使用 Prisma 加速,请确保在 Optimize 扩展之后应用它。例如:

¥If you are using Prisma Accelerate in your application, make sure you apply it after the Optimize extension. For example:

const prisma = new PrismaClient().$extends(withOptimize()).$extends(withAccelerate())

如果你在应用中使用 Prisma 中间件,请确保在任何 Prisma 客户端扩展(例如 Optimize)之前添加它们。例如:

¥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())

2.5.使用 Prisma Optimize 生成洞察

¥2.5. Use Prisma Optimize to generate insights

按照以下步骤开始使用 Prisma Optimize 生成查询洞察:

¥Follow these steps to start generating query insights with Prisma Optimize:

  1. 在“优化”仪表板中,点击“开始录制”按钮,然后运行你的应用并在录制处于活动状态时执行一些 Prisma 查询。

    ¥In the Optimize dashboard, click the Start recording button, then run your app and execute some Prisma queries while recording is active.

  2. 你的应用运行并根据执行的 Prisma 查询生成洞察后,请点击“停止录制”按钮。

    ¥After your app runs and generates insights based on the executed Prisma queries, click the Stop recording button.

  3. 点击 单个查询详情 进行探索,并查看“建议”选项卡,了解任何建议的改进措施,以提高查询性能。

    ¥Explore individual query details by clicking on them, and check the Recommendations tab for any suggested improvements to enhance query performance.

    info

    Prisma AI to understand recommendations and apply them within your Prisma model context. :::使用

如需亲身实践学习,请试用 分步示例

¥For a hands-on learning experience, try out the step-by-step example.

需要帮助吗?

¥Need help?

如果你需要帮助,请联系我们 Discord 上的 #help-and-questions 通道,或联系 我们的社区 了解其他人如何使用 Optimize。

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