agentleFS
xiaobei930xiaobei930/cc-best/skills/database/SKILL.md

database

Skill48 starsChanged 10 months agoMITView on GitHub

Database design, query optimization, migrations, and indexing. Use when designing schemas, writing queries, or managing migrations.

24 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use payment, monitoring and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • lightpointventuresSkill

    payment

    当用户想加支付、收款、集成 Stripe、做订阅付费时使用 — 集成支付系统,支持一次性和订阅

    Also in 3 projects that use this

  • rogue-dev-studioSkill

    monitoring

    Claude monitoring and usage tracking tools (UV, claude-monitor, claude-usage-cli). Use when the project needs this capability or the user / team manifest asks for it. Use for specialized `monitoring` work when listed in TEAM.yaml or explicitly requested.

    Also in 3 projects that use this

  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 3 projects that use this

  • ffsshhttiikkSkill

    i18n

    Internationalization and localization

    Also in 3 projects that use this

  • OpenAISkill

    email

    Email sending integration guidance — Resend (native Vercel Marketplace) with React Email templates. Covers API setup, transactional emails, domain verification, and template patterns. Use when sending emails from a Vercel-deployed application.

    Also in 3 projects that use this

  • GitHubSkill

    refactor

    Surgical code refactoring to improve maintainability without changing behavior. Covers extracting functions, renaming variables, breaking down god functions, improving type safety, eliminating code smells, and applying design patterns. Less drastic than repo-rebuilder; use for gradual improvements.

    Also in 5 projects that use this

The file

Files
SKILL.md

数据库模式技能

关联 Agent: architect — 架构设计时加载数据库约束上下文

本技能提供数据库设计和操作的最佳实践,支持多数据库按需加载。

触发条件

  • 设计数据库 Schema
  • 编写数据库查询
  • 优化查询性能
  • 管理数据库迁移
  • 配置索引

数据库专属模式

根据项目技术栈,加载对应的数据库专属文件:

数据库加载文件适用场景
PostgreSQLpostgres.md企业应用、复杂查询
MySQLmysql.mdWeb 应用、读多写少
Oracleoracle.md大型企业、高并发 OLTP
SQLitesqlite.md嵌入式、移动端、本地化

检测方式: 根据连接字符串、ORM 配置或项目依赖确定数据库类型。


通用 Schema 设计

命名规范

-- 表名:小写下划线,复数形式
users, order_items, user_preferences

-- 列名:小写下划线
created_at, updated_at, user_id, is_active

-- 索引名:idx_表名_列名
idx_users_email, idx_orders_user_id_created_at

-- 外键名:fk_表名_关联表
fk_orders_users

必备字段

CREATE TABLE users (
    id BIGINT PRIMARY KEY,           -- 主键
    -- 业务字段...
    created_at TIMESTAMP NOT NULL,   -- 创建时间
    updated_at TIMESTAMP NOT NULL,   -- 更新时间
    deleted_at TIMESTAMP             -- 软删除
);

关系设计

关系类型设计方式示例
一对多子表添加外键orders.user_id → users
多对多中间表 + 联合主键user_roles(user_id, role_id)
一对一子表主键 = 外键user_settings.user_id

通用索引策略

何时创建索引

  • ✅ WHERE 条件频繁使用的列
  • ✅ JOIN 关联的列
  • ✅ ORDER BY / GROUP BY 的列
  • ❌ 很少查询的列
  • ❌ 值重复率高的列(如性别)
  • ❌ 频繁更新的列

索引类型选择

查询模式推荐索引
WHERE col = valueB-tree
WHERE col > valueB-tree
全文搜索全文索引
JSON 字段查询GIN/JSON 索引
时序数据范围查询BRIN(PG)

复合索引原则

-- 规则:等值列在前,范围列在后
-- 查询:WHERE status = 'active' AND created_at > '2024-01-01'

-- ✅ 正确顺序
CREATE INDEX idx_orders_status_created ON orders(status, created_at);

-- ❌ 错误顺序(范围列在前会导致后续列无法使用索引)
CREATE INDEX idx_orders_created_status ON orders(created_at, status);

N+1 问题

问题示例

获取 100 个用户及其订单:
1 次查询获取用户 + 100 次查询获取每个用户的订单 = 101 次查询

解决方案

方案方式适用场景
预加载JOIN 或 IN 查询数据量适中
批量加载分批 IN 查询大数据量
延迟加载按需查询不确定是否需要

分页优化

-- ❌ 大偏移量慢(OFFSET 10000 需要扫描 10000 行)
SELECT * FROM posts ORDER BY id LIMIT 20 OFFSET 10000;

-- ✅ 游标分页(直接定位)
SELECT * FROM posts WHERE id > 10000 ORDER BY id LIMIT 20;

事务原则

ACID 特性

特性含义
A 原子性全部成功或全部失败
C 一致性数据始终有效
I 隔离性事务互不干扰
D 持久性提交后永久保存

隔离级别

级别脏读不可重复读幻读性能
READ UNCOMMITTED✓✓✓最高
READ COMMITTED✗✓✓高
REPEATABLE READ✗✗✓中
SERIALIZABLE✗✗✗低

迁移管理

迁移原则

  1. 版本控制 - 所有迁移文件纳入 Git
  2. 只增不改 - 不修改已执行的迁移
  3. 可回滚 - 每个 UP 对应 DOWN
  4. 原子性 - 一个迁移只做一件事

常用 ORM 命令

# Prisma
npx prisma migrate dev --name add_column

# SQLAlchemy/Alembic
alembic revision --autogenerate -m "add column"
alembic upgrade head

# TypeORM
npm run typeorm migration:generate -- -n AddColumn
npm run typeorm migration:run

最佳实践清单

  • 表名/列名统一命名规范
  • 必备字段:id, created_at, updated_at
  • 软删除而非物理删除
  • 基于查询模式创建索引
  • 避免 N+1 查询
  • 大数据量使用游标分页
  • 迁移文件纳入版本控制
  • 合理配置连接池
  • 使用 EXPLAIN 分析慢查询

数据库专属内容

详细的数据库专属实现请参考:

  • PostgreSQL: postgres.md - 数据类型、索引策略、RLS、性能诊断
  • MySQL: mysql.md - InnoDB 优化、索引策略、字符集
  • Oracle: oracle.md - 分区表、全局索引、PL/SQL
  • SQLite: sqlite.md - WAL 模式、PRAGMA 优化、嵌入式场景

记住: 数据库设计是系统的地基——索引、约束、迁移策略在上线前就要规划好。

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.