This commit is contained in:
1 parent
c1541b80df
commit
b4c7908029
97 files changed
+12403
-154
No files matched your search
@@ -0,0 +1,46 @@
|
||||
---
|
||||
|
||||
## references/database.md
|
||||
|
||||
```markdown
|
||||
# 数据库设计规范
|
||||
|
||||
## 通用字段
|
||||
|
||||
- `id`:主键,cuid / uuid / 自增
|
||||
- `createdAt`:创建时间
|
||||
- `updatedAt`:更新时间
|
||||
- `deletedAt`:软删除(可选)
|
||||
|
||||
## 建模
|
||||
|
||||
- 先画实体关系,再写 schema
|
||||
- 一对多用外键
|
||||
- 多对多用关联表
|
||||
- 避免过宽表,必要时拆表
|
||||
|
||||
## 索引
|
||||
|
||||
- 外键加索引
|
||||
- 高频查询字段加索引
|
||||
- 复合索引注意顺序
|
||||
- 唯一约束用 unique
|
||||
|
||||
## 迁移
|
||||
|
||||
- 每次 schema 变更生成迁移文件
|
||||
- 迁移必须可回滚
|
||||
- 生产环境先备份
|
||||
|
||||
## 事务
|
||||
|
||||
- 多表写入用事务
|
||||
- 保持事务短小
|
||||
- 避免在事务里做网络请求
|
||||
|
||||
## 查询
|
||||
|
||||
- 避免 N+1
|
||||
- 列表接口必须分页
|
||||
- 大字段按需 select
|
||||
```
|
||||
Reference in new issue
Block a user