我在过滤后的用户索引页面上获得了很多关于Heroko的H12超时,这是我的activeadmin页面:
ActiveAdmin.register User do
menu :parent => 'Users'
actions :all, :except => [:destroy, :new]
filter :first_name, :as => :string
filter :last_name, :as => :string
filter :email, :as => :string, filters: [:starts_with]
filter :kind, as: :select, collection: User::KINDS
filter :gender, as: :select, collection: User::GENDERS
permit_params :first_name, :last_name, :email, :status, :kind,
:raw_password, :referrer_code
index pagination_total: false do
column(:id){|u| link_to u.id, [:admin, u]}
column :first_name
column :last_name
column :email
column :kind
column :gender
column :created_at
actions
end
form do |f|
f.inputs "User Details" do
f.input :first_name
f.input :last_name
f.input :email, as: :email
f.input :raw_password, label: "New Password", hint: "Leave it blank to keep the current password"
f.input :status, as: :select, collection: User::STATUSES
f.input :kind, as: :select, collection: User::KINDS
f.input :gender, as: :select, collection: User::GENDERS
f.input :birthday, as: :date_picker
end
f.actions
end
我可以在日志中看到多重计数ex:
Started GET "/admin/users?utf8=%E2%9C%93&q%5Bfirst_name_contains%5D=&q%5Blast_name_contains%5D=&q%5Bemail_starts_with%5D=rdude244%40hotmail.com&q%5Bkind_eq%5D=&q%5Bgender_eq%5D=&commit=Filter&order=id_desc" for ::1 at 2017-11-01 10:03:05 -0400
Processing by Admin::UsersController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"first_name_contains"=>"", "last_name_contains"=>"", "email_starts_with"=>"rdude244@hotmail.com", "kind_eq"=>"", "gender_eq"=>""}, "commit"=>"Filter", "order"=>"id_desc"}
AdminUser Load (110.8ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT 1 [["id", 2]]
(1085.1ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') LIMIT 30 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') LIMIT 30 OFFSET 0) subquery_for_count
(1105.4ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') LIMIT 1 OFFSET 30) subquery_for_count
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') LIMIT 30 OFFSET 0) subquery_for_count
User Load (1531.4ms) SELECT "users".* FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') ORDER BY "users"."id" desc LIMIT 30 OFFSET 0
Rendered /Users/hugorincon/.rvm/gems/ruby-2.3.1@moviepass-admin/gems/activeadmin-1.1.0/app/views/active_admin/resource/index.html.arb (4034.8ms)
Completed 200 OK in 4484ms (Views: 223.2ms | ActiveRecord: 4046.2ms)
有一种方法可以删除SELECT COUNT(count_column)吗?减少加载时间。
答案 0 :(得分:0)
我认为限制1 /偏移30查询来自PaginatedCollection#build_pagination。我的猜测是其他查询也来自这个模块,所以我不乐观你可以在没有猴子修补的情况下禁用它们。我确实看到这些查询现在使用索引的速度提高了7-8倍,但750K表上的索引范围扫描的1500ms仍然很慢。也许还有其他数据库问题?为了找到其他查询,我会在这个模块中放置一些断点,然后逐步查看会发生什么。
答案 1 :(得分:0)
我只是删除了该请求,但每次都在heroku上获得超时。
Nov 02 16:21:36 moviepass-admin heroku/router: at=error code=H12 desc="Request timeout" method=GET path="/admin/users?utf8=%E2%9C%93&q%5Bemail_starts_with%5D=Schatzinator1%40gmail.com&commit=Filter" host=cs.moviepass.com request_id=8735fd5a-4d62-462a-9f0d-9a558413c720 fwd="65.196.51.162" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=http
Nov 02 16:21:48 moviepass-admin heroku/router: at=error code=H12 desc="Request timeout" method=GET path="/admin/users?utf8=%E2%9C%93&q%5Bemail_starts_with%5D=gnarlynoodlez.email%40gmail.com&commit=Filter" host=cs.moviepass.com request_id=57c82b9d-c70a-4f63-88aa-e48695571e36 fwd="99.42.145.134" dyno=web.4 connect=0ms service=30000ms status=503 bytes=0 protocol=http
Nov 02 16:22:01 moviepass-admin heroku/router: at=error code=H12 desc="Request timeout" method=GET path="/admin/users?utf8=%E2%9C%93&q%5Bemail_starts_with%5D=Schatzinator1%40gmail.com&commit=Filter" host=cs.moviepass.com request_id=bbc5716f-af22-4b3f-93e0-2694ff62da88 fwd="200.84.242.248" dyno=web.2 connect=0ms service=30000ms status=503 bytes=0 protocol=http
Nov 02 16:22:02 moviepass-admin heroku/router: at=error code=H12 desc="Request timeout" method=GET path="/admin/users?utf8=%E2%9C%93&q%5Bemail_starts_with%5D=pandey.nivedita02%40gmail.com&commit=Filter" host=cs.moviepass.com request_id=729400a2-18d9-4204-931f-c8302d4a6442 fwd="184.75.152.175" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=http
每次超时都有此请求:
Started GET "/admin/users?utf8=%E2%9C%93&q%5Bfirst_name_contains%5D=&q%5Blast_name_contains%5D=&q%5Bemail_starts_with%5D=rdude244%40hotmail.com&q%5Bkind_eq%5D=&q%5Bgender_eq%5D=&commit=Filter&order=id_desc" for ::1 at 2017-11-01 10:03:05 -0400
Processing by Admin::UsersController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"first_name_contains"=>"", "last_name_contains"=>"", "email_starts_with"=>"rdude244@hotmail.com", "kind_eq"=>"", "gender_eq"=>""}, "commit"=>"Filter", "order"=>"id_desc"}
AdminUser Load (110.8ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT 1 [["id", 2]]
(1085.1ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') LIMIT 30 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') LIMIT 30 OFFSET 0) subquery_for_count
User Load (1531.4ms) SELECT "users".* FROM "users" WHERE ("users"."email" ILIKE 'rdude244@hotmail.com%') ORDER BY "users"."id" desc LIMIT 30 OFFSET 0
Rendered /Users/hugorincon/.rvm/gems/ruby-2.3.1@moviepass-admin/gems/activeadmin-1.1.0/app/views/active_admin/resource/index.html.arb (4034.8ms)
Completed 200 OK in 3484ms (Views: 223.2ms | ActiveRecord: 3046.2ms)
我目前正在使用puma和4个标准x2 dynos,puma config:
workers Integer(ENV['WEB_CONCURRENCY'] || 4)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 2)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
ActiveSupport.on_load(:active_record) do
config = ActiveRecord::Base.configurations[Rails.env] ||
Rails.application.config.database_configuration[Rails.env]
config['pool'] = ENV['RAILS_MAX_THREADS'] || 5
ActiveRecord::Base.establish_connection(config)
end
end