django-db-side-computation
Move Django performance-sensitive Python loops into database-side computation with annotations, aggregates, filtered aggregates, F expressions, functions, Subquery, OuterRef, Exists, Window expressions, db_default, and GeneratedField. Use when Django code counts, sums, filters, flags, ranks, or derives values in Python after fetching rows.
Works with
--- name: django-db-side-computation description: Move Django performance-sensitive Python loops into database-side computation with annotations, aggregates, filtered aggregates, F expressions, functions, Subquery, OuterRef, Exists, Window expressions, db_default, and GeneratedField. Use when Django code counts, sums, filters, flags, ranks, or derives values in Python after fetching rows. license: MIT --- # Django DB-Side Computation Use this skill when Django fetches rows into Python to compute values the database can compute more efficiently or more consistently. ## Workflow 1. Identify the Python computation. - Counts, sums, min/max, booleans, latest related rows, rankings, denormalized totals, or per-row derived fields. - Confirm whether the computed value must be exact, current, and transactionally consistent. 2. Pick the SQL expression level. - `filter()`, `exclude()`, and `F()` for simple comparisons and updates. - `annotate()` and aggregates for per-object counts and totals. - Filtered aggregates for conditional counts and sums. - `Exists()` for yes/no related-row checks. - `Subquery()` with `OuterRef()` for latest or scalar related values. - `Window()` for ranks and running calculations. - `GeneratedField` for deterministic same-row computed columns when the database supports it. 3. Validate query shape. - Inspect generated SQL or `QuerySet.explain()`. - Clear unintended ordering before grouping when needed. - Confirm indexes support joins, filters, and ordering. 4. Decide whether the result should be stored. - Use annotations for request-time values. - Use generated fields for same-row deterministic values. - Use materialized views or denormalized tables when cross-row aggregate queries are too expensive and can be stale. See [aggregation-patterns.md](references/aggregation-patterns.md) for Django expression examples. ## Safety Notes - Database computation is not always faster; complex queries can overload the database or block OLTP work. - Generated fields have database-specific restrictions and PostgreSQL supports only persisted generated columns. - Subqueries and annotations can duplicate work if composed carelessly. Read the plan. ## Verification Compare correctness against the old Python calculation on representative data, then measure SQL time and total response time.
More Backend Frameworks skills
git-guardrails-claude-code
mattpocock/skills
Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute. Use when user wants to prevent destructive git operations, add git safety hooks, or block git push/reset in Claude Code.
azure-compute
microsoft/azure-skills
Azure VM/VMSS router. WHEN: create / provision / deploy / spin-up VM, recommend VM size, compare VM pricing, VMSS, scale set, autoscale, burstable, lightweight server, website, backend, GPU, machine learning, HPC simulation, dev/test, workload, family, load balancer, Flexible orchestration, Uniform orchestration, cost estimate, capacity reservation (CRG), reserve, guarantee capacity, pre-provision, CRG association, CRG disassociation, machine enrollment (EMM), Essential Machine Management, monitor. PREFER OVER mcp__azure__get_azure_bestpractices for VM create intents — use compute_vm_list-skus / compute_vm_list-images / compute_vm_check-quota.
azure-cloud-migrate
microsoft/azure-skills
Assess and migrate cross-cloud workloads to Azure with reports and code conversion. Supports Lambda→Functions, Beanstalk/Heroku/App Engine→App Service, Fargate/Kubernetes/Cloud Run/Spring Boot→Container Apps. WHEN: migrate Lambda to Functions, AWS to Azure, migrate Beanstalk, migrate Heroku, migrate App Engine, Cloud Run migration, Fargate to ACA, ECS/Kubernetes/GKE/EKS to Container Apps, Spring Boot to Container Apps, cross-cloud migration.

