{"success":true,"name":"name-repeated-sql-query-logic-with-views","description":"The same complex SQL SELECT logic is copy-pasted around.","category":"ai-brimind","path":"ai-brimind/name-repeated-sql-query-logic-with-views/SKILL.md","file":"SKILL.md","content":"---\nname: name-repeated-sql-query-logic-with-views\ndescription: \"The same complex SQL SELECT logic is copy-pasted around.\"\n---\n\n# Name Repeated SQL Query Logic With Views\n\n## When to use\nProject uses SQL, and the same filters, joins, or derived columns are duplicated across multiple queries or reports.\n\n## Example request\n'Active customer' defined in five queries → the definition changes and four copies go stale; a view names the concept once. Reporting JOINs pasted into every dashboard query → CREATE VIEW customer_summary centralizes them; queries then read like sentences. A view stacked on views stacked on views → nobody can predict its cost; view depth needs the same discipline as function nesting. Heavy aggregation view queried constantly → plain views recompute every time; that's the signal to consider a materialized view, where the engine supports it.\n\n## Instructions\nAct like a developer who refuses to define the same concept twice. When query logic repeats — a business definition like 'active subscriber', a standard set of joins, derived columns — extract it into a view with a name that states the concept, and make the copies query the view. The definition then changes in exactly one place. Keep discipline: views should be shallow (avoid towers of views on views), and remember a plain view is a saved query, not saved results — it recomputes on every read, so check the plan of queries built on it. If a heavy view is read constantly, evaluate the engine's materialized view feature with an explicit refresh strategy rather than inventing caching tables by hand. Only finalize once the repeated logic exists in one named place and you can state the cost of querying it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1131\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}