Skip to main content

长时间运行的事务

Optimize 提供切实可行的建议,帮助你识别和解决由长时间运行的事务引起的性能问题。

¥Optimize provides actionable recommendations to help you identify and resolve performance issues caused by long-running transactions.

长时间运行的事务会锁定资源并长时间保持数据库连接,从而对可扩展性和弹性产生负面影响。以下是一个常见的存在问题的长期事务示例:

¥Long-running transactions can negatively impact scalability and resilience by locking resources and holding database connections for extended periods. Below is a common example of a problematic long-running transaction:

// Example: A single massive transaction performing multiple steps
await prisma.$transaction(async (prisma) => {
const order = await prisma.order.create({
data: {
/* ... */
},
});
await prisma.user.update({
where: { id: userId },
data: { balance: { decrement: order.total } },
});
await prisma.shipping.create({ data: { orderId: order.id /* ... */ } });
// Additional dependent operations
});

问题是什么?

¥What is the problem?

长时间运行的事务可能会导致一些严重问题,从而损害应用的性能和可靠性:

¥Long-running transactions can cause several critical issues that harm the performance and reliability of your application:

  • 数据库锁:长事务会锁定行、表或其他资源,从而阻止其他查询访问。这会导致争用和阻塞,从而严重干扰并发操作。

    ¥Database locks: Long transactions hold locks on rows, tables, or other resources, preventing access by other queries. This leads to contention and blocking, which can significantly disrupt concurrent operations.

  • 连接绑定:事务会在整个运行期间占用数据库连接。由于连接池有限,可用连接数会很快耗尽,导致应用整体速度变慢或故障。

    ¥Connection tie-ups: Transactions occupy database connections for their entire duration. With a limited connection pool, this can quickly exhaust available connections, resulting in application-wide slowdowns or failures.

  • 争用增加:随着锁的累积和连接被占用,其他事务会排队,从而造成瓶颈、更高的延迟和更低的吞吐量。

    ¥Increased contention: As locks accumulate and connections are tied up, other transactions queue up, creating bottlenecks, higher latency, and reduced throughput.

  • 可扩展性挑战:在高流量系统中,长事务导致的效率低下会被放大,从而限制系统有效扩展的能力。

    ¥Scalability challenges: Inefficiencies caused by long transactions are magnified in high-traffic systems, limiting the system’s ability to scale effectively.

  • 脆弱性:当长事务失败或超时时,所有中间进度都将丢失。这在具有多个依赖步骤的工作流中尤其成问题,因为从部分故障中恢复会变得复杂且容易出错。

    ¥Fragility: When a long transaction fails or times out, all intermediate progress is lost. This is especially problematic in workflows with multiple dependent steps, as recovering from partial failures becomes complex and error-prone.

  • 调试难题:由于长时间运行的事务步骤繁多,并且可能因超时、死锁或意外依赖导致故障,因此对其进行故障排除颇具挑战性。

    ¥Debugging difficulties: Troubleshooting long-running transactions is challenging due to their multiple steps and potential failures caused by timeouts, deadlocks, or unexpected dependencies.