Skip to main content

重复查询

Optimize 提供建议,帮助你识别和解决由重复查询引起的性能问题。

¥Optimize provides recommendations to help you identify and resolve performance issues caused by repeated queries.

以下针对 Post 模型的查询使用相同的参数重复执行:

¥The following query targeting the Post model is executed repeatedly with identical parameters:

await prisma.post.findMany({
where: {
published: true
},
take: 20
})

问题是什么?

¥What is the problem?

在短时间内使用相同参数多次执行相同查询时,可能会导致:

¥When the same query is executed multiple times with the same parameters within a short time frame, it can lead to:

  • 时间浪费:应用和数据库之间可能会建立新的连接,查询及其参数会发送到数据库,数据库会处理查询,并将结果返回给应用。

    ¥Time waste: A new connection may be established between the application and database, the query and its parameters are sent to the database, the database processes the query, and the results are sent back to the application.

  • 资源使用增加:查询执行会增加 CPU 和内存使用率以及磁盘 I/O,从而给数据库的系统资源带来压力。

    ¥Increased resource usage: Query execution increases CPU and memory usage, as well as disk I/O, putting strain on your database's system resources.

  • 更高成本:在无服务器数据库定价模型中,更高的资源使用率会导致成本增加。

    ¥Higher costs: In serverless database pricing models, higher resource usage can result in increased costs.

信息

要了解更多关于如何在 Prisma Postgres 中使用缓存避免重复查询的信息,请参阅 缓存文档

¥To learn more about avoiding repeated queries with caching in Prisma Postgres, refer to the caching documentation.