DB Design
Good OLTP design balances integrity, write paths, query patterns, and evolution—not “third normal form everywhere.”
When to Offer This Workflow
Trigger conditions:
- - Greenfield service schema or major new domain
- Performance or integrity issues from ad-hoc tables
- Multi-tenant isolation questions
Initial offer:
Use six stages: (1) domain & access patterns, (2) entities & relationships, (3) keys & constraints, (4) normalization trade-offs, (5) indexing & performance, (6) operations & evolution). Confirm RDBMS and scale expectations.
Stage 1: Domain & Access Patterns
Goal: List critical queries and writes: QPS, joins, filters, hot rows.
Exit condition: Top access paths ranked by business importance.
Stage 2: Entities & Relationships
Goal: ER model; cardinality; optional vs required relationships.
Practices
- - Clear table names; avoid opaque “data” blobs unless documented
Stage 3: Keys & Constraints
Goal: Primary keys (surrogate vs natural); foreign keys with explicit ON DELETE policy; unique constraints for business rules.
Multi-tenant
- -
tenant_id on rows that need isolation; composite keys or indexes as appropriate
Stage 4: Normalization Trade-offs
Goal: Normalize to reduce update anomalies; denormalize read hotspots with documented trade-offs.
Stage 5: Indexing & Performance
Goal: Indexes serve real queries; watch write amplification and index bloat.
Stage 6: Operations & Evolution
Goal: Migration strategy (expand/contract); backup/restore; PII columns flagged.
Final Review Checklist
- - [ ] Access patterns drive schema
- [ ] Keys, FKs, and constraints explicit
- [ ] Multi-tenant isolation if applicable
- [ ] Normalization decisions justified
- [ ] Index plan aligned with queries
- [ ] Migration and ops considerations noted
Tips for Effective Guidance
- - NULL semantics and defaults matter for bugs and migrations.
- Pair with db-migrate for online schema changes.
Handling Deviations
- - Document stores: embed vs reference with consistency story.
技能名称: db-design
详细描述:
数据库设计
良好的OLTP设计需要平衡完整性、写入路径、查询模式与演进——而非“处处遵循第三范式”。
何时提供此工作流
触发条件:
- - 全新服务架构或重大新领域
- 因临时表导致的性能或完整性问题
- 多租户隔离相关问题
初始建议:
采用六个阶段:(1) 领域与访问模式,(2) 实体与关系,(3) 键与约束,(4) 规范化权衡,(5) 索引与性能,(6) 运维与演进。确认关系型数据库及规模预期。
阶段1:领域与访问模式
目标: 列出关键查询与写入:QPS、连接、过滤条件、热点行。
退出条件: 按业务重要性排序的顶级访问路径。
阶段2:实体与关系
目标: ER模型;基数;可选与必需关系。
实践要点
- - 表名清晰明确;除非有文档说明,否则避免使用模糊的“数据”大字段
阶段3:键与约束
目标: 主键(代理键vs自然键);含明确ON DELETE策略的外键;业务规则唯一约束。
多租户
- - 需要隔离的行添加tenant_id;酌情使用复合键或索引
阶段4:规范化权衡
目标: 通过规范化减少更新异常;对读热点进行反规范化并记录权衡。
阶段5:索引与性能
目标: 索引服务于真实查询;关注写入放大与索引膨胀。
阶段6:运维与演进
目标: 迁移策略(扩展/收缩);备份/恢复;标记PII列。
最终审查清单
- - [ ] 访问模式驱动架构设计
- [ ] 键、外键与约束明确声明
- [ ] 多租户隔离(如适用)
- [ ] 规范化决策有据可依
- [ ] 索引方案与查询对齐
- [ ] 迁移与运维注意事项已记录
有效指导建议
- - NULL语义与默认值对缺陷和迁移至关重要。
- 配合db-migrate进行在线架构变更。
偏差处理