0

In my Rails app I have the N+1 problem where I'm making extra call(s) to the database to get associated data over and over again, specially when logging impressions per model.

For example:

Started GET "/" for 127.0.0.1 at 2018-12-02 16:21:05 -0500
Processing by JobsController#index as HTML
  Job Load (4.4ms)  SELECT  "jobs".* FROM "jobs" WHERE "jobs"."published_at" IS NOT NULL ORDER BY "jobs"."published_at" DESC LIMIT $1 OFFSET $2  [["LIMIT", 30], ["OFFSET", 0]]
  ↳ app/controllers/jobs_controller.rb:22
  User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 13], ["LIMIT", 1]]
  ↳ app/controllers/jobs_controller.rb:24
  Impression Exists (1.3ms)  SELECT  1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 AND "impressions"."session_hash" = $3 LIMIT $4  [["impressionable_id", 705], ["impressionable_type", "Job"], ["session_hash", "d80d52dd401011a626d600167140e49f"], ["LIMIT", 1]]
  ↳ app/controllers/jobs_controller.rb:24
  Impression Exists (0.6ms)  SELECT  1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 AND "impressions"."session_hash" = $3 LIMIT $4  [["impressionable_id", 704], ["impressionable_type", "Job"], ["session_hash", "d80d52dd401011a626d600167140e49f"], ["LIMIT", 1]]
  ↳ app/controllers/jobs_controller.rb:24
  Impression Exists (0.4ms)  SELECT  1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 AND "impressions"."session_hash" = $3 LIMIT $4  [["impressionable_id", 703], ["impressionable_type", "Job"], ["session_hash", "d80d52dd401011a626d600167140e49f"], ["LIMIT", 1]]
  ↳ app/controllers/jobs_controller.rb:24

I'm using the impressionist gem and using the impressionist method directly, taking all of the Jobs on the page and logging an impression. The problem is because I'm only recording unique impressions, for records that already have an impression, these additional calls are redundant. I tried to use @jobs.includes(:impressions).each to preload the associated data hoping Rails was smart enough to figure out which records existed, but Rails still outputs numerous Impression Exists queries.

@jobs.each{|job| impressionist(job,'', :unique => [:session_hash])}
VegaStudios
  • 378
  • 1
  • 4
  • 22
  • please try with https://github.com/flyerhzm/bullet – ray Dec 03 '18 at 06:16
  • I'll try this out and see if it's more contextual – VegaStudios Dec 03 '18 at 16:35
  • Further discussion and an answer from John McAliley takes place here, https://github.com/charlotte-ruby/impressionist/issues/272 – VegaStudios Dec 08 '18 at 15:54
  • A link to a potential solution is always welcome, but please [add context around the link](https://meta.stackoverflow.com/a/8259/169503) so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being _barely more than a link to an external site_ is a possible reason as to [Why and how are some answers deleted?](https://stackoverflow.com/help/deleted-answers). – elixenide Dec 09 '18 at 13:58

0 Answers0