templates() in admin:
- Replace per-template SELECT COUNT(*) articles queries with a single
GROUP BY query before the loop — O(n) SQLite calls → O(1)
- Replace per-template SELECT * LIMIT 501 (for count) with a new
count_template_data() that runs SELECT COUNT(*) — cheaper per call
- Add count_template_data() to content/__init__.py
handle_refill_monthly_credits() in worker:
- Replace N×3 per-supplier queries (fetch supplier, insert ledger,
update balance) with 2 bulk SQL statements:
1. INSERT INTO credit_ledger SELECT ... for all eligible suppliers
2. UPDATE suppliers SET credit_balance = credit_balance + monthly_credits
- Wrap in single transaction() for atomicity
- Log total suppliers updated at INFO level
audiences() in admin:
- Add LIMIT 20 guard + comment explaining why one API call per audience
is unavoidable (no bulk contacts endpoint in Resend)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>