Skip to content

Conversation

fm3
Copy link
Member

@fm3 fm3 commented Apr 3, 2025

It appears the new postgres version doesn’t find an efficient query plan anymore for our old WITH query. This PR restructures the query to get the LIMIT in early.

Steps to test:

  • View annotation list, should work
  • Share some annotations with multiple teams
  • Should be still listed correctly in annotation list view (no duplicates)
  • I already tested with a postgres client that the new query is no longer slow on the large production database.

Issues:


@fm3 fm3 self-assigned this Apr 3, 2025
Copy link
Contributor

coderabbitai bot commented Apr 3, 2025

📝 Walkthrough

Walkthrough

This pull request documents a bug fix in the changelog for an annotation list loading issue and refactors the SQL query in the AnnotationDAO. The SQL query in the findAllListableExplorationals method now uses a new CTE named an to optimize performance and prevent left-join fanout issues while retrieving annotation data and associated team information.

Changes

File(s) Change Summary
CHANGELOG.unreleased.md Added a new entry in the "Fixed" section documenting a bug fix for the annotation list loading time when many annotations are present.
app/models/annotation/Annotation.scala Refactored the findAllListableExplorationals method to replace the old CTE (teams_agg) with a new CTE (an). Adjusted joins and filters to efficiently retrieve annotation details and associated team data.

Possibly related PRs

Suggested labels

refactoring

Poem

I'm a rabbit hopping through the code,
Finding bugs and refactors in a lively mode.
The query now skips the fanout plight,
With CTE magic, everything feels right.
I nibble on carrots, proud of each tweak—
Cheers to clean code and a performance peak! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1129b62 and 68fd2d6.

📒 Files selected for processing (1)
  • CHANGELOG.unreleased.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.unreleased.md
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: circleci_build

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@fm3 fm3 marked this pull request as ready for review April 3, 2025 08:16
@fm3 fm3 requested a review from MichaelBuessemeyer April 3, 2025 08:17
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
app/models/annotation/Annotation.scala (1)

407-443: Validate the query filtering and CTE usage.

The new CTE (an) cleanly gathers essential annotation properties and filters by state, access, user, and type. Ensuring indexes exist on _id, visibility, state, _user, and _dataset columns will help maintain good performance, especially on large tables.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa4dd9c and 1129b62.

📒 Files selected for processing (2)
  • CHANGELOG.unreleased.md (1 hunks)
  • app/models/annotation/Annotation.scala (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: circleci_build
🔇 Additional comments (3)
app/models/annotation/Annotation.scala (2)

402-406: Good approach to split the query.

Separating the core “annotation” selection from the eventual team aggregation helps avoid fanout and allows applying the LIMIT earlier, which should yield a more efficient query plan in Postgres.


444-494:

❓ Verification inconclusive

Consider verifying query performance with EXPLAIN and broad data.

While the refactoring is beneficial, we recommend verifying the execution plan and comparing timings in your typical staging environment. You can run an EXPLAIN (ANALYZE, BUFFERS) to confirm whether Postgres is leveraging the new CTE approach efficiently.


🏁 Script executed:

#!/bin/bash
# Description: Run an EXPLAIN ANALYZE on the new query from lines 444-494.

cat <<'EOF' > /tmp/annotation_query.sql
EXPLAIN (ANALYZE, BUFFERS)
WITH an AS (
  ...
  -- same subquery from lines 407-443
)
SELECT ...
-- same final SELECT from lines 444-494
EOF

psql -h <YOUR_DB_HOST> -U <YOUR_DB_USER> -d <YOUR_DB_NAME> -f /tmp/annotation_query.sql
rm /tmp/annotation_query.sql

Length of output: 184


Verify Query Performance Using Correct DB Connection Details

The refactoring looks promising; however, the EXPLAIN script ran with placeholder values (e.g., YOUR_DB_HOST, YOUR_DB_USER) and did not execute as intended. Please re-run the performance verification in your staging environment with the proper database connection parameters. This manual check is essential to ensure that Postgres leverages the new CTE approach efficiently.

  • Replace placeholders with your actual DB host, user, and database name.
  • Execute the script with correct parameters to get the EXPLAIN (ANALYZE, BUFFERS) output.
  • Confirm that the query execution plan and timings meet your performance criteria before merging.
CHANGELOG.unreleased.md (1)

28-28: Thanks for documenting the fix.

Adding this note about the annotated list loading fix is crucial for users tracking performance improvements and release notes.

Copy link
Contributor

@MichaelBuessemeyer MichaelBuessemeyer left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this 🎉

@fm3 fm3 enabled auto-merge (squash) April 7, 2025 07:45
@fm3 fm3 merged commit 6b2e8fd into master Apr 7, 2025
3 checks passed
@fm3 fm3 deleted the list-annotations-query-plan branch April 7, 2025 08:00
philippotto pushed a commit that referenced this pull request Apr 10, 2025
…ent query plan (#8498)

* Restructure list annotations query to guide Postgres to a more efficient query plan

* changelog

---------

Co-authored-by: MichaelBuessemeyer <39529669+MichaelBuessemeyer@users.noreply.github.com>
@philippotto philippotto mentioned this pull request Apr 10, 2025
2 tasks
philippotto added a commit that referenced this pull request Apr 10, 2025
* Fix undo in merger mode (#8463)

* rename some variables

* more renaming

* more renaming

* fix undo in merger mode

* format

* change any to number

---------

Co-authored-by: Michael Büßemeyer <frameworklinux+MichaelBuessemeyer@users.noreply.github.com>
Co-authored-by: MichaelBuessemeyer <39529669+MichaelBuessemeyer@users.noreply.github.com>

* Fix navbar menu highlighting (#8473)

* fix highlighting the correct navbar item

* update changelog

* Release 25.03.0

* add highlights

* fix changelog and migration guide for 25.03.0

* Restructure list annotations query to guide Postgres to a more efficient query plan (#8498)

* Restructure list annotations query to guide Postgres to a more efficient query plan

* changelog

---------

Co-authored-by: MichaelBuessemeyer <39529669+MichaelBuessemeyer@users.noreply.github.com>

* Fix concurrency bug in skeleton saving (#8513)

* Fix concurrency bug in skeleton saving

* changelog

* update changelog and  migration guide for release 25.03.1

---------

Co-authored-by: Michael Büßemeyer <frameworklinux+MichaelBuessemeyer@users.noreply.github.com>
Co-authored-by: MichaelBuessemeyer <39529669+MichaelBuessemeyer@users.noreply.github.com>
Co-authored-by: Tom Herold <tom.herold@scalableminds.com>
Co-authored-by: Florian M <fm3@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants