sql-database-assistant

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SQL Database Assistant

SQL数据库助手

Category: Engineering Domain: Database Development & Optimization
分类: 工程 领域: 数据库开发与优化

Overview

概述

The SQL Database Assistant skill provides tools for analyzing SQL query performance, exploring database schemas from DDL files, and generating migration SQL from schema differences. It helps teams write efficient queries, maintain clean schemas, and manage database evolution safely.
SQL数据库助手技能提供用于分析SQL查询性能、从DDL文件探索数据库架构以及根据架构差异生成迁移SQL的工具。它帮助团队编写高效的查询、维护清晰的架构并安全管理数据库演进。

Quick Start

快速开始

bash
undefined
bash
undefined

Analyze a SQL query for performance issues

Analyze a SQL query for performance issues

python scripts/query_optimizer.py --file slow_query.sql
python scripts/query_optimizer.py --file slow_query.sql

Analyze inline SQL

Analyze inline SQL

python scripts/query_optimizer.py --query "SELECT * FROM users WHERE name LIKE '%john%'"
python scripts/query_optimizer.py --query "SELECT * FROM users WHERE name LIKE '%john%'"

Explore schema from DDL file

Explore schema from DDL file

python scripts/schema_explorer.py --file schema.sql
python scripts/schema_explorer.py --file schema.sql

Generate migration from schema diff

Generate migration from schema diff

python scripts/migration_generator.py --from old_schema.sql --to new_schema.sql
python scripts/migration_generator.py --from old_schema.sql --to new_schema.sql

JSON output

JSON output

python scripts/query_optimizer.py --file query.sql --format json
undefined
python scripts/query_optimizer.py --file query.sql --format json
undefined

Tools Overview

工具概述

query_optimizer.py

query_optimizer.py

Analyzes SQL queries for performance issues and optimization opportunities.
FeatureDescription
SELECT * detectionFlags queries selecting all columns
Missing index hintsIdentifies WHERE/JOIN columns likely needing indexes
N+1 detectionFlags correlated subquery patterns
Full table scanDetects queries without WHERE clauses on large tables
JOIN analysisChecks join conditions and types
LIKE optimizationFlags leading wildcard LIKE patterns
分析SQL查询的性能问题与优化空间。
功能描述
SELECT * 检测标记选择所有列的查询
缺失索引提示识别WHERE/JOIN子句中可能需要索引的列
N+1检测标记关联子查询模式
全表扫描检测检测针对大表且无WHERE子句的查询
JOIN分析检查JOIN条件与类型
LIKE优化提示标记以通配符开头的LIKE模式

schema_explorer.py

schema_explorer.py

Generates documentation from SQL DDL (CREATE TABLE) files.
FeatureDescription
Table catalogLists all tables with column counts
Column detailsDocuments types, nullability, defaults
Index listingCatalogs indexes and their columns
Relationship mappingIdentifies foreign key relationships
Markdown outputGenerates schema documentation
从SQL DDL(CREATE TABLE)文件生成文档。
功能描述
表目录列出所有表及列数
列详情记录数据类型、可空性、默认值
索引列表分类记录索引及其关联列
关系映射识别外键关系
Markdown输出生成架构文档

migration_generator.py

migration_generator.py

Generates migration SQL by comparing two schema DDL files.
FeatureDescription
Column additionsALTER TABLE ADD COLUMN for new columns
Column removalsALTER TABLE DROP COLUMN for removed columns
Type changesALTER TABLE ALTER COLUMN for type modifications
New tablesCREATE TABLE for entirely new tables
Dropped tablesDROP TABLE for removed tables
Index changesCREATE/DROP INDEX for index differences
通过对比两个架构DDL文件生成迁移SQL。
功能描述
列新增为新列生成ALTER TABLE ADD COLUMN语句
列删除为移除的列生成ALTER TABLE DROP COLUMN语句
类型变更为类型修改生成ALTER TABLE ALTER COLUMN语句
新表创建为全新表生成CREATE TABLE语句
表删除为移除的表生成DROP TABLE语句
索引变更为索引差异生成CREATE/DROP INDEX语句

Workflows

工作流

Query Optimization Workflow

