database-schema-designer by softaworks/agent-toolkit
npx skills add https://github.com/softaworks/agent-toolkit --skill database-schema-designer内置最佳实践,设计可用于生产环境的数据库模式。
只需描述您的数据模型:
design a schema for an e-commerce platform with users, products, orders
您将获得一个完整的 SQL 模式,例如:
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE orders (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id),
total DECIMAL(10,2) NOT NULL,
INDEX idx_orders_user (user_id)
);
您的请求中应包含:
| 触发词 | 示例 |
|---|---|
design schema |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| "design a schema for user authentication" |
database design | "database design for multi-tenant SaaS" |
create tables | "create tables for a blog system" |
schema for | "schema for inventory management" |
model data | "model data for real-time analytics" |
I need a database | "I need a database for tracking orders" |
design NoSQL | "design NoSQL schema for product catalog" |
| 术语 | 定义 |
|---|---|
| 规范化 | 组织数据以减少冗余(1NF → 2NF → 3NF) |
| 第三范式 | 第三范式 - 列之间没有传递依赖 |
| OLTP | 联机事务处理 - 写入密集,需要规范化 |
| OLAP | 联机分析处理 - 读取密集,受益于反规范化 |
| 外键 | 引用另一个表主键的列 |
| 索引 | 加速查询的数据结构(以降低写入速度为代价) |
| 访问模式 | 您的应用程序如何读写数据(查询、连接、过滤) |
| 反规范化 | 有意复制数据以加速读取 |
| 任务 | 方法 | 关键考虑因素 |
|---|---|---|
| 新架构 | 首先规范化至 3NF | 领域建模优先于 UI |
| SQL 与 NoSQL | 由访问模式决定 | 读写比例很重要 |
| 主键 | INT 或 UUID | 分布式系统使用 UUID |
| 外键 | 始终约束 | ON DELETE 策略至关重要 |
| 索引 | 外键 + WHERE 子句列 | 列顺序很重要 |
| 迁移 | 始终可逆 | 首先确保向后兼容 |
您的数据需求
|
v
+-----------------------------------------------------+
| 阶段 1: 分析 |
| * 识别实体和关系 |
| * 确定访问模式(读取密集 vs 写入密集) |
| * 根据需求选择 SQL 或 NoSQL |
+-----------------------------------------------------+
|
v
+-----------------------------------------------------+
| 阶段 2: 设计 |
| * 规范化至 3NF(SQL)或嵌入/引用(NoSQL) |
| * 定义主键和外键 |
| * 选择合适的数据类型 |
| * 添加约束(UNIQUE、CHECK、NOT NULL) |
+-----------------------------------------------------+
|
v
+-----------------------------------------------------+
| 阶段 3: 优化 |
| * 规划索引策略 |
| * 为读取密集查询考虑反规范化 |
| * 添加时间戳(created_at、updated_at) |
+-----------------------------------------------------+
|
v
+-----------------------------------------------------+
| 阶段 4: 迁移 |
| * 生成迁移脚本(up + down) |
| * 确保向后兼容性 |
| * 规划零停机部署 |
+-----------------------------------------------------+
|
v
可用于生产环境的模式
| 命令 | 使用时机 | 操作 |
|---|---|---|
design schema for {domain} | 从头开始 | 完整模式生成 |
normalize {table} | 修复现有表 | 应用规范化规则 |
add indexes for {table} | 性能问题 | 生成索引策略 |
migration for {change} | 模式演进 | 创建可逆迁移 |
review schema | 代码审查 | 审计现有模式 |
工作流: 从 design schema 开始 → 使用 normalize 迭代 → 使用 add indexes 优化 → 使用 migration 演进
| 原则 | 原因 | 实现 |
|---|---|---|
| 领域建模 | UI 会变,领域不变 | 实体名称反映业务概念 |
| 数据完整性优先 | 数据损坏修复成本高 | 在数据库级别设置约束 |
| 为访问模式优化 | 无法同时优化两者 | OLTP:规范化,OLAP:反规范化 |
| 为扩展规划 | 事后改造很痛苦 | 索引策略 + 分区计划 |
| 避免 | 原因 | 替代方案 |
|---|---|---|
| 到处使用 VARCHAR(255) | 浪费存储空间,隐藏意图 | 根据字段适当调整大小 |
| 使用 FLOAT 存储金额 | 舍入误差 | 使用 DECIMAL(10,2) |
| 缺少外键约束 | 产生孤立数据 | 始终定义外键 |
| 外键上没有索引 | 连接操作慢 | 为每个外键建立索引 |
| 将日期存储为字符串 | 无法比较/排序 | 使用 DATE、TIMESTAMP 类型 |
| 查询中使用 SELECT * | 获取不必要的数据 | 明确指定列列表 |
| 不可逆的迁移 | 无法回滚 | 始终编写 DOWN 迁移 |
| 添加 NOT NULL 而不设置默认值 | 破坏现有行 | 先添加可为空列,回填数据,再添加约束 |
设计模式后检查:
| 范式 | 规则 | 违反示例 |
|---|---|---|
| 第一范式 | 原子值,无重复组 | product_ids = '1,2,3' |
| 第二范式 | 第一范式 + 无部分依赖 | 订单明细表中的客户姓名 |
| 第三范式 | 第二范式 + 无传递依赖 | 从邮政编码派生的国家 |
-- 错误:列中包含多个值
CREATE TABLE orders (
id INT PRIMARY KEY,
product_ids VARCHAR(255) -- '101,102,103'
);
-- 正确:为明细项创建单独的表
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT
);
CREATE TABLE order_items (
id INT PRIMARY KEY,
order_id INT REFERENCES orders(id),
product_id INT
);
-- 错误:customer_name 仅依赖于 customer_id
CREATE TABLE order_items (
order_id INT,
product_id INT,
customer_name VARCHAR(100), -- 部分依赖!
PRIMARY KEY (order_id, product_id)
);
-- 正确:客户数据放在单独的表中
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100)
);
-- 错误:country 依赖于 postal_code
CREATE TABLE customers (
id INT PRIMARY KEY,
postal_code VARCHAR(10),
country VARCHAR(50) -- 传递依赖!
);
-- 正确:单独的邮政编码表
CREATE TABLE postal_codes (
code VARCHAR(10) PRIMARY KEY,
country VARCHAR(50)
);
| 场景 | 反规范化策略 |
|---|---|
| 读取密集的报告 | 预计算聚合 |
| 昂贵的连接操作 | 缓存派生列 |
| 分析仪表板 | 物化视图 |
-- 为性能而反规范化
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
total_amount DECIMAL(10,2), -- 计算得出
item_count INT -- 计算得出
);
| 类型 | 使用场景 | 示例 |
|---|---|---|
| CHAR(n) | 固定长度 | 州代码、ISO 日期 |
| VARCHAR(n) | 可变长度 | 姓名、电子邮件 |
| TEXT | 长内容 | 文章、描述 |
-- 良好的大小设置
email VARCHAR(255)
phone VARCHAR(20)
country_code CHAR(2)
| 类型 | 范围 | 使用场景 |
|---|---|---|
| TINYINT | -128 到 127 | 年龄、状态码 |
| SMALLINT | -32K 到 32K | 数量 |
| INT | -2.1B 到 2.1B | ID、计数 |
| BIGINT | 非常大 | 大型 ID、时间戳 |
| DECIMAL(p,s) | 精确精度 | 金额 |
| FLOAT/DOUBLE | 近似值 | 科学数据 |
-- 金额始终使用 DECIMAL
price DECIMAL(10, 2) -- $99,999,999.99
-- 金额切勿使用 FLOAT
price FLOAT -- 舍入误差!
DATE -- 2025-10-31
TIME -- 14:30:00
DATETIME -- 2025-10-31 14:30:00
TIMESTAMP -- 自动时区转换
-- 始终以 UTC 存储
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
-- PostgreSQL
is_active BOOLEAN DEFAULT TRUE
-- MySQL
is_active TINYINT(1) DEFAULT 1
| 始终索引 | 原因 |
|---|---|
| 外键 | 加速连接操作 |
| WHERE 子句列 | 加速过滤 |
| ORDER BY 列 | 加速排序 |
| 唯一约束 | 强制唯一性 |
-- 外键索引
CREATE INDEX idx_orders_customer ON orders(customer_id);
-- 查询模式索引
CREATE INDEX idx_orders_status_date ON orders(status, created_at);
| 类型 | 最适合 | 示例 |
|---|---|---|
| B-Tree | 范围、相等 | price > 100 |
| Hash | 仅精确匹配 | email = 'x@y.com' |
| 全文 | 文本搜索 | MATCH AGAINST |
| 部分 | 行的子集 | WHERE is_active = true |
CREATE INDEX idx_customer_status ON orders(customer_id, status);
-- 使用索引(customer_id 在前)
SELECT * FROM orders WHERE customer_id = 123;
SELECT * FROM orders WHERE customer_id = 123 AND status = 'pending';
-- 不使用索引(单独使用 status)
SELECT * FROM orders WHERE status = 'pending';
规则: 选择性最高的列在前,或最常单独查询的列在前。
| 陷阱 | 问题 | 解决方案 |
|---|---|---|
| 过度索引 | 写入速度慢 | 仅索引被查询的列 |
| 错误的列顺序 | 索引未被使用 | 匹配查询模式 |
| 缺少外键索引 | 连接操作慢 | 始终为外键建立索引 |
-- 自增(简单)
id INT AUTO_INCREMENT PRIMARY KEY
-- UUID(分布式系统)
id CHAR(36) PRIMARY KEY DEFAULT (UUID())
-- 复合(连接表)
PRIMARY KEY (student_id, course_id)
FOREIGN KEY (customer_id) REFERENCES customers(id)
ON DELETE CASCADE -- 随父级删除子级
ON DELETE RESTRICT -- 如果被引用则阻止删除
ON DELETE SET NULL -- 父级删除时设为 NULL
ON UPDATE CASCADE -- 父级更改时更新子级
| 策略 | 使用时机 |
|---|---|
| CASCADE | 依赖数据(订单明细) |
| RESTRICT | 重要引用(防止意外) |
| SET NULL | 可选关系 |
-- 唯一
email VARCHAR(255) UNIQUE NOT NULL
-- 复合唯一
UNIQUE (student_id, course_id)
-- 检查
price DECIMAL(10,2) CHECK (price >= 0)
discount INT CHECK (discount BETWEEN 0 AND 100)
-- 非空
name VARCHAR(100) NOT NULL
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT NOT NULL REFERENCES customers(id)
);
CREATE TABLE order_items (
id INT PRIMARY KEY,
order_id INT NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
product_id INT NOT NULL,
quantity INT NOT NULL
);
-- 连接表
CREATE TABLE enrollments (
student_id INT REFERENCES students(id) ON DELETE CASCADE,
course_id INT REFERENCES courses(id) ON DELETE CASCADE,
enrolled_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (student_id, course_id)
);
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
manager_id INT REFERENCES employees(id)
);
-- 方法 1:单独的外键(完整性更强)
CREATE TABLE comments (
id INT PRIMARY KEY,
content TEXT NOT NULL,
post_id INT REFERENCES posts(id),
photo_id INT REFERENCES photos(id),
CHECK (
(post_id IS NOT NULL AND photo_id IS NULL) OR
(post_id IS NULL AND photo_id IS NOT NULL)
)
);
-- 方法 2:类型 + ID(灵活,完整性较弱)
CREATE TABLE comments (
id INT PRIMARY KEY,
content TEXT NOT NULL,
commentable_type VARCHAR(50) NOT NULL,
commentable_id INT NOT NULL
);
| 因素 | 嵌入 | 引用 |
|---|---|---|
| 访问模式 | 一起读取 | 分开读取 |
| 关系 | 1:少 | 1:多 |
| 文档大小 | 小 | 接近 16MB |
| 更新频率 | 很少 | 频繁 |
{
"_id": "order_123",
"customer": {
"id": "cust_456",
"name": "Jane Smith",
"email": "jane@example.com"
},
"items": [
{ "product_id": "prod_789", "quantity": 2, "price": 29.99 }
],
"total": 109.97
}
{
"_id": "order_123",
"customer_id": "cust_456",
"item_ids": ["item_1", "item_2"],
"total": 109.97
}
// 单字段
db.users.createIndex({ email: 1 }, { unique: true });
// 复合
db.orders.createIndex({ customer_id: 1, created_at: -1 });
// 文本搜索
db.articles.createIndex({ title: "text", content: "text" });
// 地理空间
db.stores.createIndex({ location: "2dsphere" });
| 实践 | 原因 |
|---|---|
| 始终可逆 | 需要回滚 |
| 向后兼容 | 零停机部署 |
| 先模式后数据 | 分离关注点 |
| 在测试环境测试 | 及早发现问题 |
-- 步骤 1:添加可为空列
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
-- 步骤 2:部署写入新列的代码
-- 步骤 3:回填现有行
UPDATE users SET phone = '' WHERE phone IS NULL;
-- 步骤 4:设为必需(如果需要)
ALTER TABLE users MODIFY phone VARCHAR(20) NOT NULL;
-- 步骤 1:添加新列
ALTER TABLE users ADD COLUMN email_address VARCHAR(255);
-- 步骤 2:复制数据
UPDATE users SET email_address = email;
-- 步骤 3:部署从新列读取的代码
-- 步骤 4:部署写入新列的代码
-- 步骤 5:删除旧列
ALTER TABLE users DROP COLUMN email;
-- 迁移:YYYYMMDDHHMMSS_description.sql
-- UP
BEGIN;
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
CREATE INDEX idx_users_phone ON users(phone);
COMMIT;
-- DOWN
BEGIN;
DROP INDEX idx_users_phone ON users;
ALTER TABLE users DROP COLUMN phone;
COMMIT;
EXPLAIN SELECT * FROM orders
WHERE customer_id = 123 AND status = 'pending';
| 查找 | 含义 |
|---|---|
| type: ALL | 全表扫描(不好) |
| type: ref | 使用了索引(好) |
| key: NULL | 未使用索引 |
| rows: high | 扫描了大量行 |
# 错误:N+1 查询
orders = db.query("SELECT * FROM orders")
for order in orders:
customer = db.query(f"SELECT * FROM customers WHERE id = {order.customer_id}")
# 正确:单次连接
results = db.query("""
SELECT orders.*, customers.name
FROM orders
JOIN customers ON orders.customer_id = customers.id
""")
| 技术 | 使用时机 |
|---|---|
| 添加索引 | WHERE/ORDER BY 慢 |
| 反规范化 | 连接操作昂贵 |
| 分页 | 结果集大 |
| 缓存 | 重复查询 |
| 读取副本 | 读取密集负载 |
| 分区 | 非常大的表 |
每周安装量
564
仓库
GitHub 星标
1.2K
首次出现
2026年1月20日
安全审计
安装于
codex405
claude-code405
gemini-cli405
cursor404
opencode401
cline398
Design production-ready database schemas with best practices built-in.
Just describe your data model:
design a schema for an e-commerce platform with users, products, orders
You'll get a complete SQL schema like:
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE orders (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id),
total DECIMAL(10,2) NOT NULL,
INDEX idx_orders_user (user_id)
);
What to include in your request:
| Trigger | Example |
|---|---|
design schema | "design a schema for user authentication" |
database design | "database design for multi-tenant SaaS" |
create tables | "create tables for a blog system" |
schema for | "schema for inventory management" |
model data | "model data for real-time analytics" |
I need a database | "I need a database for tracking orders" |
design NoSQL | "design NoSQL schema for product catalog" |
| Term | Definition |
|---|---|
| Normalization | Organizing data to reduce redundancy (1NF → 2NF → 3NF) |
| 3NF | Third Normal Form - no transitive dependencies between columns |
| OLTP | Online Transaction Processing - write-heavy, needs normalization |
| OLAP | Online Analytical Processing - read-heavy, benefits from denormalization |
| Foreign Key (FK) | Column that references another table's primary key |
| Index | Data structure that speeds up queries (at cost of slower writes) |
| Access Pattern | How your app reads/writes data (queries, joins, filters) |
| Denormalization | Intentionally duplicating data to speed up reads |
| Task | Approach | Key Consideration |
|---|---|---|
| New schema | Normalize to 3NF first | Domain modeling over UI |
| SQL vs NoSQL | Access patterns decide | Read/write ratio matters |
| Primary keys | INT or UUID | UUID for distributed systems |
| Foreign keys | Always constrain | ON DELETE strategy critical |
| Indexes | FKs + WHERE columns | Column order matters |
| Migrations | Always reversible | Backward compatible first |
Your Data Requirements
|
v
+-----------------------------------------------------+
| Phase 1: ANALYSIS |
| * Identify entities and relationships |
| * Determine access patterns (read vs write heavy) |
| * Choose SQL or NoSQL based on requirements |
+-----------------------------------------------------+
|
v
+-----------------------------------------------------+
| Phase 2: DESIGN |
| * Normalize to 3NF (SQL) or embed/reference (NoSQL) |
| * Define primary keys and foreign keys |
| * Choose appropriate data types |
| * Add constraints (UNIQUE, CHECK, NOT NULL) |
+-----------------------------------------------------+
|
v
+-----------------------------------------------------+
| Phase 3: OPTIMIZE |
| * Plan indexing strategy |
| * Consider denormalization for read-heavy queries |
| * Add timestamps (created_at, updated_at) |
+-----------------------------------------------------+
|
v
+-----------------------------------------------------+
| Phase 4: MIGRATE |
| * Generate migration scripts (up + down) |
| * Ensure backward compatibility |
| * Plan zero-downtime deployment |
+-----------------------------------------------------+
|
v
Production-Ready Schema
| Command | When to Use | Action |
|---|---|---|
design schema for {domain} | Starting fresh | Full schema generation |
normalize {table} | Fixing existing table | Apply normalization rules |
add indexes for {table} | Performance issues | Generate index strategy |
migration for {change} | Schema evolution | Create reversible migration |
review schema |
Workflow: Start with design schema → iterate with normalize → optimize with add indexes → evolve with migration
| Principle | WHY | Implementation |
|---|---|---|
| Model the Domain | UI changes, domain doesn't | Entity names reflect business concepts |
| Data Integrity First | Corruption is costly to fix | Constraints at database level |
| Optimize for Access Pattern | Can't optimize for both | OLTP: normalized, OLAP: denormalized |
| Plan for Scale | Retrofitting is painful | Index strategy + partitioning plan |
| Avoid | Why | Instead |
|---|---|---|
| VARCHAR(255) everywhere | Wastes storage, hides intent | Size appropriately per field |
| FLOAT for money | Rounding errors | DECIMAL(10,2) |
| Missing FK constraints | Orphaned data | Always define foreign keys |
| No indexes on FKs | Slow JOINs | Index every foreign key |
| Storing dates as strings | Can't compare/sort | DATE, TIMESTAMP types |
| SELECT * in queries | Fetches unnecessary data | Explicit column lists |
| Non-reversible migrations | Can't rollback | Always write DOWN migration |
| Adding NOT NULL without default | Breaks existing rows | Add nullable, backfill, then constrain |
After designing a schema:
| Form | Rule | Violation Example |
|---|---|---|
| 1NF | Atomic values, no repeating groups | product_ids = '1,2,3' |
| 2NF | 1NF + no partial dependencies | customer_name in order_items |
| 3NF | 2NF + no transitive dependencies | country derived from postal_code |
-- BAD: Multiple values in column
CREATE TABLE orders (
id INT PRIMARY KEY,
product_ids VARCHAR(255) -- '101,102,103'
);
-- GOOD: Separate table for items
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT
);
CREATE TABLE order_items (
id INT PRIMARY KEY,
order_id INT REFERENCES orders(id),
product_id INT
);
-- BAD: customer_name depends only on customer_id
CREATE TABLE order_items (
order_id INT,
product_id INT,
customer_name VARCHAR(100), -- Partial dependency!
PRIMARY KEY (order_id, product_id)
);
-- GOOD: Customer data in separate table
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100)
);
-- BAD: country depends on postal_code
CREATE TABLE customers (
id INT PRIMARY KEY,
postal_code VARCHAR(10),
country VARCHAR(50) -- Transitive dependency!
);
-- GOOD: Separate postal_codes table
CREATE TABLE postal_codes (
code VARCHAR(10) PRIMARY KEY,
country VARCHAR(50)
);
| Scenario | Denormalization Strategy |
|---|---|
| Read-heavy reporting | Pre-calculated aggregates |
| Expensive JOINs | Cached derived columns |
| Analytics dashboards | Materialized views |
-- Denormalized for performance
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
total_amount DECIMAL(10,2), -- Calculated
item_count INT -- Calculated
);
| Type | Use Case | Example |
|---|---|---|
| CHAR(n) | Fixed length | State codes, ISO dates |
| VARCHAR(n) | Variable length | Names, emails |
| TEXT | Long content | Articles, descriptions |
-- Good sizing
email VARCHAR(255)
phone VARCHAR(20)
country_code CHAR(2)
| Type | Range | Use Case |
|---|---|---|
| TINYINT | -128 to 127 | Age, status codes |
| SMALLINT | -32K to 32K | Quantities |
| INT | -2.1B to 2.1B | IDs, counts |
| BIGINT | Very large | Large IDs, timestamps |
| DECIMAL(p,s) | Exact precision | Money |
| FLOAT/DOUBLE | Approximate | Scientific data |
-- ALWAYS use DECIMAL for money
price DECIMAL(10, 2) -- $99,999,999.99
-- NEVER use FLOAT for money
price FLOAT -- Rounding errors!
DATE -- 2025-10-31
TIME -- 14:30:00
DATETIME -- 2025-10-31 14:30:00
TIMESTAMP -- Auto timezone conversion
-- Always store in UTC
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
-- PostgreSQL
is_active BOOLEAN DEFAULT TRUE
-- MySQL
is_active TINYINT(1) DEFAULT 1
| Always Index | Reason |
|---|---|
| Foreign keys | Speed up JOINs |
| WHERE clause columns | Speed up filtering |
| ORDER BY columns | Speed up sorting |
| Unique constraints | Enforced uniqueness |
-- Foreign key index
CREATE INDEX idx_orders_customer ON orders(customer_id);
-- Query pattern index
CREATE INDEX idx_orders_status_date ON orders(status, created_at);
| Type | Best For | Example |
|---|---|---|
| B-Tree | Ranges, equality | price > 100 |
| Hash | Exact matches only | email = 'x@y.com' |
| Full-text | Text search | MATCH AGAINST |
| Partial | Subset of rows | WHERE is_active = true |
CREATE INDEX idx_customer_status ON orders(customer_id, status);
-- Uses index (customer_id first)
SELECT * FROM orders WHERE customer_id = 123;
SELECT * FROM orders WHERE customer_id = 123 AND status = 'pending';
-- Does NOT use index (status alone)
SELECT * FROM orders WHERE status = 'pending';
Rule: Most selective column first, or column most queried alone.
| Pitfall | Problem | Solution |
|---|---|---|
| Over-indexing | Slow writes | Only index what's queried |
| Wrong column order | Unused index | Match query patterns |
| Missing FK indexes | Slow JOINs | Always index FKs |
-- Auto-increment (simple)
id INT AUTO_INCREMENT PRIMARY KEY
-- UUID (distributed systems)
id CHAR(36) PRIMARY KEY DEFAULT (UUID())
-- Composite (junction tables)
PRIMARY KEY (student_id, course_id)
FOREIGN KEY (customer_id) REFERENCES customers(id)
ON DELETE CASCADE -- Delete children with parent
ON DELETE RESTRICT -- Prevent deletion if referenced
ON DELETE SET NULL -- Set to NULL when parent deleted
ON UPDATE CASCADE -- Update children when parent changes
| Strategy | Use When |
|---|---|
| CASCADE | Dependent data (order_items) |
| RESTRICT | Important references (prevent accidents) |
| SET NULL | Optional relationships |
-- Unique
email VARCHAR(255) UNIQUE NOT NULL
-- Composite unique
UNIQUE (student_id, course_id)
-- Check
price DECIMAL(10,2) CHECK (price >= 0)
discount INT CHECK (discount BETWEEN 0 AND 100)
-- Not null
name VARCHAR(100) NOT NULL
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT NOT NULL REFERENCES customers(id)
);
CREATE TABLE order_items (
id INT PRIMARY KEY,
order_id INT NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
product_id INT NOT NULL,
quantity INT NOT NULL
);
-- Junction table
CREATE TABLE enrollments (
student_id INT REFERENCES students(id) ON DELETE CASCADE,
course_id INT REFERENCES courses(id) ON DELETE CASCADE,
enrolled_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (student_id, course_id)
);
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
manager_id INT REFERENCES employees(id)
);
-- Approach 1: Separate FKs (stronger integrity)
CREATE TABLE comments (
id INT PRIMARY KEY,
content TEXT NOT NULL,
post_id INT REFERENCES posts(id),
photo_id INT REFERENCES photos(id),
CHECK (
(post_id IS NOT NULL AND photo_id IS NULL) OR
(post_id IS NULL AND photo_id IS NOT NULL)
)
);
-- Approach 2: Type + ID (flexible, weaker integrity)
CREATE TABLE comments (
id INT PRIMARY KEY,
content TEXT NOT NULL,
commentable_type VARCHAR(50) NOT NULL,
commentable_id INT NOT NULL
);
| Factor | Embed | Reference |
|---|---|---|
| Access pattern | Read together | Read separately |
| Relationship | 1:few | 1:many |
| Document size | Small | Approaching 16MB |
| Update frequency | Rarely | Frequently |
{
"_id": "order_123",
"customer": {
"id": "cust_456",
"name": "Jane Smith",
"email": "jane@example.com"
},
"items": [
{ "product_id": "prod_789", "quantity": 2, "price": 29.99 }
],
"total": 109.97
}
{
"_id": "order_123",
"customer_id": "cust_456",
"item_ids": ["item_1", "item_2"],
"total": 109.97
}
// Single field
db.users.createIndex({ email: 1 }, { unique: true });
// Composite
db.orders.createIndex({ customer_id: 1, created_at: -1 });
// Text search
db.articles.createIndex({ title: "text", content: "text" });
// Geospatial
db.stores.createIndex({ location: "2dsphere" });
| Practice | WHY |
|---|---|
| Always reversible | Need to rollback |
| Backward compatible | Zero-downtime deploys |
| Schema before data | Separate concerns |
| Test on staging | Catch issues early |
-- Step 1: Add nullable column
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
-- Step 2: Deploy code that writes to new column
-- Step 3: Backfill existing rows
UPDATE users SET phone = '' WHERE phone IS NULL;
-- Step 4: Make required (if needed)
ALTER TABLE users MODIFY phone VARCHAR(20) NOT NULL;
-- Step 1: Add new column
ALTER TABLE users ADD COLUMN email_address VARCHAR(255);
-- Step 2: Copy data
UPDATE users SET email_address = email;
-- Step 3: Deploy code reading from new column
-- Step 4: Deploy code writing to new column
-- Step 5: Drop old column
ALTER TABLE users DROP COLUMN email;
-- Migration: YYYYMMDDHHMMSS_description.sql
-- UP
BEGIN;
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
CREATE INDEX idx_users_phone ON users(phone);
COMMIT;
-- DOWN
BEGIN;
DROP INDEX idx_users_phone ON users;
ALTER TABLE users DROP COLUMN phone;
COMMIT;
EXPLAIN SELECT * FROM orders
WHERE customer_id = 123 AND status = 'pending';
| Look For | Meaning |
|---|---|
| type: ALL | Full table scan (bad) |
| type: ref | Index used (good) |
| key: NULL | No index used |
| rows: high | Many rows scanned |
# BAD: N+1 queries
orders = db.query("SELECT * FROM orders")
for order in orders:
customer = db.query(f"SELECT * FROM customers WHERE id = {order.customer_id}")
# GOOD: Single JOIN
results = db.query("""
SELECT orders.*, customers.name
FROM orders
JOIN customers ON orders.customer_id = customers.id
""")
| Technique | When to Use |
|---|---|
| Add indexes | Slow WHERE/ORDER BY |
| Denormalize | Expensive JOINs |
| Pagination | Large result sets |
| Caching | Repeated queries |
| Read replicas | Read-heavy load |
| Partitioning | Very large tables |
Weekly Installs
564
Repository
GitHub Stars
1.2K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex405
claude-code405
gemini-cli405
cursor404
opencode401
cline398
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装
OpenAPI 转 TypeScript 工具 - 自动生成 API 接口与类型守卫
563 周安装
数据库模式设计器 - 内置最佳实践,自动生成生产级SQL/NoSQL数据库架构
564 周安装
Rust Unsafe代码检查器 - 安全使用Unsafe Rust的完整指南与最佳实践
564 周安装
.NET并发编程模式指南:async/await、Channels、Akka.NET选择决策树
565 周安装
韩语语法检查器 - 基于国立国语院标准的拼写、空格、语法、标点错误检测与纠正
565 周安装
技能安全扫描器 - 检测Claude技能安全漏洞,防范提示注入与恶意代码
565 周安装
| Code review |
| Audit existing schema |