检测并避免由于错误的连接查询导致的100% CPU使用率。

huangapple go评论61阅读模式
英文:

Detect and avoid 100% CPU usage due to incorrect join query

问题

我们有一个 C# 应用程序,最终用户可以连接到他们的数据库服务器并运行查询以提取数据。我们在代码中使用 ADO.NET 执行查询并根据我们的应用程序处理数据。

有时,最终用户会错误地输入不正确的连接查询或连接关系,这些语法上是正确的,但不是预期的查询。这些不正确的连接或错误查询会导致数据库服务器使用更多或100%的CPU,从而引发问题。在代码或数据库级别是否有任何选项可以验证查询,以查找不相关的连接条件或关系,并避免这种情况发生?

最终用户可以手动跟踪CPU使用情况,但这不是可行的选择。我们正在寻找自动避免这种情况发生的选项,无论是在代码级别还是数据库级别。

英文:

We have a C# application in which the end user can connect to their database server and run a query to fetch the data. We execute the query using ADO.NET in the codebase and process the data according to our application.

Sometimes, the end user will mistakenly enter an incorrect join query or join relationship, which is syntactically correct but not the intended query. This incorrect join or bad query causes the database server to use a larger or 100% of its CPU, causing an issue. Are there any options at the code or database level to validate the query to find irrelevant join conditions or relationships and to avoid such scenarios?

>The end-user can manually track the CPU usage, but this is not a viable option. We are looking for options to avoid this automatically, either at the code level or the database level.

答案1

得分: 4

你可以通过以下方式来防止这种情况发生:

  • max_parallel_workers_per_gather 设置为 0,这样单个SQL语句永远不会使用多个CPU核心。

  • statement_timeout 设置为相对较低的值,这样执行时间过长的语句会被自动取消。

英文:

You can prevent that by configuring PostgreSQL as follows:

  • set max_parallel_workers_per_gather = 0, so that a single SQL statement can never use more than a single CPU core

  • set statement_timeout to a reasonably low value, so that statements that take too long are automatically canceled

huangapple
  • 本文由 发表于 2023年2月10日 16:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定