Loading...
Loading...
Compare original and translation side by side
| Script | Purpose |
|---|---|
| Static analysis of SQL queries for performance issues |
| Generate migration file templates from change descriptions |
| Generate schema documentation from introspection queries |
| 脚本 | 用途 |
|---|---|
| 对SQL查询语句进行静态分析以排查性能问题 |
| 根据变更描述生成迁移文件模板 |
| 通过自省查询生成架构文档 |
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rn
FROM employees
) ranked WHERE rn <= 3;SELECT date, amount,
SUM(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM transactions;SELECT curr.id, curr.seq_num, prev.seq_num AS prev_seq
FROM records curr
LEFT JOIN records prev ON prev.seq_num = curr.seq_num - 1
WHERE prev.id IS NULL AND curr.seq_num > 1;INSERT INTO settings (key, value, updated_at)
VALUES ('theme', 'dark', NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at;INSERT INTO settings (key_name, value, updated_at)
VALUES ('theme', 'dark', NOW())
ON DUPLICATE KEY UPDATE value = VALUES(value), updated_at = VALUES(updated_at);See references/query_patterns.md for JOINs, CTEs, window functions, JSON operations, and more.
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rn
FROM employees
) ranked WHERE rn <= 3;SELECT date, amount,
SUM(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM transactions;SELECT curr.id, curr.seq_num, prev.seq_num AS prev_seq
FROM records curr
LEFT JOIN records prev ON prev.seq_num = curr.seq_num - 1
WHERE prev.id IS NULL AND curr.seq_num > 1;INSERT INTO settings (key, value, updated_at)
VALUES ('theme', 'dark', NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at;INSERT INTO settings (key_name, value, updated_at)
VALUES ('theme', 'dark', NOW())
ON DUPLICATE KEY UPDATE value = VALUES(value), updated_at = VALUES(updated_at);更多JOIN、CTE、窗口函数、JSON操作等内容,请查看references/query_patterns.md。
SELECT table_name, column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
ORDER BY table_name, ordinal_position;SELECT tc.table_name, kcu.column_name,
ccu.table_name AS foreign_table, ccu.column_name AS foreign_column
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu ON tc.constraint_name = ccu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY';SELECT table_name, table_rows,
ROUND(data_length / 1024 / 1024, 2) AS data_mb,
ROUND(index_length / 1024 / 1024, 2) AS index_mb
FROM information_schema.tables
WHERE table_schema = DATABASE()
ORDER BY data_length DESC;SELECT name, sql FROM sqlite_master WHERE type = 'table' ORDER BY name;SELECT t.name AS table_name, c.name AS column_name,
ty.name AS data_type, c.max_length, c.is_nullable
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
JOIN sys.types ty ON c.user_type_id = ty.user_type_id
ORDER BY t.name, c.column_id;SELECT table_name, column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
ORDER BY table_name, ordinal_position;SELECT tc.table_name, kcu.column_name,
ccu.table_name AS foreign_table, ccu.column_name AS foreign_column
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu ON tc.constraint_name = ccu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY';SELECT table_name, table_rows,
ROUND(data_length / 1024 / 1024, 2) AS data_mb,
ROUND(index_length / 1024 / 1024, 2) AS index_mb
FROM information_schema.tables
WHERE table_schema = DATABASE()
ORDER BY data_length DESC;SELECT name, sql FROM sqlite_master WHERE type = 'table' ORDER BY name;SELECT t.name AS table_name, c.name AS column_name,
ty.name AS data_type, c.max_length, c.is_nullable
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
JOIN sys.types ty ON c.user_type_id = ty.user_type_id
ORDER BY t.name, c.column_id;scripts/schema_explorer.pypython scripts/schema_explorer.py --dialect postgres --tables all --format md
python scripts/schema_explorer.py --dialect mysql --tables users,orders --format json --jsonscripts/schema_explorer.pypython scripts/schema_explorer.py --dialect postgres --tables all --format md
python scripts/schema_explorer.py --dialect mysql --tables users,orders --format json --jsonWHERE status = 'active'WHERE status = 'active'| Anti-Pattern | Rewrite |
|---|---|
| |
| |
| Correlated subquery in SELECT | LEFT JOIN with aggregation |
| |
| |
| Full-text search index (GIN/FULLTEXT) |
| Application-side random sampling or |
| 反模式 | 重写方案 |
|---|---|
| |
| |
| SELECT中的关联子查询 | 使用LEFT JOIN结合聚合操作 |
含NULL的 | |
无需去重时使用 | |
| 全文搜索索引(GIN/FULLTEXT) |
| 应用端随机采样或 |
includejoinedloadWHERE id IN (...)includejoinedloadWHERE id IN (...)python scripts/query_optimizer.py --query "SELECT * FROM orders WHERE status = 'pending'" --dialect postgres
python scripts/query_optimizer.py --query queries.sql --dialect mysql --jsonSee references/optimization_guide.md for EXPLAIN plan reading, index types, and connection pooling.
python scripts/query_optimizer.py --query "SELECT * FROM orders WHERE status = 'pending'" --dialect postgres
python scripts/query_optimizer.py --query queries.sql --dialect mysql --json更多EXPLAIN计划解读、索引类型、连接池相关内容,请查看references/optimization_guide.md。
-- Up
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
-- Down
ALTER TABLE users DROP COLUMN phone;-- Step 1: Add new column
ALTER TABLE users ADD COLUMN full_name VARCHAR(255);
-- Step 2: Backfill
UPDATE users SET full_name = name;
-- Step 3: Deploy app reading both columns
-- Step 4: Deploy app writing only new column
-- Step 5: Drop old column
ALTER TABLE users DROP COLUMN name;-- Step 1: Add nullable
ALTER TABLE orders ADD COLUMN region VARCHAR(50);
-- Step 2: Backfill with default
UPDATE orders SET region = 'unknown' WHERE region IS NULL;
-- Step 3: Add constraint
ALTER TABLE orders ALTER COLUMN region SET NOT NULL;
ALTER TABLE orders ALTER COLUMN region SET DEFAULT 'unknown';CREATE INDEX CONCURRENTLY idx_orders_status ON orders (status);-- 升级
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
-- 回滚
ALTER TABLE users DROP COLUMN phone;-- 步骤1:添加新列
ALTER TABLE users ADD COLUMN full_name VARCHAR(255);
-- 步骤2:回填数据
UPDATE users SET full_name = name;
-- 步骤3:部署读取两列的应用版本
-- 步骤4:部署仅写入新列的应用版本
-- 步骤5:删除旧列
ALTER TABLE users DROP COLUMN name;-- 步骤1:添加可空列
ALTER TABLE orders ADD COLUMN region VARCHAR(50);
-- 步骤2:使用默认值回填
UPDATE orders SET region = 'unknown' WHERE region IS NULL;
-- 步骤3:添加约束
ALTER TABLE orders ALTER COLUMN region SET NOT NULL;
ALTER TABLE orders ALTER COLUMN region SET DEFAULT 'unknown';CREATE INDEX CONCURRENTLY idx_orders_status ON orders (status);pg_dumppg_dumppython scripts/migration_generator.py --change "add email_verified boolean to users" --dialect postgres --format sql
python scripts/migration_generator.py --change "rename column name to full_name in customers" --dialect mysql --format alembic --jsonpython scripts/migration_generator.py --change "add email_verified boolean to users" --dialect postgres --format sql
python scripts/migration_generator.py --change "rename column name to full_name in customers" --dialect mysql --format alembic --json| Feature | PostgreSQL | MySQL | SQLite | SQL Server |
|---|---|---|---|---|
| UPSERT | | | | |
| Boolean | Native | | | |
| Auto-increment | | | | |
| JSON | | | Text (ext) | |
| Array | Native | Not supported | Not supported | Not supported |
| CTE (recursive) | Full support | 8.0+ | 3.8.3+ | Full support |
| Window functions | Full support | 8.0+ | 3.25.0+ | Full support |
| Full-text search | | | FTS5 extension | Full-text catalog |
| LIMIT/OFFSET | | | | |
| 特性 | PostgreSQL | MySQL | SQLite | SQL Server |
|---|---|---|---|---|
| UPSERT | | | | |
| 布尔类型 | 原生 | | | |
| 自增 | | | | |
| JSON | | | 文本(扩展) | |
| 数组 | 原生 | 不支持 | 不支持 | 不支持 |
| 递归CTE | 完全支持 | 8.0+ | 3.8.3+ | 完全支持 |
| 窗口函数 | 完全支持 | 8.0+ | 3.25.0+ | 完全支持 |
| 全文搜索 | | | FTS5扩展 | 全文目录 |
| LIMIT/OFFSET | | | | |
information_schema'YYYY-MM-DD'information_schema'YYYY-MM-DD'model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
createdAt DateTime @default(now())
}
model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id])
authorId Int
}npx prisma migrate dev --name add_user_emailprisma.user.findMany({ where: { email: { contains: '@' } }, include: { posts: true } })prisma.$queryRaw\model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
createdAt DateTime @default(now())
}
model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id])
authorId Int
}npx prisma migrate dev --name add_user_emailprisma.user.findMany({ where: { email: { contains: '@' } }, include: { posts: true } })prisma.$queryRaw\export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
name: text('name'),
createdAt: timestamp('created_at').defaultNow(),
});db.select().from(users).where(eq(users.email, email))npx drizzle-kit generate:pgnpx drizzle-kit push:pgexport const users = pgTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
name: text('name'),
createdAt: timestamp('created_at').defaultNow(),
});db.select().from(users).where(eq(users.email, email))npx drizzle-kit generate:pgnpx drizzle-kit push:pg@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
@OneToMany(() => Post, post => post.author)
posts: Post[];
}userRepo.find({ where: { email }, relations: ['posts'] })npx typeorm migration:generate -n AddUserEmail@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
@OneToMany(() => Post, post => post.author)
posts: Post[];
}userRepo.find({ where: { email }, relations: ['posts'] })npx typeorm migration:generate -n AddUserEmailclass User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String(255), unique=True, nullable=False)
name = Column(String(255))
posts = relationship('Post', back_populates='author')with Session() as session:alembic revision --autogenerate -m "add user email"See references/orm_patterns.md for side-by-side comparisons and migration workflows per ORM.
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String(255), unique=True, nullable=False)
name = Column(String(255))
posts = relationship('Post', back_populates='author')with Session() as session:alembic revision --autogenerate -m "add user email"更多各ORM的对比和迁移流程,请查看references/orm_patterns.md。
| Level | Dirty Read | Non-Repeatable Read | Phantom Read | Use Case |
|---|---|---|---|---|
| READ UNCOMMITTED | Yes | Yes | Yes | Never recommended |
| READ COMMITTED | No | Yes | Yes | Default for PostgreSQL, general OLTP |
| REPEATABLE READ | No | No | Yes (InnoDB: No) | Financial calculations |
| SERIALIZABLE | No | No | No | Critical consistency (billing, inventory) |
| 级别 | 脏读 | 不可重复读 | 幻读 | 使用场景 |
|---|---|---|---|---|
| READ UNCOMMITTED | 是 | 是 | 是 | 绝不推荐 |
| READ COMMITTED | 否 | 是 | 是 | PostgreSQL默认,通用OLTP场景 |
| REPEATABLE READ | 否 | 否 | 是(InnoDB:否) | 财务计算 |
| SERIALIZABLE | 否 | 否 | 否 | 关键一致性场景(计费、库存) |
pg_advisory_lock()pg_advisory_lock()undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined| Anti-Pattern | Problem | Fix |
|---|---|---|
| Transfers unnecessary data, breaks on schema changes | Explicit column list |
| Missing indexes on FK columns | Slow JOINs and cascading deletes | Add indexes on all foreign keys |
| N+1 queries | 1 + N round trips to database | Eager loading or batch queries |
| Implicit type coercion | | Match types in predicates |
| No connection pooling | Exhausts connections under load | PgBouncer, ProxySQL, or ORM pool |
| Unbounded queries | No LIMIT risks returning millions of rows | Always paginate |
| Storing money as FLOAT | Rounding errors | Use |
| God tables | One table with 50+ columns | Normalize or use vertical partitioning |
| Soft deletes everywhere | Complicates every query with | Archive tables or event sourcing |
| Raw string concatenation | SQL injection | Parameterized queries always |
| 反模式 | 问题 | 修复方案 |
|---|---|---|
| 传输不必要的数据,架构变更时易出错 | 显式指定列列表 |
| 外键列缺失索引 | JOIN和级联删除缓慢 | 为所有外键添加索引 |
| N+1查询 | 1+N次数据库往返 | 预加载或批量查询 |
| 隐式类型转换 | | 谓词中保持类型匹配 |
| 无连接池 | 高负载下耗尽连接 | 使用PgBouncer、ProxySQL或ORM连接池 |
| 无限制查询 | 无LIMIT可能返回数百万行 | 始终分页 |
| 用FLOAT存储金额 | 存在舍入误差 | 使用 |
| 万能表 | 一张表包含50+列 | 规范化或使用垂直分区 |
| 处处软删除 | 每个查询都需添加 | 使用归档表或事件溯源 |
| 原生字符串拼接 | SQL注入风险 | 始终使用参数化查询 |
| Skill | Relationship |
|---|---|
| database-designer | Schema architecture, normalization analysis, ERD generation |
| database-schema-designer | Visual ERD modeling, relationship mapping |
| migration-architect | Complex multi-step migration orchestration |
| api-design-reviewer | Ensuring API endpoints align with query patterns |
| observability-platform | Query performance monitoring, slow query alerts |
| 技能 | 关系 |
|---|---|
| database-designer | 架构设计、规范化分析、ERD生成 |
| database-schema-designer | 可视化ERD建模、关系映射 |
| migration-architect | 复杂多步骤迁移编排 |
| api-design-reviewer | 确保API端点与查询模式对齐 |
| observability-platform | 查询性能监控、慢查询告警 |