
dbt Model Optimization & Refactoring
Optimize and refactor your dbt models for performance and maintainability
What You Can Do
You can analyze your dbt models to identify performance bottlenecks, redundancies, and design issues, then receive concrete refactoring recommendations with implementation guidance. This skill helps you restructure models following dbt best practices, improve query efficiency, and enhance code maintainability across your entire dbt project.
Features
Identify slow queries, inefficient joins, and materialization strategy issues causing bottlenecks
Detect redundant logic, unmaintainable patterns, and violations of dbt naming conventions
Receive step-by-step refactoring plans with before/after code examples
Review existing tests and generate new test cases for refactored models
Visualize and optimize the DAG structure to reduce model interdependencies
Auto-create or improve YAML documentation for refactored models
Ensure models follow industry standards for staging, intermediate, and mart layers
Example Output
Input: Your dbt project with 50+ models
Output:
### Performance Issues Found
- stg_customers: Cartesian join in customer_transactions (20s execution time)
- fct_orders: Redundant group-by across 3 upstream models
### Refactoring Plan
1. Extract transaction aggregation into int_customer_transactions
2. Replace cartesian join with LEFT JOIN + ROW_NUMBER()
3. Add incremental logic to stg_customers (current: full refresh)
### Code Changes
-- Before
SELECT c.*, t.*
FROM raw_customers c
CROSS JOIN raw_transactions t -- Inefficient!
-- After
SELECT c.*, t.transaction_count
FROM {{ ref('stg_customers') }} c
LEFT JOIN {{ ref('int_customer_transactions') }} t
USING (customer_id)
Expected Gains: 40% query time reduction, 3 fewer dependencies, improved test coverage
What's Included
- SKILL.md: The Claude skill with dbt analysis workflows, optimization templates, and refactoring decision trees
- Model Audit Checklist: Comprehensive list of performance and maintainability criteria
- Refactoring Templates: SQL patterns for common refactoring scenarios (incremental models, staging layers, fact tables)
- Test Generation Snippets: dbt YAML templates for unit tests and model contracts
- DAG Optimization Guide: Best practices for model layering and dependency management
Who It's For
- Analytics Engineers — Refactor legacy dbt projects and improve code quality
- Data Team Leads — Establish optimization standards across your organization
- dbt Project Owners — Reduce query costs and accelerate model execution
- Data Platform Teams — Scale dbt projects while maintaining performance SLAs
Best For
- Identifying and fixing performance bottlenecks in slow-running models
- Restructuring large dbt projects to follow layered architecture (staging, intermediate, marts)
- Eliminating redundant logic and consolidating overlapping models
- Adding test coverage and documentation during refactoring
- Planning incremental materialization strategies to reduce compute costs







