-
Notifications
You must be signed in to change notification settings - Fork 35
⚡ Bolt: [performance improvement] Optimize SQLAlchemy queries to use standard GROUP BY #757
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -435,14 +435,20 @@ def get_closure_status( | |||||||||||||||||||||||
| ).scalar() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Get all confirmation counts in a single query instead of multiple round-trips | ||||||||||||||||||||||||
| from sqlalchemy import case | ||||||||||||||||||||||||
| # Optimized: Standard GROUP BY is measurably faster than multiple func.sum(case(...)) aggregations | ||||||||||||||||||||||||
| stats = db.query( | ||||||||||||||||||||||||
| func.sum(case((ClosureConfirmation.confirmation_type == 'confirmed', 1), else_=0)).label('confirmed'), | ||||||||||||||||||||||||
| func.sum(case((ClosureConfirmation.confirmation_type == 'disputed', 1), else_=0)).label('disputed') | ||||||||||||||||||||||||
| ).filter(ClosureConfirmation.grievance_id == grievance_id).first() | ||||||||||||||||||||||||
| ClosureConfirmation.confirmation_type, | ||||||||||||||||||||||||
| func.count(ClosureConfirmation.id) | ||||||||||||||||||||||||
| ).filter(ClosureConfirmation.grievance_id == grievance_id).group_by(ClosureConfirmation.confirmation_type).all() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| confirmations_count = 0 | ||||||||||||||||||||||||
| disputes_count = 0 | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| confirmations_count = stats.confirmed or 0 | ||||||||||||||||||||||||
| disputes_count = stats.disputed or 0 | ||||||||||||||||||||||||
| for ctype, count in stats: | ||||||||||||||||||||||||
| if ctype == 'confirmed': | ||||||||||||||||||||||||
| confirmations_count = count | ||||||||||||||||||||||||
| elif ctype == 'disputed': | ||||||||||||||||||||||||
| disputes_count = count | ||||||||||||||||||||||||
|
Comment on lines
+447
to
+451
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential enum values for confirmation_type. The code directly compares 🔧 Proposed fix to handle enum values for ctype, count in stats:
- if ctype == 'confirmed':
+ ctype_val = ctype.value if hasattr(ctype, 'value') else ctype
+ if ctype_val == 'confirmed':
confirmations_count = count
- elif ctype == 'disputed':
+ elif ctype_val == 'disputed':
disputes_count = count📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| required_confirmations = max(1, int(total_followers * ClosureService.CONFIRMATION_THRESHOLD)) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle potential enum values for confirmation_type.
The code directly compares
ctypeagainst string literals, but other files in this PR defensively handle enum values (seebackend/routers/admin.pylines 60-61 andbackend/routers/utility.pylines 73-74). For consistency and correctness, apply the same pattern here.🔧 Proposed fix to handle enum values
📝 Committable suggestion
🤖 Prompt for AI Agents