sql-database-assistant

v2026.09.24

This skill should be used when the user asks to "optimize SQL queries", "explore database schemas", "generate migration SQL", "analyze query performance", or "document database structure".

GitHub
安装命令
npx skhub add borghei/sql-database-assistant
Markdown
SKILL.md

SQL Database Assistant

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.

Clarify First

Before analyzing or generating, confirm these inputs. If any is unknown or vague, ASK — do not assume:

  • Task — query optimization / schema documentation / migration generation (selects the script)
  • SQL input — the query, the DDL file, or the from/to schema pair to operate on (the script's actual input)
  • Target database / dialect — Postgres / MySQL / etc. (changes index recommendations and migration SQL syntax)

Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions at the top of the artifact.

Quick Start

# Analyze a SQL query for performance issues
python scripts/query_optimizer.py --file slow_query.sql

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

# Explore schema from DDL file
python scripts/schema_explorer.py --file schema.sql

# Generate migration from schema diff
python scripts/migration_generator.py --from old_schema.sql --to new_schema.sql

# JSON output
python scripts/query_optimizer.py --file query.sql --format json

Tools Overview

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

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

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

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

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

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

CI Integration

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

# Generate schema docs
python scripts/schema_explorer.py --file schema.sql --format markdown > SCHEMA.md

Reference Documentation

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 WHEREPoor index usageUse UNION or IN
Functions on indexed columnsPrevents index useApply to value side

Index Guidelines

Query PatternIndex Type
WHERE col = valueB-tree on col
WHERE col1 = v AND col2 = vComposite (col1, col2)
ORDER BY colB-tree on col
WHERE col LIKE 'prefix%'B-tree on col
WHERE col IN (...)B-tree on col
Full-text searchFull-text index

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
发现
标签

此技能尚未发布标签。

版本
最新版本元数据

版本

v2026.09.24

发布时间

2026年9月24日

分类

未分类

许可证

NOASSERTION

源路径

engineering/sql-database-assistant

默认分支

main

最新提交

f308cbd

Tree SHA

d30ff9d