Skip to content

fix(trees): replace child/synonym LEFT JOINs with scalar subqueries#8243

Draft
grantfitzsimmons wants to merge 1 commit into
v7_12_0_7_basefrom
issue-8242
Draft

fix(trees): replace child/synonym LEFT JOINs with scalar subqueries#8243
grantfitzsimmons wants to merge 1 commit into
v7_12_0_7_basefrom
issue-8242

Conversation

@grantfitzsimmons

Copy link
Copy Markdown
Member

Fixes #8242

Note

The scalar query logic was suggested by DeepSeek V4 Pro. There may be more performant ways of accomplishing this. Feedback is welcome.

The tree view endpoint constructed queries with three independent LEFT OUTER JOINs on the same tree table, one for counting children, one for looking up the accepted parent, and one for aggregating synonyms. When expanding a node with many children, the result set would balloon as children * accepted_matches * synonyms, overwhelming the database and causing a 504 gateway timeout.

This happened because each join added rows multiplicatively before GROUP BY collapsed them with DISTINCT aggregates like count(DISTINCT ...) and GROUP_CONCAT(DISTINCT ...). For a storage tree node like "Rack 001" at SAIAB, which has hundreds of direct child storage locations, the query would run for 40+ seconds and continue executing even after the HTTP connection was severed. Page refreshes would spawn additional instances, all consuming database resources.

Testing instructions

Compare against the same database on both v7 and on issue-8242.

  1. Use the SAIAB database backup provided in the linked issue
  2. Log into any collection (e.g. "Biodiversity Occurrences")
  3. Search for "Rack 001" in the Storage tree
  4. Expand the node. It should load normally without 504
  • Browse tree nodes generally (Taxon, Geography, Storage) to confirm child_count and synonyms still display correctly.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3b45650a-69af-40f5-8de2-b82f9293f084

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-8242

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📋Back Log

Development

Successfully merging this pull request may close these issues.

1 participant