Project

General

Profile

Actions

Feature #44363

closed

Improve Activity view performance by preloading associations in activity providers

Added by minsuk choi about 2 months ago. Updated 21 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
Resolution:
Fixed

Description

When many events accumulate over the activity period (Setting.activity_days_default),
rendering the Activity view (/activities) can execute tens of thousands of SQL queries,
because several activity providers registered via acts_as_activity_provider are missing
preload for associations that the view (and acts_as_event options) actually access.

This is the same category of issue previously fixed for Journal in #42077 and #44252,
but it also affects the following providers:

1. Attachment (files / documents event types) - app/models/attachment.rb
`author` and `container` (polymorphic) are not preloaded. Each event triggers
separate queries for event_author and project (via container.project).

2. Document (documents event type) - app/models/document.rb
The :author option in acts_as_event re-queries `attachments` (and then `author`)
for every event instead of using a preloaded association.

3. WikiContentVersion (wiki_edits event type) - app/models/wiki_content_version.rb
`author`, `page`, `page.wiki`, and `page.wiki.project` are not preloaded.

4. TimeEntry (time_entries event type) - app/models/time_entry.rb
The :title option in acts_as_event accesses `issue` (and `issue.tracker` /
`issue.status`), none of which are preloaded.

Steps to reproduce:
1. Create enough issues/attachments/wiki edits/time entries so that many activity
events fall inside the default activity period.
2. Visit /activities.
3. Observe the number of SQL queries executed (e.g. via the Rails log, or the
bullet gem added in 6.1).

Expected: the number of queries should not grow linearly with the number of events.
Actual: it does, because of the missing preloads listed above.

The attached patch adds the missing preload calls to each provider's scope (and,
for Document, changes the author lookup to use the preloaded association instead
of re-querying). It does not change any externally visible behavior.


Files

activity-n1-preload.patch (3.2 KB) activity-n1-preload.patch minsuk choi, 2026-08-21 06:52
Actions #1

Updated by Go MAEDA 24 days ago

  • Category set to Performance

Setting the target version to 7.1.0.

Actions #2

Updated by Go MAEDA 24 days ago

  • Target version set to 7.1.0
Actions #3

Updated by Go MAEDA 21 days ago

  • Tracker changed from Patch to Feature
  • Subject changed from Activity view: several activity providers are missing preload, causing N+1 queries to Improve Activity view performance by preloading associations in activity providers
  • Status changed from New to Closed
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch in r25118. Thank you for improving Redmine.

Actions

Also available in: Atom PDF