Skip to content

Performance: N+1 queries in Account domain iteration methods #41

@philipithomas

Description

@philipithomas

Description

In app/models/account.rb, several methods iterate through domains using .each loops:

  • host() (line 66-79) calls domains.length then primary_custom_domain which iterates again
  • unverified_domain?() (line 134-139) iterates all domains
  • apex_domain() (line 91-95) iterates all domains
  • primary_custom_domain() iterates all domains

These are called on every page load via redirect_if_host_changed.

Recommendation

  • Use domains.exists?(verified: false) for unverified_domain?
  • Use domains.find_by(redirect_for_name: [nil, '']) for primary_custom_domain
  • Cache domain lookups or eager-load domains where needed

Severity

Medium

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions