Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seeing n+1 queries even if using with_read_marks_for #111

Closed
aa365 opened this issue Oct 25, 2019 · 2 comments
Closed

seeing n+1 queries even if using with_read_marks_for #111

aa365 opened this issue Oct 25, 2019 · 2 comments

Comments

@aa365
Copy link

aa365 commented Oct 25, 2019

Running the following code, I'm seeing a lot of n+1 queries. They're hitting the ActiveRecord cache so there is no performance hit, but I was wondering if this is expected behaviour:

Event.all.with_read_marks_for(user).each { |e| puts e.unread?(user) }

Here's the log output:

User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 38], ["LIMIT", 1]]
Event Load (0.6ms)  SELECT "events".*,
                                        "read_marks".id AS read_mark_id,
                                        'User'::varchar AS read_mark_reader_type,
                                        38 AS read_mark_reader_id FROM "events" LEFT JOIN "read_marks"
                ON "read_marks".readable_type  = 'Event'
               AND "read_marks".readable_id    = "events"."id"
               AND "read_marks".reader_id      = 38
               AND "read_marks".reader_type    = 'User'
               AND "read_marks".timestamp     >= "events"."updated_at"

ReadMark Load (0.3ms)  SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."reader_id" = $1 AND "read_marks"."reader_type" = $2 AND "read_marks"."readable_type" = $3 AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT $4  [["reader_id", 38], ["reader_type", "User"], ["readable_type", "Event"], ["LIMIT", 1]]

  CACHE ReadMark Load (0.0ms)  SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."reader_id" = $1 AND "read_marks"."reader_type" = $2 AND "read_marks"."readable_type" = $3 AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT $4  [["reader_id", 38], ["reader_type", "User"], ["readable_type", "Event"], ["LIMIT", 1]]

  CACHE ReadMark Load (0.0ms)  SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."reader_id" = $1 AND "read_marks"."reader_type" = $2 AND "read_marks"."readable_type" = $3 AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT $4  [["reader_id", 38], ["reader_type", "User"], ["readable_type", "Event"], ["LIMIT", 1]]

  CACHE ReadMark Load (0.0ms)  SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."reader_id" = $1 AND "read_marks"."reader_type" = $2 AND "read_marks"."readable_type" = $3 AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT $4  [["reader_id", 38], ["reader_type", "User"], ["readable_type", "Event"], ["LIMIT", 1]]

  CACHE ReadMark Load (0.0ms)  SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."reader_id" = $1 AND "read_marks"."reader_type" = $2 AND "read_marks"."readable_type" = $3 AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT $4  [["reader_id", 38], ["reader_type", "User"], ["readable_type", "Event"], ["LIMIT", 1]]
ledermann added a commit that referenced this issue Oct 25, 2019
@ledermann
Copy link
Owner

Thanks for reporting.

Just fixed a memoization issue. Please check your code against the unreleased gem by adding this to your Gemfile:

gem 'unread', github: 'ledermann/unread'

If it works for you, I will publish a new release.

@aa365
Copy link
Author

aa365 commented Oct 26, 2019

that's fixed it. Thanks for the quick response! 👍

@aa365 aa365 closed this as completed Oct 26, 2019
HwakyoungLee pushed a commit to grepp/unread that referenced this issue May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants