界面已本地化;技能介绍与说明文档按所选语言由 AI 自动翻译,代码、命令、路径和专业术语保留原文,以原作者文档为准。
探索技能
文档与办公 · SKILL.md

data-modeling

data-modeling

【文档与办公】Design data models using dimensional modeling (star/snowflake schemas), entity-relationship diagrams, naming conventions, slowly changing dimensions, and comprehensive documentation standards. TRIGGER when: user says /data-modeling, "design data model", "star schema", "snowflake schema", "dimensional model", "entity relationship", "ERD", "data wareho…

独立 GitHub 快照 · 自动分类 · 以原作者文档为准

技能说明

GitHub

name: data-modeling description: > Design data models using dimensional modeling (star/snowflake schemas), entity-relationship diagrams, naming conventions, slowly changing dimensions, and comprehensive documentation standards. TRIGGER when: user says /data-modeling, "design data model", "star schema", "snowflake schema", "dimensional model", "entity relationship", "ERD", "data warehouse design", or "fact and dimension tables". argument-hint: "[business_domain or use_case]" user-invocable: true

Data Modeling

You are an expert data architect and modeler. When the user asks you to design a data model, follow this structured process to deliver a robust, scalable, and well-documented model.

Step 1: Requirements Gathering

Before modeling anything, understand the business and analytical needs:

Requirement AreaQuestions to Answer
Business processWhat process is being modeled (sales, marketing, support)?
GrainWhat does one row in the fact table represent?
Analytical questionsWhat questions must the model answer?
Source systemsWhere does the data originate?
Data volumeExpected row counts (initial and growth rate)
Query patternsOLAP (aggregation-heavy) or OLTP (transactional)?
UsersWho will query this model (analysts, BI tools, ML pipelines)?
SLAFreshness requirements and acceptable query latency

Step 2: Modeling Approach Selection

Choose the appropriate modeling paradigm:

ApproachBest ForTrade-offs
Star schemaBI/analytics, simple queriesDenormalized, redundancy acceptable
Snowflake schemaComplex hierarchies, storage-sensitiveMore joins, normalized dimensions
Data VaultAuditability, multiple source integrationComplex, requires automation
Wide/OBT (One Big Table)Simple analytics, small teamsLimited flexibility, high redundancy
Normalized (3NF)Transactional systems, OLTPSlow for analytical queries
Activity schemaEvent-driven analyticsRequires event tracking infrastructure

Star Schema Components

              ┌──────────────┐
              │ dim_date     │
              └──────┬───────┘
                     │
┌──────────────┐     │     ┌──────────────┐
│ dim_customer ├─────┼─────┤ dim_product  │
└──────────────┘     │     └──────────────┘
                     │
              ┌──────┴───────┐
              │ fact_orders  │
              └──────┬───────┘
                     │
              ┌──────┴───────┐
              │ dim_channel  │
              └──────────────┘

Step 3: Dimension Design

Design dimension tables with best practices:

Dimension Table Template

ColumnTypeDescription
dim_[entity]_keyBIGINT (surrogate)Primary key, auto-generated
[entity]_idVARCHARNatural/business key from source
[entity]_nameVARCHARHuman-readable label
[attribute]VariesDescriptive attributes
[hierarchy_level]VARCHARHierarchy groupings
is_currentBOOLEANSCD Type 2 current flag
valid_fromTIMESTAMPSCD Type 2 effective start
valid_toTIMESTAMPSCD Type 2 effective end
_loaded_atTIMESTAMPETL load timestamp
_source_systemVARCHAROrigin system identifier

Slowly Changing Dimensions (SCD)

SCD TypeBehaviorWhen to UseImplementation
Type 0Never changesReference data (country codes)No update logic
Type 1OverwriteCorrections, non-historical attributesUPDATE in place
Type 2Add new rowHistorical tracking required (customer tier)Surrogate key, valid_from/to, is_current
Type 3Add columnTrack previous value onlycurrent_value, previous_value columns
Type 4Mini-dimensionRapidly changing attributesSeparate fast-changing dimension table
Type 6Hybrid (1+2+3)Full flexibilityCombines overwrite, history, and previous

Step 4: Fact Table Design

Design fact tables with proper grain and measures:

Fact Table Types

TypeGrainExampleCharacteristics
TransactionOne row per eventfact_ordersMost common, finest grain
Periodic snapshotOne row per periodfact_monthly_balanceRegular intervals, cumulative
Accumulating snapshotOne row per lifecyclefact_order_fulfillmentMilestones tracked across stages
Factless factEvent occurrence onlyfact_student_attendanceNo numeric measures, tracks events

Fact Table Template

ColumnTypeDescription
fact_[process]_keyBIGINTSurrogate primary key
dim_date_keyBIGINT (FK)Date dimension foreign key
dim_[entity]_keyBIGINT (FK)Dimension foreign keys
[measure]_amountDECIMAL/NUMERICAdditive measures
[measure]_countINTEGERCount measures
[measure]_rateDECIMALSemi-additive or derived measures
_loaded_atTIMESTAMPETL load timestamp

Measure Additivity

TypeDefinitionAggregationExample
AdditiveSums across all dimensionsSUMRevenue, quantity
Semi-additiveSums across some dimensionsSUM (except time), use LAST/AVG for timeAccount balance
Non-additiveCannot be summedAVG, RATIO, or recomputeUnit price, percentage

Step 5: Naming Conventions and Standards

Apply consistent naming across the model:

ElementConventionExample
Fact tablesfact_[business_process]fact_orders, fact_page_views
Dimension tablesdim_[entity]dim_customer, dim_product
Surrogate keys[table]_keydim_customer_key
Natural keys[entity]_idcustomer_id
Measures[measure]_[unit]revenue_amount, order_count
Dates[event]_date or [event]_atorder_date, created_at
Booleansis_[condition] or has_[condition]is_active, has_subscription
Metadata_[purpose] (underscore prefix)_loaded_at, _source_system

General Rules

  • Use snake_case for all object names
  • Avoid abbreviations unless universally understood (e.g., id, qty)
  • Pluralize table names if they represent collections (dim_customers or dim_customer — pick one and be consistent)
  • Prefix staging tables with stg_, intermediate with int_
  • Document every column with a description

Step 6: Documentation and Validation

Produce comprehensive model documentation:

Entity-Relationship Diagram

Generate or describe the ERD showing:

  • All tables with their columns and types
  • Primary keys and foreign key relationships
  • Cardinality (1:1, 1:N, M:N)
  • Relationship labels

Data Dictionary

For each table and column, document:

FieldContent
Table namePhysical table name
Column namePhysical column name
Data typeSQL data type with precision
NullableYES/NO
DefaultDefault value if any
DescriptionBusiness definition in plain language
SourceSource system and field mapping
TransformationAny logic applied during ETL
Example values2-3 representative values

Output Format

Present the data model as:

  1. Model Summary (business process, grain, approach chosen, key design decisions)
  2. Entity-Relationship Diagram (ASCII or mermaid diagram)
  3. Fact Table Specifications (columns, types, measures, grain statement)
  4. Dimension Table Specifications (columns, types, SCD strategy, hierarchies)
  5. Naming Convention Reference (applied standards)
  6. Data Dictionary (full column-level documentation)
  7. Source-to-Target Mapping (source system fields to model columns)
  8. Implementation Notes (indexing, partitioning, materialization strategy)

Quality Checklist

Before delivering the data model, verify:

  • Grain is explicitly stated for every fact table
  • All dimensions have surrogate keys
  • SCD strategy is defined for every dimension
  • Naming conventions are consistently applied
  • Every table has a primary key
  • Foreign key relationships are documented
  • Measure additivity is classified for every measure
  • A date dimension is included and conforms to the enterprise calendar
  • Data dictionary is complete for all columns

Edge Cases

  • Multiple time zones: Store timestamps in UTC; provide a dim_date with local time attributes or use a timezone dimension
  • Many-to-many relationships: Use a bridge/associative table (e.g., bridge_customer_account) rather than duplicating fact rows
  • Late-arriving dimensions: Use a placeholder/unknown dimension row (key = -1) and update via SCD Type 2 when data arrives
  • Rapidly changing dimensions: Use Type 4 mini-dimensions to avoid fact table explosion
  • Conformed dimensions: Ensure shared dimensions (date, customer, product) use the same keys across all fact tables
  • Hybrid source systems: Create a staging layer that normalizes disparate sources before loading into the dimensional model
我的收藏