Files
Barış Keserandgoogle-labs-jules[bot] 5dde0cafbf perf: optimize N+1 query in encoder and fansub wallet updates (#350)
Refactored the `calculate_revenue` task in `billing/tasks.py` to eliminate N+1
database queries when distributing revenue to encoders and fansub group owners.

Changes:
- Replaced individual `Wallet.objects.get_or_create` and `wallet.save()` calls
  inside loops with bulk operations.
- Implemented a 'fetch-create-update' pattern:
  1. Identify all required user IDs.
  2. Bulk-create missing `Wallet` objects.
  3. Fetch all relevant wallets in a single query.
  4. Perform in-memory balance updates.
  5. Use `bulk_update` to persist changes in a single query per pool.
- Cleaned up unused imports (`VideoFile`) and variables.
- Ensured `python-dotenv` is an optional dependency in `manage.py` and
  `aniscrap_core/settings.py` for improved environment resilience.

This optimization reduces the number of database queries from O(N) to O(1)
per distribution pool, significantly improving performance for tasks with
many unique revenue recipients.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-04-21 15:25:32 +00:00
..
2026-04-19 21:05:39 +00:00