Closed
requested to merge github/fork/BenMorganIO/use-size-instead-of-count-for-has-many-field into master
Created by: BenMorganIO
To help improve the admin performance, I noticed that there was a long string of #count
queries going on; see before for the issue. The odd part to me was that I was doing this in my code: Course.includes(:videos)
and yet there was a constant call to #count
going on. Since the AR association is already loaded, why go back to PG? Switched from using #count
to #size
so that AR can intelligently load up the data and call count if its not there or, if it is there, just use ruby.
Before:
17:50:43 web.1 | Started GET "/admin/courses" for ::1 at 2015-11-21 17:50:43 -0800
17:50:43 web.1 | Processing by Admin::CoursesController#index as HTML
17:50:43 web.1 | Course Load (0.4ms) SELECT "courses".* FROM "courses" LIMIT 20 OFFSET 0
17:50:43 web.1 | Video Load (0.4ms) SELECT "videos".* FROM "videos" WHERE "videos"."course_id" IN (13, 14, 15, 16)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.1ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.1ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.0ms)
17:50:43 web.1 | (0.2ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."course_id" = $1 [["course_id", 13]]
17:50:43 web.1 | Rendered fields/has_many/_index.html.erb (0.8ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.0ms)
17:50:43 web.1 | (0.2ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."course_id" = $1 [["course_id", 14]]
17:50:43 web.1 | Rendered fields/has_many/_index.html.erb (1.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.0ms)
17:50:43 web.1 | (0.1ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."course_id" = $1 [["course_id", 15]]
17:50:43 web.1 | Rendered fields/has_many/_index.html.erb (0.8ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.0ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.1ms)
17:50:43 web.1 | (0.1ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."course_id" = $1 [["course_id", 16]]
17:50:43 web.1 | Rendered fields/has_many/_index.html.erb (0.9ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_collection.html.erb (45.2ms)
17:50:43 web.1 | (0.3ms) SELECT COUNT(*) FROM "courses"
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/index.html.erb within layouts/administrate/application (59.9ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_sidebar.html.erb (0.7ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_flashes.html.erb (0.1ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_javascript.html.erb (53.1ms)
17:50:43 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/layouts/administrate/application.html.erb (187.3ms)
17:50:43 web.1 | Completed 200 OK in 205ms (Views: 202.2ms | ActiveRecord: 1.7ms)
After:
17:51:21 web.1 | Started GET "/admin/courses" for ::1 at 2015-11-21 17:51:21 -0800
17:51:21 web.1 | Processing by Admin::CoursesController#index as HTML
17:51:21 web.1 | Course Load (0.4ms) SELECT "courses".* FROM "courses" LIMIT 20 OFFSET 0
17:51:21 web.1 | Video Load (0.4ms) SELECT "videos".* FROM "videos" WHERE "videos"."course_id" IN (13, 14, 15, 16)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered fields/has_many/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered fields/has_many/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered fields/has_many/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/number/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/string/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/fields/text/_index.html.erb (0.0ms)
17:51:21 web.1 | Rendered fields/has_many/_index.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_collection.html.erb (41.2ms)
17:51:21 web.1 | (0.2ms) SELECT COUNT(*) FROM "courses"
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/index.html.erb within layouts/administrate/application (55.5ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_sidebar.html.erb (0.8ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_flashes.html.erb (0.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/administrate/application/_javascript.html.erb (51.1ms)
17:51:21 web.1 | Rendered /Users/BenAMorgan/.rvm/gems/ruby-2.2.3/bundler/gems/administrate-a14e388bed54/app/views/layouts/administrate/application.html.erb (182.7ms)
17:51:21 web.1 | Completed 200 OK in 201ms (Views: 198.6ms | ActiveRecord: 1.0ms)