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

Avoid triggering N+1 on index page #38

Merged
merged 1 commit into from
Apr 8, 2024

Conversation

luizkowalski
Copy link
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
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
4 checks passed
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