【问题标题】:How to know which attributes are used on GROUP_BY in Rails如何知道 Rails 中的 GROUP_BY 使用了哪些属性
【发布时间】:2012-10-27 10:10:13
【问题描述】:

我正在通过在适当的地方添加索引来改进数据库表。

我想为用于 GROUP_BY 查询的属性添加索引。

有没有办法自动知道 GROUP_BY 使用了哪些字段?

【问题讨论】:

    标签: ruby-on-rails-3 postgresql indexing group-by


    【解决方案1】:

    我认为 GROUP BY 的性能与索引的存在之间没有关联。如果您有一个运行缓慢的查询,那么我首先会查看您有谓词或连接的列上的索引。

    【讨论】:

    • 要查找慢查询,请将其放入您的 /config/environments/development.rb 文件 config.active_record.auto_explain_threshold_in_seconds = 0.5
    • Bullet gem 也非常方便——它可以识别您应该调用急切加载的情况,这对于 activerecord 来说是一个很大的性能改进。
    • 在数据库端,您可以设置 'log_min_duration_statement=n' 其中 n 是以毫秒为单位的持续时间,或者查看 auto_explain postgresql.org/docs/9.2/static/auto-explain.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2014-02-14
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多