我最近将大约5万条记录导入ActiveAdmin应用程序,现在性能非常差。 ActiveAdmin是否设计用于处理此数量的记录?
通过在remove_filter
中添加我未使用的过滤器,我的性能略有提升。
我没有任何关联,我知道会导致一些性能问题。我的模型结构是完全平坦的,有几个子类使用单表继承。
我正在使用:
ruby '2.1.1'
gem 'rails', '4.1.0'
gem 'activeadmin', github: 'activeadmin'
我已将应用程序部署到Heroku。我已经将Heroku的一些日志从底部插入。
这是我的型号代码:
class Product < ActiveRecord::Base
# Scopes
scope :upward_trending, -> { where( "status > ?", 100) }
scope :downward_trending, -> { where( "status < ?", 100) }
scope :uncategorised, -> {where(category: '') }
scope :categorised, -> {where.not(category: '') }
end
这是我的资源代码:
ActiveAdmin.register Product do
menu :label => "All Products", :priority => 1
config.clear_action_items!
permit_params :name, :link, :category, :image_url, :price, :interest, :interest_changes, :revenue, :start_date, :end_date, :company, :country, :price_eur, :price_gbp, :price_aud, :price_nzd, :price_cad
# Input
form do |f|
f.inputs 'Details' do
f.input :country, :as => :string
f.input :category
end
f.actions
end
# Scopes
scope :upward_trending, :default => true
scope :downward_trending
scope :all
# Default Sort
config.sort_order = "end_date_desc"
index do
column "Product Name", :sortable => :name do |a|
link_to a.name, a.link, :target => "_blank"
end
column "Image" do |a|
div :class => "image_url" do
link_to (image_tag a.image_url, class: 'image_url'), a.image_url, :target => "_blank", class: 'fancybox'
end
end
column "Price", :sortable => :price_eur do |a|
div :class => "number" do
case current_user.currency
when 'EUR'
number_to_currency(a.price_eur, unit: "€")
when 'GBP'
number_to_currency(a.price_gbp, unit: "£")
when 'AUD'
number_to_currency(a.price_aud, unit: "$")
when 'CAD'
number_to_currency(a.price_cad, unit: "$")
when 'NZD'
number_to_currency(a.price_nzd, unit: "$")
else
number_to_currency(a.price, unit: "$")
end
end
end
column "Status", :sortable => :status do |a|
div :class => "average" do
number_to_percentage(a.status, precision: 0)
end
end
column :category
column "Updated", :sortable => "end_date" do |a|
if a.end_date > Time.now - 5.days
distance_of_time_in_words(a.end_date, Time.now, include_seconds: true) + " ago"
else
a.end_date.to_formatted_s(:long)
end
end
#column :company
#column :country
end
# Sidebar
#sidebar :ProductSearch, :priority => 1 do
# render partial: 'admin/search_products', :locals => {:model_name => 'products'}
#end
# Filters
filter :category, :as => :check_boxes, :collection => proc { Product.all.collect {|dd| dd.category}.uniq.sort }
#filter :name, :label => "Product Name", :as => :string, filters: ['contains']
#filter :price, :label => "USD Price"
#filter :interest, :label => "Units Sold"
#filter :company, :as => :select, :collection => proc { Product.all.collect {|dd| dd.company}.uniq.sort }
filter :country, :as => :select, :collection => proc { Product.all.collect {|dd| dd.country}.uniq.sort }
filter :end_date, :label => "Date"
remove_filter :link
remove_filter :image_url
remove_filter :price
remove_filter :interest
remove_filter :interest_changes
remove_filter :revenue
remove_filter :start_date
remove_filter :price_eur
remove_filter :price_gbp
remove_filter :price_aud
remove_filter :price_nzd
remove_filter :price_cad
end
以下是Heroku在加载资源时的一些日志,在这种情况下它会超时。
2014-09-17T21:22:09.778167+00:00 app[web.1]: Started GET "/admin/products" for 91.226.23.198 at 2014-09-17 21:22:09 +0000
2014-09-17T21:22:09.786533+00:00 app[web.1]: Processing by Admin::ProductsController#index as HTML
2014-09-17T21:22:25.828163+00:00 heroku[web.1]: source=web.1 dyno=heroku.29301280.ba6942e6-4473-477d-8fa9-b3de141f9f06 sample#load_avg_1m=0.08 sample#load_avg_5m=0.09 sample#load_avg_15m=0.04
2014-09-17T21:22:25.828431+00:00 heroku[web.1]: source=web.1 dyno=heroku.29301280.ba6942e6-4473-477d-8fa9-b3de141f9f06 sample#memory_total=670.15MB sample#memory_rss=511.80MB sample#memory_cache=0.00MB sample#memory_swap=15
8.34MB sample#memory_pgpgin=352746pages sample#memory_pgpgout=221723pages
2014-09-17T21:22:25.829347+00:00 heroku[web.1]: Process running mem=670M(130.9%)
2014-09-17T21:22:25.829678+00:00 heroku[web.1]: Error R14 (Memory quota exceeded)
2014-09-17T21:22:39.775186+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/admin/products" host=*.herokuapp.com request_id=e3abc8d7-f52d-47b2-bbb0-161823e1a596 fwd="91.226.23.198" d
yno=web.1 connect=1ms service=30001ms status=503 bytes=0
2014-09-17T21:22:40.763399+00:00 app[web.1]: E, [2014-09-17T21:22:40.714804 #2] ERROR -- : worker=0 PID:127 timeout (31s > 30s), killing
2014-09-17T21:22:41.133007+00:00 app[web.1]: E, [2014-09-17T21:22:41.132895 #2] ERROR -- : reaped #<Process::Status: pid 127 SIGKILL (signal 9)> worker=0
2014-09-17T21:22:43.505823+00:00 app[web.1]: I, [2014-09-17T21:22:43.491614 #158] INFO -- : worker=0 ready
2014-09-17T21:22:46.406853+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=x.herokuapp.com request_id=9769d818-5231-44db-ab19-d6f7597c308b fwd="91.226.23.198" dyno=web.1 connect=1ms service=5666ms
status=304 bytes=111
编辑:
我已经尝试在end_date上添加一个索引,因为我正在使用它进行排序。不幸的是,这对加载时间几乎没有任何影响:
Sep 17 15:22:34 x app/web.1: Completed 200 OK in 8556ms (Views: 7377.9ms | ActiveRecord: 1173.7ms)
Sep 17 15:23:07 x app/web.1: Completed 200 OK in 8864ms (Views: 7640.8ms | ActiveRecord: 1220.0ms)
Sep 17 15:28:47 x app/web.1: Completed 200 OK in 9551ms (Views: 8039.2ms | ActiveRecord: 1442.5ms)
Sep 17 15:29:01 x app/web.1: Completed 200 OK in 8921ms (Views: 7651.1ms | ActiveRecord: 1264.0ms)
答案 0 :(得分:2)
看起来罪魁祸首就是这行代码:
filter :country, :as => :select, :collection => proc { Product.all.collect {|dd| dd.country}.uniq.sort }
将其更改为:
filter :country, :as => :select, :collection => proc { Product.pluck(:country).uniq.sort }