Patch #44363
openActivity view: several activity providers are missing preload, causing N+1 queries
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
No data to display