慢速活动管理员索引页



我有 Rails 5 和活动的管理 gem 工作。在开发中,我在使用rail s运行时看到了很多查询。他们中的大多数是CACHE(0.0ms)型 . 最后几秒钟后,它显示:

Rendered /home/blabla/.rvm/gems/ruby-2.2.5@test/gems/activeadmin-1.0.0.pre4/app/views/active_admin/resource/index.html.arb (10588.9ms)
Completed 200 OK in 10646ms (Views: 10338.2ms | ActiveRecord: 264.1ms)

渲染查看时间是问题所在吗?在索引视图中,我显示了有关某些模型及其关联的大量信息。我也添加了scoped_collection方法,其中包含一些用于预先加载的包含,但它是一样的,它太慢了...... 我感谢任何帮助...

谢谢!

这可能是由于过滤器。您可以指定要包含的过滤器,否则模型上的所有属性都将作为过滤器添加。https://github.com/activeadmin/activeadmin/blob/master/docs/3-index-pages.md。

CACHE的都很好。这意味着 ActiveRecord 已经拥有数据,并且不需要执行数据库调用。

为了诊断问题,我推荐使用项目符号 gem,它将突出显示因缺少包含而导致的缓慢查询。要真正深入了解性能,您可以尝试使用带有火焰图扩展的机架迷你轮廓仪宝石。我在诊断导致慢新闻的问题方面取得了成功。通常这是一个 N+1 问题,但每次进行编辑时,开发版本都会重新编译所有 CSS。

通过使用includes加速ActiveAdmin非常简单:

controller do
def scoped_collection
super.includes(:bill_address)
end
end

请注意,生产中的渲染不包括动态编译资源,因此这在开发中可能很重要,但实际上不是问题。

如果您的模型使用has_many :through关联来设置多对多连接,则关联模型的记录数很大,由于筛选器的原因,它很慢。

class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end

https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

如果"患者"的索引较慢,最好添加以下说明。

preserve_default_filters!
remove_filter :physicians

https://activeadmin.info/3-index-pages.html

最新更新