Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL - utilise JSON aggregations for relationships #14532

Merged
merged 44 commits into from
Sep 11, 2024

Conversation

mike12345567
Copy link
Collaborator

Description

This PR addresses a number of issues with loading a large number of relationships in Budibase, primarily documented in: #13820.

We also have a linear ticket: https://linear.app/budibase/issue/BUDI-8311/rows-in-a-table-go-missing-when-multiple-relationships-are-attached-to which discusses the change this PR implements, moving to use JSON aggregations to retrieve relationships rather than joining them onto the main table.

The issue with our old method is that as the number of many relationships grows, the size of the response data grows exponentially. For example if a table has 10 many relationships attached to it, each with 1000 rows related to a row in the main table, this is not a response size of 10001 rows (1000 rows for each of the 10 relationships, and 1 row for the main table row) - it is somewhere in the region of a response size 1000 to the power of 10. This is an un-avoidable issue with joining, usually this would be avoided by simply not joining everything, but that is not how Budibase works, we need all relationship data available for the page so that we can calculate formulas (amongst other requirements, like relational filtering etc).

Even if we do not return the full result set we still end up in this scenario, as the sheer amount of data getting joined can cause the query to never respond. We see this when working with SQS, we can cause threads to stop responding with heavily related data.

This fix means that the main table never has any joins on it - all joins are singular and part of sub-queries. This means that the response time will always scale O(n) - with the number of rows that need joined nothing more. The sub-queries can retrieve a large number of rows from the related table (up to our hard limit) and then merge these using JSON aggregation functions into a single column per row, this works well as this is what Budibase would normally do with the data set anyway.

This also fixes some issues we have with pagination - when we limit the main query to 100, we will always get 100 rows from the main table if there is that many rows - this improvement helps implement some other features.

mike12345567 and others added 30 commits August 23, 2024 18:00
…rds-compat

Test against SQL Server 2017, get JSON aggregation of relationships working under 2017.
@mike12345567 mike12345567 self-assigned this Sep 6, 2024
@mike12345567 mike12345567 requested a review from a team as a code owner September 6, 2024 09:48
@mike12345567 mike12345567 requested review from adrinr and removed request for a team September 6, 2024 09:48
@github-actions github-actions bot added firestorm Data/Infra/Revenue Team size/xl labels Sep 6, 2024
Copy link
Collaborator

@samwho samwho left a comment

Choose a reason for hiding this comment

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

Went through this on a call with Mike just now, approval in principal. Just a few little things to look into that we've left in comments throughout.

packages/server/datasource-sha.env Show resolved Hide resolved
@mike12345567 mike12345567 merged commit 31f8691 into master Sep 11, 2024
11 of 12 checks passed
@mike12345567 mike12345567 deleted the fix/sql-many-relationships branch September 11, 2024 09:33
@github-actions github-actions bot locked and limited conversation to collaborators Sep 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
firestorm Data/Infra/Revenue Team size/xl
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants