ruby-on-rails - 为 Rails 中的 Postgres 查询临时增加 'statement_timeout'?

标签 ruby-on-rails database postgresql timeout

我已经在 database.yml 中将我的 statement_timeout 配置为几秒钟,但是我的应用程序中有一些昂贵的查询,需要更长的查询执行时间。在每个查询级别上实现此目标的推荐方法是什么? 我需要暂时将 statement_timeout 设置为更大的值,执行查询并将其重置为默认值?或者甚至不需要重置?

最佳答案

我认为您只能通过更改整个连接的 statement_timeout 然后将其还原才能实现:

  def execute_expensive_query
    ActiveRecord::Base.connection.execute 'SET statement_timeout = 600000' # 10 minutes
    # DB query with long execution time
  ensure
    ActiveRecord::Base.connection.execute 'SET statement_timeout = 5000' # 5 seconds
  end

在数据库级别,您可以根据 this guide 仅为当前事务设置 statement_timeout :

BEGIN;
SET LOCAL statement_timeout = 250;
...
COMMIT;

关于ruby-on-rails - 为 Rails 中的 Postgres 查询临时增加 'statement_timeout'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50325229/

相关文章:

ruby-on-rails - 不理解类、模块和类 << self 方法

ruby-on-rails - 无法使用 minimagick Rails 打开事件存储图像附件

SQL 查询在字段中放置一个词而不是 0?

ruby-on-rails - ruby rails : Creating a link based on new database entry

postgresql - 我在哪里可以获得 pldbgapi.sql 以便为 postgresql 安装调试器?

css - 背景图片 heroku rails 不工作

ruby-on-rails - 重装工厂_女孩工厂

mysql - 在 JOIN 中选择最小值

python - 在 psycopg2 中将参数列表传递给 SQL

postgresql - 在 Gorm 模型中添加整数数组作为数据类型