You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RuoYi-Vue/openspec/changes/archive/2026-06-28-refactor-databas.../design.md

55 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Design: 数据库优化与迁移方案
## 优化策略
### 表结构优化
- 规范化设计3NF
- 消除冗余字段
- 合理设计外键
- 添加审计字段created_at, updated_at
### 索引优化
- 主键索引
- 唯一索引
- 复合索引(根据查询模式)
- 覆盖索引
### 迁移方案
```
1. 备份现有数据库
2. 创建新表结构
3. 数据转换和迁移
4. 验证数据完整性
5. 切换数据源
6. 保留回滚方案
```
## 数据迁移脚本
```sql
-- 1. 备份
CREATE TABLE sys_user_backup AS SELECT * FROM sys_user;
-- 2. 创建新表
CREATE TABLE sys_user_v2 (
id BIGINT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
-- 其他字段
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- 3. 迁移数据
INSERT INTO sys_user_v2 (id, username, ...)
SELECT user_id, user_name, ... FROM sys_user;
-- 4. 验证
SELECT COUNT(*) FROM sys_user;
SELECT COUNT(*) FROM sys_user_v2;
-- 5. 切换(重命名)
RENAME TABLE sys_user TO sys_user_old, sys_user_v2 TO sys_user;
-- 6. 回滚(如需要)
RENAME TABLE sys_user TO sys_user_v2, sys_user_old TO sys_user;
```