Skip to content

⚡ Stop the events homepage from running a query per event - #3136

Open
jefftriplett wants to merge 2 commits into
mainfrom
fix-events-page-performance
Open

jefftriplett wants to merge 2 commits into
mainfrom
fix-events-page-performance

Conversation

@jefftriplett

@jefftriplett jefftriplett commented Sep 20, 2026

Copy link
Copy Markdown
Member

/events/ runs a query for every event on the page.

EventHomepage loads all past and all upcoming events into Python lists, and the list template then reads next_time, is_scheduled_to_start_this_year, is_scheduled_to_end_this_year, venue and get_absolute_url for each row, and every one of those hits the database.

Two changes:

  • The homepage querysets now select_related("occurring_rule", "venue", "calendar") and prefetch_related("recurring_rules").
  • Event.next_time / Event.previous_time filter the prefetched recurring_rules in Python. They called .filter() on the related manager, which bypasses the prefetch cache and so queried once per event.

GET /events/, locally against Postgres:

events before after
90 638 queries, 0.19s 28 queries, 0.05s
2000 8362 queries, 4.17s 28 queries, 0.48s

Fixes #3125

Copilot AI lite review requested due to automatic review settings September 20, 2026 21:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

There are a few correctness/performance issues in the updated code paths (notably an incorrect return annotation and a potential performance regression when recurring_rules is not prefetched) that should be addressed before merging.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Reduces database query volume on the /events/ homepage by ensuring the list view eagerly loads related event data and by making Event.next_time / Event.previous_time able to reuse prefetched recurring_rules rather than triggering per-event queries during template rendering.

Changes:

  • Add a with_related() helper on EventHomepage and apply select_related(...) / prefetch_related(...) to the homepage querysets.
  • Update Event.next_time / Event.previous_time to filter recurring_rules in Python so the prefetch cache can be reused.
  • Add a regression test asserting homepage query count does not grow as more events are added.
File Description
apps/​events/​views.py Centralizes related-object eager loading for the events homepage and reuses a single now timestamp across homepage queries/sorts.
apps/​events/​models.py Adjusts next_time / previous_time to avoid bypassing prefetched recurring_rules cache.
apps/​events/​tests/​test_views.py Adds a query-count regression test to guard against future N+1 regressions on /events/.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread apps/events/models.py
Comment thread apps/events/views.py Outdated
Comment thread apps/events/views.py Outdated
Copilot AI review requested due to automatic review settings September 20, 2026 21:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The changes directly address the N+1 query behavior with consistent eager-loading and include a regression test guarding against query-per-row regressions.

Review effort: Lite
Findings: None

Resolved since last review (3)

Copilot AI review requested due to automatic review settings September 20, 2026 22:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The changes directly address the documented N+1 query cause, add targeted eager-loading, and include regression tests to prevent performance and rendering regressions.

Review effort: Lite
Findings: None

@jefftriplett
jefftriplett force-pushed the fix-events-page-performance branch from 6f9d3f5 to e4407b4 Compare September 20, 2026 22:20
Copilot AI review requested due to automatic review settings September 20, 2026 22:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The changes are targeted, consistent across call sites, and include a regression test specifically guarding against the original query-per-row performance issue.

Review effort: Lite
Findings: None

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.

Bug: https://www.python.org/events/ takes forever to load

2 participants