Skip to content

ray-marvin/sql-query-optimizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Query Analyzer & Optimizer

A FastAPI backend that analyzes SQL queries for performance issues, provides AI-powered optimization suggestions using Claude, and tracks data lineage.

Features

  • SQL Parsing — Extracts tables, columns, joins, subqueries using sqlglot
  • Issue Detection — Identifies SELECT *, missing WHERE, ORDER without LIMIT, excessive joins, high complexity
  • AI Optimization — Uses Claude to suggest query rewrites, index definitions, and structural improvements
  • Data Lineage — Tracks which tables and columns each query accesses, with graph representation
  • Query History — Stores and retrieves past analyses with pagination

Tech Stack

  • FastAPI — async Python web framework
  • PostgreSQL / SQLite — query history storage
  • sqlglot — SQL parsing and analysis
  • Claude API — AI-powered optimization suggestions
  • Docker — containerized deployment

Quick Start

Local (without Docker)

python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

# optional: add your API key for AI suggestions
cp .env.example .env

uvicorn app.main:app --reload

Docker

export ANTHROPIC_API_KEY=sk-ant-xxxxx
docker compose up --build

API docs at http://localhost:8000/docs

API Endpoints

POST /queries/analyze

Analyze a SQL query and get optimization suggestions.

curl -X POST http://localhost:8000/queries/analyze \
  -H "Content-Type: application/json" \
  -d '{"sql": "SELECT * FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100 ORDER BY o.created_at"}'

GET /queries/history

Paginated query history.

GET /queries/{query_id}

Full details of a previously analyzed query.

POST /lineage/analyze

Analyze data lineage for a SQL query.

GET /lineage/{query_id}

Get stored lineage for a previously analyzed query.

GET /health

Health check.

About

SQL query analyzer that detects performance anti-patterns and provides AI-powered optimization suggestions using Claude API, with data lineage tracking

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors