{"success":true,"name":"precompute-sql-aggregates-only-after-measuring-the-live-quer","description":"Tempted to maintain a SQL summary table or counter column.","category":"ai-brimind","path":"ai-brimind/precompute-sql-aggregates-only-after-measuring-the-live-quer/SKILL.md","file":"SKILL.md","content":"---\nname: precompute-sql-aggregates-only-after-measuring-the-live-quer\ndescription: \"Tempted to maintain a SQL summary table or counter column.\"\n---\n\n# Precompute SQL Aggregates Only After Measuring the Live Query\n\n## When to use\nProject uses SQL, and you are about to add denormalized counters or summary tables (like a cached order_count) before proving the live aggregate is too slow.\n\n## Example request\ncomments_count column added on day one → it drifts from reality after the first missed code path; every denormalized value is a sync obligation. Live COUNT assumed slow → EXPLAIN showed it reads an index in milliseconds; the optimization solved an imagined problem. Summary genuinely needed after measurement → the engine may offer materialized views with refresh; check built-ins before hand-rolling sync logic. Counter updated in five code paths → the sixth path forgets; if you must denormalize, centralize the update in one place (one function, or a database mechanism).\n\n## Instructions\nAct like an engineer who buys complexity only with measured evidence. When a computed value (count, sum, ranking) seems to need caching in a column or summary table: first measure the honest version — run the live aggregate with EXPLAIN ANALYZE at realistic volume, often it's fine with the right index. Only if it's proven too slow for its real usage, denormalize deliberately: prefer the engine's built-in (materialized views with a refresh strategy) over hand-maintained tables; if hand-maintained, funnel every update through exactly one code path so drift has one place to be wrong, and write a reconciliation check that can detect and repair divergence. Every denormalized value is a standing promise to keep two truths equal — count that cost. Only proceed once the slowness is measured, the sync mechanism is singular, and drift is detectable.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1138\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}