Skip to content

Conversation

Eeshu-Yadav
Copy link

Checklist

  • I have read the OpenWISP Contributing Guidelines.
  • I have manually tested the changes proposed in this pull request.
  • I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • I have updated the documentation.

Reference to Existing Issue

Closes #383.

Description of Changes

This PR optimizes SQL queries in the notification system to address performance issues when sending notifications to multiple recipients. The main improvements include:

Performance Optimizations:

  • Reduced SQL queries from 3×N to 1-2 total queries for N recipients by implementing bulk operations
  • Bulk notification creation using Django's bulk_create() instead of individual saves
  • Aggregated storm prevention queries using Django ORM's Count() with Q() filters instead of separate COUNT queries per recipient
  • Bulk websocket updates with shared data fetching to minimize database hits

Technical Changes:

  • Modified notify_handler() in handlers.py to use bulk creation with proper signal management
  • Added bulk_check_notification_storm_and_unread_count() function for efficient multi-recipient storm checking
  • Added bulk_notification_update_handler() for optimized websocket updates

@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/383-optimize-notification-sql-queries branch from 26f2f30 to 480df32 Compare August 14, 2025 05:29
…#383

- Implemented bulk notification creation using Django's bulk_create()
- Added bulk_check_notification_storm_and_unread_count() function to perform
  aggregated queries instead of individual COUNT queries per recipient
- Added bulk_notification_update_handler() for efficient websocket updates
- Reduced SQL queries from 3*N to 1-2 total queries for N recipients
- Preserved all existing functionality including email notifications
- All tests pass with significant performance improvement

Closes openwisp#383
@Eeshu-Yadav Eeshu-Yadav force-pushed the issues/383-optimize-notification-sql-queries branch from 480df32 to f50f875 Compare August 14, 2025 05:36
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.

[bug] Sending a notification generates many SQL queries

1 participant