Skip to content

Manage N + 1 queries #1691

@noracato

Description

@noracato

Some database queries in the system have been identified in Sentry as N+1 queries, meaning that multiple database queries are executed where only a single, more optimized query could be used. N+1 queries result in slower performance.

Although each individual query may not take long, executing multiple unnecessary queries adds up and slows down the overall request time.

For example, selecting a scenario from all scenarios is one of the most time-expensive queries, taking about a third of a second. Already the third most expensive query takes only 9.56 milliseconds on average.

By refactoring these slow queries to use join table queries, related data can be fetched in a single query instead of multiple queries.

Steps:

  • Identify all current flagged N + 1 queries
  • Make a plan to tackle each. This can be either refactoring some code, or make better use of ActiveRecord's optimizations.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions