Skip to content

Avoid triggering N+1 on index page#38

Merged
fractaledmind merged 1 commit intofractaledmind:mainfrom
luizkowalski:fix-unneeded-max
Apr 8, 2024
Merged

Avoid triggering N+1 on index page#38
fractaledmind merged 1 commit intofractaledmind:mainfrom
luizkowalski:fix-unneeded-max

Conversation

@luizkowalski
Copy link
Copy Markdown
Contributor

I noticed a couple of N+1, namely:

  • recent_occurrence is being computed but never used
  • occurrences.size is not being computed, triggers N+1

To test it, I created a couple of exceptions locally
image

Queries before:

web        |   SolidErrors::Error Load (2.0ms)  SELECT "solid_errors".*, MAX("solid_errors_occurrences"."created_at") AS recent_occurrence FROM "solid_errors" INNER JOIN "solid_errors_occurrences" ON "solid_errors_occurrences"."error_id" = "solid_errors"."id" WHERE "solid_errors"."resolved_at" IS NULL GROUP BY "solid_errors"."id" ORDER BY "recent_occurrence" DESC
web        |   SolidErrors::Occurrence Count (0.6ms)  SELECT COUNT(*) FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 4]]
web        |   SolidErrors::Occurrence Maximum (0.2ms)  SELECT MAX("solid_errors_occurrences"."created_at") FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 4]]
web        |   SolidErrors::Occurrence Count (0.2ms)  SELECT COUNT(*) FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 3]]
web        |   SolidErrors::Occurrence Maximum (0.2ms)  SELECT MAX("solid_errors_occurrences"."created_at") FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 3]]
web        |   SolidErrors::Occurrence Count (0.3ms)  SELECT COUNT(*) FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 2]]
web        |   SolidErrors::Occurrence Maximum (0.2ms)  SELECT MAX("solid_errors_occurrences"."created_at") FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 2]]
web        |   SolidErrors::Occurrence Count (0.3ms)  SELECT COUNT(*) FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 1]]
web        |   SolidErrors::Occurrence Maximum (0.1ms)  SELECT MAX("solid_errors_occurrences"."created_at") FROM "solid_errors_occurrences" WHERE "solid_errors_occurrences"."error_id" = $1  [["error_id", 1]]

after:

web        |   SolidErrors::Error Load (0.5ms)  SELECT "solid_errors".*, MAX("solid_errors_occurrences"."created_at") AS recent_occurrence, COUNT("solid_errors_occurrences"."id") AS occurrences_count FROM "solid_errors" INNER JOIN "solid_errors_occurrences" ON "solid_errors_occurrences"."error_id" = "solid_errors"."id" WHERE "solid_errors"."resolved_at" IS NULL GROUP BY "solid_errors"."id" ORDER BY "recent_occurrence" DESC

Copy link
Copy Markdown
Owner

@fractaledmind fractaledmind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. And great PR. Thank you

@fractaledmind fractaledmind merged commit d89e870 into fractaledmind:main Apr 8, 2024
fractaledmind added a commit that referenced this pull request Apr 9, 2024
After PR #38, ensure that last_seen_at is a DateTime object
@luizkowalski luizkowalski deleted the fix-unneeded-max branch April 23, 2024 19:01
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

Successfully merging this pull request may close these issues.

2 participants