查询优化工作流

  1. Identify slow queries - Collect queries from slow query log
  2. Analyze - Run query_optimizer.py on each query
  3. Review findings - Prioritize by estimated impact
  4. Optimize - Apply suggested improvements
  5. Verify - Re-analyze to confirm optimization
  1. 识别慢查询 - 从慢查询日志中收集查询语句
  2. 分析 - 对每个查询运行query_optimizer.py
  3. 审查结果 - 根据预估影响优先级排序
  4. 优化 - 应用建议的改进方案
  5. 验证 - 重新分析以确认优化效果

Schema Documentation Workflow

架构文档工作流

  1. Export DDL - Dump schema from database
  2. Explore - Run schema_explorer.py to generate docs
  3. Review - Check relationships and data types
  4. Publish - Include in project documentation
  1. 导出DDL - 从数据库导出架构
  2. 探索 - 运行schema_explorer.py生成文档
  3. 审查 - 检查关系与数据类型
  4. 发布 - 纳入项目文档

Migration Workflow

迁移工作流

  1. Capture current - Export current schema DDL
  2. Define target - Write desired schema DDL
  3. Generate migration - Run migration_generator.py
  4. Review SQL - Check generated migration for safety
  5. Test - Apply to staging database first
  6. Deploy - Apply to production with rollback plan
  1. 捕获当前架构 - 导出当前架构DDL
  2. 定义目标架构 - 编写期望的架构DDL
  3. 生成迁移脚本 - 运行migration_generator.py
  4. 审查SQL - 检查生成的迁移脚本安全性
  5. 测试 - 先在预发布数据库应用
  6. 部署 - 应用到生产环境并准备回滚方案

CI Integration

CI集成

bash
undefined
bash
undefined

Lint SQL queries

Lint SQL queries

python scripts/query_optimizer.py --file queries/ --format json --strict
python scripts/query_optimizer.py --file queries/ --format json --strict

Generate schema docs

Generate schema docs

python scripts/schema_explorer.py --file schema.sql --format markdown > SCHEMA.md
undefined
python scripts/schema_explorer.py --file schema.sql --format markdown > SCHEMA.md
undefined

Reference Documentation

参考文档

  • SQL Optimization - Index strategies, query patterns, anti-patterns
  • SQL Optimization - 索引策略、查询模式、反模式

Common Patterns Quick Reference

常见模式速查

Query Anti-Patterns

查询反模式

PatternIssueFix
SELECT *
Fetches unnecessary dataList specific columns
LIKE '%term%'
Cannot use indexUse full-text search
Correlated subqueryN+1 query patternRewrite as JOIN
No WHERE clauseFull table scanAdd filtering conditions
OR
in WHERE
Poor index usageUse UNION or IN
Functions on indexed columnsPrevents index useApply to value side
模式问题修复方案
SELECT *
获取不必要的数据列出具体列
LIKE '%term%'
无法使用索引使用全文搜索
关联子查询N+1查询模式重写为JOIN
无WHERE子句全表扫描添加过滤条件
WHERE
中使用
OR
索引使用效率低使用UNION或IN
对索引列使用函数无法使用索引将函数应用到值的一侧

Index Guidelines

索引指南

Query PatternIndex Type
WHERE col = value
B-tree on col
WHERE col1 = v AND col2 = v
Composite (col1, col2)
ORDER BY col
B-tree on col
WHERE col LIKE 'prefix%'
B-tree on col
WHERE col IN (...)
B-tree on col
Full-text searchFull-text index
查询模式索引类型
WHERE col = value
基于col的B-tree索引
WHERE col1 = v AND col2 = v
复合索引(col1, col2)
ORDER BY col
基于col的B-tree索引
WHERE col LIKE 'prefix%'
基于col的B-tree索引
WHERE col IN (...)
基于col的B-tree索引
全文搜索全文索引

Migration Safety

迁移安全

  • Always generate rollback SQL alongside forward migration
  • Test migrations against a copy of production data
  • Add columns as nullable first, then backfill, then add constraints
  • Never rename columns directly; add new, migrate data, drop old
  • 始终同时生成向前迁移与回滚SQL
  • 针对生产数据副本测试迁移脚本
  • 先添加可空列,再回填数据,最后添加约束
  • 切勿直接重命名列;应新增列、迁移数据、删除旧列