Skip to content

Learnmore-smart/database-design

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Database Design Skill

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.

🎯 Core Features

1. Database Paradigm Selection (SQL vs. NoSQL)

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).

2. Universal Naming Conventions

Enforces stringent and consistent naming strategies designed to keep databases clean and understandable:

  • Use snake_case globally.
  • Table names must always be singular (e.g., user over users).
  • Standardized prefixing for indexes (e.g., idx_{table}_{columns}).

3. Baseline Schema Architecture

Requires essential architectural columns for maintainability:

  • Mandatory id fields (recommends UUID/UUID v7 for distributed environments).
  • Necessary created_at and updated_at timestamps using TIMESTAMPTZ.
  • Utilization of database-level triggers to guarantee timestamp updates.

4. Data Type Best Practices & Anti-Patterns

Avoid generic pitfalls with strict data type guidelines:

  • Money/Currency: Mandates using INTEGER (cents) or NUMERIC, strictly forbidding FLOAT.
  • Timestamps: Enforces TIMESTAMPTZ for time zone safety.
  • Enums: Recommends VARCHAR with a CHECK constraint over native ENUM types for backward compatibility and alterability.
  • JSON: Requires JSONB for optimized document payloads.

5. Query Optimization & Indexing Rules

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.

6. Normalization & Relationships

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.

7. Enterprise Patterns

Guides you through production-ready implementations for advanced scenarios:

  • Soft Deletes & Auditing: Strategies employing deleted_at fields 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.

数据库设计技能 (Database Design Skill)

企业级标准化数据库建模、Schema 架构与性能优化参考指南。该技能为 AI 助手提供专家级知识库,指导其在数据库选型、索引设计、数据类型规范以及复杂企业级功能等方面做出更具可扩展性的高层架构决策。

🎯 核心功能

1. 数据库选型与决策矩阵 (SQL vs NoSQL)

提供清晰的场景决策指南。明确何时使用关系型数据库(如强调 ACID 的 PostgreSQL),何时采用非关系型数据库(如强调高吞吐与灵活性 MongoDB/Redis)。

2. 通用命名约定

施行严格、统一的命名约束,以维持数据库的可读性与一致性:

  • 强制使用 snake_case 下划线命名。
  • 表名使用 单数形式(例如:user 而不是 users)。
  • 索引遵循标准化命名(如 idx_{table}_{columns})。

3. 基础 Schema 架构规范

确保表结构具备通用性和可维护性:

  • 推荐在分布式环境中使用 UUID/UUID v7 作为主键。
  • 强制包含 created_atupdated_at 字段,推荐使用 TIMESTAMPTZ
  • 利用数据库底层触发器自动维持更新时间的准确性。

4. 数据类型最佳实践与避坑指南

规避常见设计错误:

  • 涉及金钱: 严禁使用 FLOAT,必须使用 INTEGER(分)或 NUMERIC 类型避免精度丢失。
  • 时间戳: 坚持使用带时区的 TIMESTAMPTZ
  • 枚举型: 优先使用 VARCHAR 配合 CHECK 约束,而非原生 ENUM 类型,以防未来的结构修改成本过高。
  • JSON载荷: 强烈推荐 JSONB 替代过去的文本类型。

5. 查询优化与索引规则

防止系统出现性能瓶颈:

  • 黄金法则:外键必须建立索引。
  • 复合索引必须遵守“最左前缀法则”,并将高基数(区分度高)字段置前。
  • 运用部分索引 (Partial Indexes),节省空间开销并加速定向查询。

6. 范式化与数据关系

提供在理论第三范式与业务性能现实之间的折中方案。指导何时该反范式化处理(如计数器缓存),以及复合外键与中间表的正确构建模式。

7. 企业级架构设计模式

涵盖高阶企业级需求及其实施路径:

  • 软删除与审计: 设计 deleted_at 软删方案并利用不可变日志表记录重要审计。
  • 多租户隔离架构: 从物理分库到单表逻辑隔离,详细解析如何结合 PostgreSQL 行级安全性 (RLS) 开展安全设计。
  • 零停机迁移: 制定安全的业务数据与结构升级策略。

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors