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

Fix ModelReceipt queries #4432

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Fix ModelReceipt queries #4432

wants to merge 4 commits into from

Conversation

kitsuta
Copy link
Member

@kitsuta kitsuta commented Nov 22, 2024

Hybrid properties were the root cause of performance issues with various queries we were trying to run on model receipts. Now we return an SQL aggregate function instead, allowing you to build similar queries without the massive performance hit. This change uses the attendee receipt discrepancies and nonzero balance pages as a proof of concept -- if these become useable once on prod, then we can apply this change to related currently-unusable pages.

One important thing to note is that you should NOT ever use the new _sql aggregate functions from two different tables in one query -- this either creates a cartesian join or, if you specify joins, repeats the rows from earlier tables in the join and screws up calculations (e.g., doubling the item total). You can see how we handle needing aggregates from two different tables in the attendees_nonzero_balance page handler, where we create a subquery for the ReceiptItems table. This appears to work well alongside normal aggregate functions/outer joins for ReceiptTransactions.

For more information, see https://stackoverflow.com/questions/12484975/sqlalchemy-using-func-with-outerjoin-on-multiple-tables and https://blog.miguelgrinberg.com/post/nested-queries-with-sqlalchemy-orm

Hybrid properties were the root cause of performance issues with various queries we were trying to run on model receipts. Now we return an SQL aggregate function instead, allowing you to build similar queries without the massive performance hit. This change uses the attendee receipt discrepancies and nonzero balance pages as a proof of concept -- if these become useable once on prod, then we can apply this change to related currently-unusable pages.

One important thing to note is that you should NOT ever use the new _sql aggregate functions from two different tables in one query -- this either creates a cartesian join or, if you specify joins, repeats the rows from earlier tables in the join and screws up calculations (e.g., doubling the item total). You can see how we handle needing aggregates from two different tables in the attendees_nonzero_balance page handler, where we create a subquery for the ReceiptItems table. This appears to work well alongside normal aggregate functions/outer joins for ReceiptTransactions.

For more information, see https://stackoverflow.com/questions/12484975/sqlalchemy-using-func-with-outerjoin-on-multiple-tables and https://blog.miguelgrinberg.com/post/nested-queries-with-sqlalchemy-orm
@kitsuta kitsuta requested a review from bitbyt3r November 22, 2024 17:43
We were accidentally selecting only discrepant receipts when you had include_discrepancies off, whoops.
These were the only pages that relied on the .is_paid hybrid property, which I took out.
This will let us add items to attendee receipts for purchases that don't affect their default cost without tripping the receipt discrepancies page.

The FK stands for Foreign Key.
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.

1 participant