Skip to content

refactor(speakers): replace nested IN(SELECT DISTINCT) with correlated EXISTS in getUniqueActivitiesCountBySummit#560

Closed
mulldug wants to merge 2 commits into
mainfrom
refactor/speaker-activities-count-query
Closed

refactor(speakers): replace nested IN(SELECT DISTINCT) with correlated EXISTS in getUniqueActivitiesCountBySummit#560
mulldug wants to merge 2 commits into
mainfrom
refactor/speaker-activities-count-query

Conversation

@mulldug

@mulldug mulldug commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

ref: https://app.clickup.com/t/9014802374/86b9b1qrk

Summary

  • getUniqueActivitiesCountBySummit previously wrapped the filtered speaker set in IN(SELECT DISTINCT speaker_ids), forcing MySQL to materialise the full intermediate result and probe it per outer presentation row — observed at ~20s on moderately-sized data sets.
  • Restructured as a single correlated EXISTS rooted at PresentationSpeaker: the inner QB checks assignment or moderator membership against the outer presentation p directly, with filter predicates appended inline via apply2Query.
  • MySQL can now use the Presentation_Speakers(PresentationID) index and short-circuit on the first match per presentation row.

Test plan

  • Existing unit tests pass (OAuth2SummitSpeakersApiTest, SubmitterRepositoryTest)
  • Verify query execution time on staging with a representative data set

Summary by CodeRabbit

  • Refactor
    • Optimized backend performance for summit activity counting.

…d EXISTS in getUniqueActivitiesCountBySummit

The previous implementation wrapped the filter subquery output in IN(SELECT DISTINCT speaker_ids), forcing MySQL to materialise the full intermediate set and probe it per outer presentation row.

Restructured as a single correlated EXISTS rooted at PresentationSpeaker: the inner QB checks assignment or moderator membership against the outer presentation p directly, with filter predicates appended inline. MySQL can now use the Presentation_Speakers(PresentationID) index and short-circuit on the first match per row.
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

getUniqueActivitiesCountBySummit() in DoctrineSpeakerRepository is refactored from a single nested Doctrine query using COUNT(DISTINCT p.id) with an EXISTS subquery to two separate queries — one for assigned speakers and one for moderators — whose presentation ID results are merged and de-duplicated in PHP.

Changes

Unique Activities Count Refactor

Layer / File(s) Summary
Dual-query unique activities count
app/Repositories/Summit/DoctrineSpeakerRepository.php
getUniqueActivitiesCountBySummit() replaces the single outer Doctrine count query with two queries fetching presentation IDs for assigned speakers and moderator speakers respectively. Parameters from the inner speaker ID query are copied into both outer queries. Results are merged via array_unique() in PHP and the count is returned.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 Two queries hop where once was one,
Assigned and moderator — both get done.
Their IDs merge in PHP's embrace,
array_unique tidies up the place.
The count hops out, correct and bright,
A refactored burrow, snug and right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title claims the refactoring replaces 'nested IN(SELECT DISTINCT) with correlated EXISTS', but the actual implementation replaces correlated EXISTS with two non-correlated IN subqueries merged in PHP, which is the opposite direction. Update the title to accurately reflect the final implementation, such as: 'refactor(speakers): replace correlated EXISTS with non-correlated IN subqueries in getUniqueActivitiesCountBySummit'
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/speaker-activities-count-query

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/summit-api/openapi/pr-560/

This page is automatically updated on each push to this PR.

…nate correlated subquery

Replaced the correlated EXISTS (which scanned all PresentationSpeaker rows per outer presentation)
with two non-correlated IN subqueries — one for the assignment path, one for the moderator path —
each embedding the filtered speaker set once. Presentation ID lists are merged and deduplicated
in PHP. MySQL can now materialise the speaker set once rather than re-executing per presentation
row.
@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/summit-api/openapi/pr-560/

This page is automatically updated on each push to this PR.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
app/Repositories/Summit/DoctrineSpeakerRepository.php (1)

723-750: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Deduplicate assignment IDs before hydrating them.

Line 724 selects raw p.id from a many-speaker join, so a presentation with multiple qualifying assigned speakers still returns duplicate rows and relies on PHP to discard them. Add distinct(true) to reduce DB result size while keeping the final array_unique for assignment/moderator overlap.

♻️ Proposed change
         $assignmentQb = $this->getEntityManager()->createQueryBuilder()
+            ->distinct(true)
             ->select('p.id')
             ->from('models\summit\Presentation', 'p')
             ->join('p.speakers', 'a')
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/Repositories/Summit/DoctrineSpeakerRepository.php` around lines 723 -
750, The query in the assignmentQb query builder is joining presentations with
multiple speakers, which causes duplicate presentation IDs to be returned from
the database. Add distinct(true) to the assignmentQb query builder (the one that
selects presentation IDs where speakers are qualifying speakers using the join
on 'p.speakers') to eliminate duplicates at the database level. Keep the final
array_unique call on the merged results to handle any overlap between assignment
and moderator presentation IDs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@app/Repositories/Summit/DoctrineSpeakerRepository.php`:
- Around line 723-750: The query in the assignmentQb query builder is joining
presentations with multiple speakers, which causes duplicate presentation IDs to
be returned from the database. Add distinct(true) to the assignmentQb query
builder (the one that selects presentation IDs where speakers are qualifying
speakers using the join on 'p.speakers') to eliminate duplicates at the database
level. Keep the final array_unique call on the merged results to handle any
overlap between assignment and moderator presentation IDs.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ef47d5c4-f281-48a5-b1b4-9cee81351920

📥 Commits

Reviewing files that changed from the base of the PR and between 5a64bc5 and f85637c.

📒 Files selected for processing (1)
  • app/Repositories/Summit/DoctrineSpeakerRepository.php

@smarcet

smarcet commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

supersede by #563

@smarcet smarcet closed this Jun 22, 2026
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.

2 participants