-
Notifications
You must be signed in to change notification settings - Fork 120
Closed
Description
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]]
Metadata
Metadata
Assignees
Labels
No labels