Loading...
Loading...
Compare original and translation side by side
| # | Question | What It Determines |
|---|---|---|
| 1 | What entities does the system manage? | Table names |
| 2 | What are the relationships between entities? | Foreign keys, join tables |
| 3 | What are the key attributes of each entity? | Column definitions |
| 4 | What are the primary query patterns? | Index strategy |
| 5 | What is the expected data volume? (rows, growth rate) | Partitioning, scaling |
| 6 | What is the read/write ratio? | Normalization vs denormalization |
| 7 | SQL or NoSQL? (or both?) | Storage engine selection |
| # | 问题 | 作用 |
|---|---|---|
| 1 | 系统需要管理哪些实体? | 确定表名 |
| 2 | 实体之间的关系是什么? | 确定外键、关联表 |
| 3 | 每个实体的核心属性有哪些? | 确定列定义 |
| 4 | 核心查询模式是什么? | 确定索引策略 |
| 5 | 预期数据量级是多少?(行数、增长速率) | 确定分区、扩容方案 |
| 6 | 读写比例是多少? | 权衡规范化与反规范化 |
| 7 | 用SQL还是NoSQL?(还是两者结合?) | 确定存储引擎选型 |
| Factor | Choose SQL (PostgreSQL, MySQL) | Choose Document (MongoDB) | Choose Key-Value (Redis) |
|---|---|---|---|
| Data shape | Structured, relational | Semi-structured, nested | Simple lookups, caching |
| Query complexity | Complex joins, aggregations | Document-level queries | Key-based access only |
| Consistency needs | ACID required | Eventual consistency OK | Ephemeral or cached data |
| Schema evolution | Migrations manageable | Schema-free flexibility | No schema |
| Scale pattern | Vertical first, then read replicas | Horizontal sharding | In-memory, limited size |
| 考量因素 | 选择SQL(PostgreSQL、MySQL) | 选择文档型数据库(MongoDB) | 选择键值对数据库(Redis) |
|---|---|---|---|
| 数据形态 | 结构化、关系型 | 半结构化、嵌套结构 | 简单查询、缓存场景 |
| 查询复杂度 | 复杂关联、聚合查询 | 文档级查询 | 仅需键值访问 |
| 一致性要求 | 需要ACID保证 | 可接受最终一致性 | 临时或缓存数据 |
| schema 迭代需求 | 可通过迁移流程管理 | 需要无 schema 灵活性 | 无 schema |
| 扩容模式 | 先垂直扩容,后读副本扩容 | 水平分片扩容 | 内存存储,容量有限 |
| Decision | Guidance |
|---|---|
| Primary keys | UUIDs for distributed systems, auto-increment for single-node |
| Column types | Use the most specific type ( |
| Nullability | Default NOT NULL; allow NULL only when absence is meaningful |
| Defaults | Set sensible defaults ( |
| Constraints | Add CHECK, UNIQUE, and FK constraints at the schema level |
| Naming | |
| 决策项 | 指导原则 |
|---|---|
| 主键 | 分布式系统用UUID,单节点系统用自增ID |
| 列类型 | 使用最精准的类型(日期用 |
| 可空性 | 默认设为NOT NULL;仅当值不存在有明确业务含义时才允许NULL |
| 默认值 | 设置合理的默认值( |
| 约束 | 在schema层添加CHECK、UNIQUE和外键约束 |
| 命名规范 | 用 |
| Normal Form | Rule | Violation Example | Fix |
|---|---|---|---|
| 1NF | Atomic values, no repeating groups | | Separate |
| 2NF | All non-key columns depend on entire PK | | Move to |
| 3NF | No transitive dependencies | | Separate |
| 范式 | 规则 | 违反示例 | 修复方案 |
|---|---|---|---|
| 1NF | 原子值,无重复组 | | 拆分出单独的 |
| 2NF | 所有非键列完全依赖主键 | 复合主键的 | 移到 |
| 3NF | 无传递依赖 | | 拆分出单独的 |
| Scenario | Pattern | When to Apply |
|---|---|---|
| Read-heavy dashboards | Materialized views or summary tables | Measured slow query |
| Frequently joined data | Embed as JSONB column | Join is >80% of query time |
| Reporting / analytics | Separate denormalized reporting tables | OLAP workload |
| Caching layer | Computed columns refreshed on write | High-frequency reads |
| 场景 | 模式 | 适用时机 |
|---|---|---|
| 读密集的看板 | 物化视图或汇总表 | 可量化的慢查询场景 |
| 频繁关联的数据 | 嵌入为JSONB列 | 关联耗时占查询总耗时80%以上 |
| 报表/分析场景 | 单独的反规范化报表表 | OLAP工作负载 |
| 缓存层 | 写入时刷新的计算列 | 高频读场景 |
| Relationship | Implementation | Index Needed |
|---|---|---|
| One-to-One | FK with UNIQUE constraint on child | On FK column |
| One-to-Many | FK on the "many" side | On FK column |
| Many-to-Many | Junction/join table with composite PK | On both FK columns |
| Polymorphic | Separate FK columns with CHECK constraint (preferred) or type+id pattern | On type+id or each FK |
| Self-referential (trees) | | On parent_id or path |
| 关系类型 | 实现方式 | 需要的索引 |
|---|---|---|
| 一对一 | 子表外键加UNIQUE约束 | 外键列建索引 |
| 一对多 | "多"的一侧加外键 | 外键列建索引 |
| 多对多 | 带复合主键的关联表/连接表 | 两个外键列都建索引 |
| 多态关联 | 优先用带CHECK约束的独立外键列,或类型+ID模式 | 类型+ID列或每个外键列建索引 |
| 自引用(树结构) | 同表 | parent_id或路径列建索引 |
| Index Type | Best For | Example |
|---|---|---|
| B-tree (default) | Equality and range queries | |
| GIN | Full-text search, JSONB, arrays | |
| Partial | Subset of rows matching condition | |
| Covering (INCLUDE) | Index-only scans avoiding table lookup | |
| Composite | Multi-column queries | |
| 索引类型 | 适用场景 | 示例 |
|---|---|---|
| B-tree(默认) | 等值和范围查询 | |
| GIN | 全文搜索、JSONB、数组 | |
| Partial(部分索引) | 匹配条件的行子集 | |
| Covering(覆盖索引,用INCLUDE) | 仅索引扫描,避免回表 | |
| Composite(联合索引) | 多列查询 | |
| Position | Column Type | Reason |
|---|---|---|
| First | High-cardinality equality columns | Most selective filter first |
| Middle | Additional equality columns | Further narrows results |
| Last | Range columns (dates, numbers) | Range scan on remaining rows |
(A, B, C)AA+BA+B+CBC| 位置 | 列类型 | 原因 |
|---|---|---|
| 首位 | 高基数等值列 | 优先用最高筛选性的过滤条件 |
| 中间 | 其他等值列 | 进一步缩小结果范围 |
| 末位 | 范围列(日期、数字) | 对剩余行做范围扫描 |
(A, B, C)AA+BA+B+CBC| Signal in EXPLAIN ANALYZE | Problem | Fix |
|---|---|---|
| Seq Scan on large table | Missing index | Add appropriate index |
| Nested Loop with large outer table | Inefficient join | Add index or restructure query |
| High actual vs estimated rows | Stale statistics | Run |
| Hash Join high memory | | Tune |
| EXPLAIN ANALYZE中的信号 | 问题 | 修复方案 |
|---|---|---|
| 大表上的顺序扫描 | 缺失索引 | 添加合适的索引 |
| 大外表的嵌套循环连接 | 关联效率低 | 添加索引或重构查询 |
| 实际行数与预估行数差异大 | 统计信息过时 | 对表执行 |
| 哈希连接内存占用过高 | | 调优 |
-- N+1 problem (bad):
SELECT * FROM users;
-- Then for EACH user: SELECT * FROM orders WHERE user_id = ?;
-- Fixed with join:
SELECT u.*, o.* FROM users u LEFT JOIN orders o ON o.user_id = u.id;
-- Fixed with batch load:
SELECT * FROM orders WHERE user_id = ANY($1);-- N+1问题(错误写法):
SELECT * FROM users;
-- 然后对每个用户执行: SELECT * FROM orders WHERE user_id = ?;
-- 用关联查询修复:
SELECT u.*, o.* FROM users u LEFT JOIN orders o ON o.user_id = u.id;
-- 用批量加载修复:
SELECT * FROM orders WHERE user_id = ANY($1);| Rule | Rationale |
|---|---|
| Every migration has a corresponding rollback | Safe to revert |
| Test rollback in staging before production | Verify reversibility |
| Data-destructive rollbacks need explicit approval | Prevent accidental data loss |
| Keep migration files immutable once applied | Reproducible state |
| Backfill large tables in batches (1000 rows) | Avoid table locks |
| 规则 | 原因 |
|---|---|
| 每次迁移都要有对应的回滚脚本 | 可安全回退 |
| 生产环境执行前先在测试环境测试回滚 | 验证可回滚性 |
| 会导致数据丢失的回滚需要明确审批 | 避免意外数据丢失 |
| 已执行的迁移文件保持不可变 | 保证环境状态可复现 |
| 大表数据分批回填(每次1000行) | 避免锁表 |
-- Backfill in chunks of 1000
UPDATE users SET display_name = username
WHERE display_name IS NULL
AND id IN (SELECT id FROM users WHERE display_name IS NULL LIMIT 1000);-- 每次回填1000条
UPDATE users SET display_name = username
WHERE display_name IS NULL
AND id IN (SELECT id FROM users WHERE display_name IS NULL LIMIT 1000);| Change Type | Safe Approach | Dangerous Approach |
|---|---|---|
| Add column | Add nullable or with default | Add NOT NULL without default |
| Remove column | Expand-contract (two deploys) | Drop column directly |
| Rename column | Add new, copy data, drop old | ALTER RENAME (breaks queries) |
| Add index | | |
| Change column type | Add new column, migrate data | |
| 变更类型 | 安全方案 | 危险方案 |
|---|---|---|
| 添加列 | 添加可空列或带默认值的列 | 添加不带默认值的NOT NULL列 |
| 删除列 | 扩缩模式(两次部署) | 直接删除列 |
| 重命名列 | 添加新列、同步数据、删除旧列 | ALTER RENAME(会破坏现有查询) |
| 添加索引 | | |
| 修改列类型 | 添加新列、迁移数据 | |
docs/database/docs(db): add schema design for <feature>docs/database/docs(db): add schema design for <feature>| User Intent | Next Skill | Rationale |
|---|---|---|
| "Create the migrations" | | Plan migration implementation |
| "Write specs for this" | | Behavioral specs for data operations |
| "Implement the schema" | | TDD with migration tests |
| "Just save the design" | None | Schema design is the deliverable |
| "Review for performance" | | Analyze query patterns |
| 用户意图 | 下一个使用的技能 | 原因 |
|---|---|---|
| "创建迁移脚本" | | 规划迁移实现方案 |
| "编写相关规格说明" | | 编写数据操作的行为规格 |
| "实现这个schema" | | 基于TDD编写迁移测试 |
| "仅保存设计即可" | 无 | schema设计本身就是交付物 |
| "做性能评审" | | 分析查询模式 |
| ORM | Language | Strength | Watch Out For |
|---|---|---|---|
| Prisma | TypeScript | Type-safe schema, migrations | N+1 in nested queries, limited raw SQL |
| Drizzle | TypeScript | SQL-like API, lightweight | Newer ecosystem, fewer guides |
| SQLAlchemy | Python | Mature, flexible, raw SQL support | Complex session management |
| GORM | Go | Convention-based, auto-migrate | Silent failures, implicit behavior |
| ORM | 适用语言 | 优势 | 注意事项 |
|---|---|---|---|
| Prisma | TypeScript | 类型安全的schema、迁移能力 | 嵌套查询容易出现N+1问题,原生SQL支持有限 |
| Drizzle | TypeScript | 类SQL API、轻量 | 生态较新,相关教程较少 |
| SQLAlchemy | Python | 成熟、灵活,支持原生SQL | 会话管理复杂 |
| GORM | Go | 约定优于配置,支持自动迁移 | 容易出现静默失败,隐式行为多 |
connections = (CPU cores * 2) + disk spindlesconnections = (CPU核心数 * 2) + 磁盘主轴数| Mistake | Why It Is Wrong | What To Do Instead |
|---|---|---|
| No foreign key constraints | Orphaned data, broken relationships | Always define FK constraints |
| VARCHAR for everything | Loses type safety, wastes storage | Use specific types (timestamptz, int, uuid) |
| No indexes on FK columns | Slow joins on related tables | Index every FK column |
| Premature denormalization | Complexity without measured benefit | Start normalized, denormalize with evidence |
| Dropping columns directly | Breaks running application code | Use expand-contract pattern |
| Locks table during index creation | Always use |
| Auto-sync schema in production | Unpredictable destructive changes | Use explicit migration files |
| No rollback plan for migrations | Cannot recover from failed deploy | Write down migration for every up migration |
| Nullable columns everywhere | Loses data integrity guarantees | Default NOT NULL, allow NULL intentionally |
| 错误 | 危害 | 替代方案 |
|---|---|---|
| 无外键约束 | 产生孤儿数据、关系断裂 | 始终定义外键约束 |
| 所有列都用VARCHAR | 丢失类型安全,浪费存储空间 | 使用专用类型(timestamptz、int、uuid) |
| 外键列无索引 | 关联表查询慢 | 每个外键列都建索引 |
| 过早反规范化 | 引入不必要复杂度,无实际收益 | 从规范化设计开始,有性能证据再反规范化 |
| 直接删除列 | 破坏线上运行的应用代码 | 使用扩缩模式 |
| 不用CONCURRENTLY创建索引 | 创建索引期间锁表 | 生产环境始终用 |
| 生产环境开启schema自动同步 | 产生不可预测的破坏性变更 | 使用显式的迁移文件 |
| 迁移无回滚计划 | 部署失败后无法恢复 | 每个升级迁移都对应写回滚迁移 |
| 所有列都允许为NULL | 丢失数据完整性保证 | 默认设为NOT NULL,仅必要时允许NULL |
mcp__context7__resolve-library-idmcp__context7__query-docsprismatypeormknexmcp__context7__resolve-library-idmcp__context7__query-docsprismatypeormknex| Skill | Relationship |
|---|---|
| Upstream: API resources map to database entities |
| Upstream: specs define data persistence requirements |
| Downstream: schema design informs implementation plan |
| Downstream: migration tests written before migration code |
| Downstream: query optimization after schema is live |
| Upstream: reverse-engineer existing schema behavior |
| Parallel: backend specialist for ORM and query patterns |
| 技能 | 关系 |
|---|---|
| 上游:API资源与数据库实体对应 |
| 上游:规格说明定义数据持久化要求 |
| 下游:schema设计为实现计划提供输入 |
| 下游:迁移代码编写前先写迁移测试 |
| 下游:schema上线后做查询优化 |
| 上游:反向推导现有schema的行为 |
| 并行:后端专家提供ORM和查询模式相关支持 |