An enterprise-grade, standardized reference skill for database modeling, schema architecture, and performance optimization. This skill serves as a built-in expert compass for making scalable architectural decisions regarding database selection, indexing, data types, and enterprise features.
Provides clear, context-aware guidelines on when to select Relational Databases (like PostgreSQL for ACID compliance) vs. Non-Relational Databases (like MongoDB/Redis for schema flexibility and high-throughput workloads).
Enforces stringent and consistent naming strategies designed to keep databases clean and understandable:
- Use
snake_caseglobally. - Table names must always be singular (e.g.,
useroverusers). - Standardized prefixing for indexes (e.g.,
idx_{table}_{columns}).
Requires essential architectural columns for maintainability:
- Mandatory
idfields (recommends UUID/UUID v7 for distributed environments). - Necessary
created_atandupdated_attimestamps usingTIMESTAMPTZ. - Utilization of database-level triggers to guarantee timestamp updates.
Avoid generic pitfalls with strict data type guidelines:
- Money/Currency: Mandates using
INTEGER(cents) orNUMERIC, strictly forbiddingFLOAT. - Timestamps: Enforces
TIMESTAMPTZfor time zone safety. - Enums: Recommends
VARCHARwith aCHECKconstraint over nativeENUMtypes for backward compatibility and alterability. - JSON: Requires
JSONBfor optimized document payloads.
Guards against performance bottlenecks:
- The Golden Rule: Always index foreign keys.
- Adherence to the "leftmost prefix rule" for composite indexes.
- Usage of partial indexes to optimize storage and query latency.
Provides nuanced guidance on balancing theoretical normalization with practical performance considerations. Covers when to maintain 3NF, when denormalization (like counter caches) is justified, and how to construct proper junction tables.
Guides you through production-ready implementations for advanced scenarios:
- Soft Deletes & Auditing: Strategies employing
deleted_atfields and immutable audit logs. - Multi-Tenancy Architecture: Options ranging from discrete databases to shared architectures backed by Row-Level Security (RLS).
- Safe Migrations: Strategies to ensure zero-downtime structural upgrades.
企业级标准化数据库建模、Schema 架构与性能优化参考指南。该技能为 AI 助手提供专家级知识库,指导其在数据库选型、索引设计、数据类型规范以及复杂企业级功能等方面做出更具可扩展性的高层架构决策。
提供清晰的场景决策指南。明确何时使用关系型数据库(如强调 ACID 的 PostgreSQL),何时采用非关系型数据库(如强调高吞吐与灵活性 MongoDB/Redis)。
施行严格、统一的命名约束,以维持数据库的可读性与一致性:
- 强制使用
snake_case下划线命名。 - 表名使用 单数形式(例如:
user而不是users)。 - 索引遵循标准化命名(如
idx_{table}_{columns})。
确保表结构具备通用性和可维护性:
- 推荐在分布式环境中使用 UUID/UUID v7 作为主键。
- 强制包含
created_at与updated_at字段,推荐使用TIMESTAMPTZ。 - 利用数据库底层触发器自动维持更新时间的准确性。
规避常见设计错误:
- 涉及金钱: 严禁使用
FLOAT,必须使用INTEGER(分)或NUMERIC类型避免精度丢失。 - 时间戳: 坚持使用带时区的
TIMESTAMPTZ。 - 枚举型: 优先使用
VARCHAR配合CHECK约束,而非原生ENUM类型,以防未来的结构修改成本过高。 - JSON载荷: 强烈推荐
JSONB替代过去的文本类型。
防止系统出现性能瓶颈:
- 黄金法则:外键必须建立索引。
- 复合索引必须遵守“最左前缀法则”,并将高基数(区分度高)字段置前。
- 运用部分索引 (Partial Indexes),节省空间开销并加速定向查询。
提供在理论第三范式与业务性能现实之间的折中方案。指导何时该反范式化处理(如计数器缓存),以及复合外键与中间表的正确构建模式。
涵盖高阶企业级需求及其实施路径:
- 软删除与审计: 设计
deleted_at软删方案并利用不可变日志表记录重要审计。 - 多租户隔离架构: 从物理分库到单表逻辑隔离,详细解析如何结合 PostgreSQL 行级安全性 (RLS) 开展安全设计。
- 零停机迁移: 制定安全的业务数据与结构升级策略。