Skip to main content

Universal SQL query optimizer to analyze, optimize, and monitor slow queries.

Project description

Query Optimizer Logo

Query Optimizer

An AI-powered SQL query optimizer that analyzes slow database queries and suggests performance improvements. Achieve 10–100× faster queries with intelligent index recommendations, query rewrites, and execution-plan analysis.

CI License: MIT PyPI


Table of Contents


🚀 Features

  • Multi-Database Support — PostgreSQL, MySQL, SQLite
  • Smart Analysis — Identifies missing indexes, inefficient joins, suboptimal WHERE/GROUP patterns, and more
  • Actionable Recommendations — Concrete SQL (e.g. CREATE INDEX ...) and rewritten queries
  • Multiple Access Methods — REST API, CLI tool, Python library
  • Production Ready — Containerized with Docker and CI/CD ready
  • Safety-first — Dry-run mode, explicit --apply flag for changes that mutate schema

💡 Quickstart

Hosted API (Recommended)

Use the hosted API when you want a no-ops, zero-maintenance start:

curl -s -X POST https://query-optimizer.onrender.com/analyze \
  -H "Content-Type: application/json" \
  -d '{
    "query": "SELECT * FROM users WHERE email = \'user@example.com\'",
    "db_type": "postgresql"
  }'

Install CLI Tool

pip install git+https://github.com/makroumi/query-optimizer.git

Usage after install:

# Analyze a single ad-hoc query (no DB connection)
query-optimize "SELECT * FROM users WHERE created_at > '2023-01-01'" --db-type postgresql

# Analyze queries from a file
query-optimize --file slow_queries.sql --db-type mysql

# Connect to your DB for schema-aware analysis (safer + better recommendations)
query-optimize --connection "postgresql://user:pass@localhost/db" --analyze-schema

⚠️ By default the tool runs in read-only analysis mode. Use --apply with care to run any schema-changing recommendations.

Run Locally with Docker

git clone https://github.com/makroumi/query-optimizer.git
cd query-optimizer
docker build -t query-optimizer .
docker run --rm -p 8000:8000 query-optimizer

The web UI + API will be available at http://localhost:8000.


🧭 Usage

API Example

import requests

payload = {
    "query": "SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.status = 'pending'",
    "db_type": "postgresql"
}

resp = requests.post("https://query-optimizer.onrender.com/analyze", json=payload)
print(resp.json())

Visit https://query-optimizer.onrender.com/docs for interactive API docs (OpenAPI/Swagger).

CLI Example

# Analyze queries in a file and output JSON recommendations
query-optimize --file slow_queries.sql --db-type sqlite --output recommendations.json

# Analyze a live DB (requires connection string and proper credentials)
query-optimize --connection "postgresql://user:pass@host:5432/dbname" --analyze-schema --file slow_queries.sql

Python Library

from query_optimizer import QueryOptimizer

optimizer = QueryOptimizer(db_type="postgresql")
result = optimizer.analyze("""
    SELECT u.*, COUNT(o.id) as order_count
    FROM users u
    LEFT JOIN orders o ON u.id = o.user_id
    GROUP BY u.id
    HAVING COUNT(o.id) > 10
""")

# programmatic access to recommendations and the optimized query
print(result.recommendations)
print(result.optimized_query)

🧪 Demo

Run the included demo script to see the optimizer in action against a test SQLite DB:

python demo_optimizer.py

What the demo does:

  • creates a sample SQLite database
  • runs intentionally slow queries
  • shows optimization recommendations and SQL to fix them
  • (optionally) applies indexes in a sandbox

📊 Example Output

{
  "original_query": "SELECT * FROM users WHERE email = 'user@example.com'",
  "issues": [
    {
      "type": "missing_index",
      "severity": "high",
      "description": "No index on users.email causing full table scan"
    }
  ],
  "recommendations": [
    {
      "type": "add_index",
      "sql": "CREATE INDEX idx_users_email ON users(email);",
      "impact": "~100x faster for email lookups"
    }
  ],
  "optimized_query": "SELECT id, name, email FROM users WHERE email = 'user@example.com'",
  "estimated_improvement": "95%"
}

🛠️ Development

Clone and set up a dev environment:

git clone https://github.com/makroumi/query-optimizer.git
cd query-optimizer
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -e .
pip install -r requirements.txt

Run tests

pytest -q

Test coverage and linting are included in the CI pipeline. See .github/workflows for details.


🤝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m "Add amazing feature"
  4. Push to your branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Please follow the project's coding standards and add unit tests for new functionality.


🏢 Enterprise & Support

  • Free Tier: 3 API calls/day
  • Pro Tier: Unlimited calls, priority support — $49/month
  • Enterprise: Self-hosted, custom adapters, SLA

Contact: elmehdi.makroumi@gmail.com


📄 License

MIT License — see LICENSE file in repository.


🙏 Acknowledgments

Built with frustration-driven development after too many 3am debugging sessions with slow queries. Thanks to the open-source community and many hours of query plan spelunking.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

query_optimizer-0.1.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

query_optimizer-0.1.1-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file query_optimizer-0.1.1.tar.gz.

File metadata

  • Download URL: query_optimizer-0.1.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for query_optimizer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ebfa06874b5f45459338aa27b1bfe95b43c72ee1aa8267d741b1c1f1fcbf8713
MD5 3db742ef5799ca597220c2e8b55c3a0a
BLAKE2b-256 05f9dbfea59adc610a6e9047b44cbb60db8e6d240b86f8a6121f4348f5f88423

See more details on using hashes here.

File details

Details for the file query_optimizer-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for query_optimizer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 133c126a8d5dedcfddcd30b0a45b718a5bceee4e5f396128e4af9bb5f831c736
MD5 7523e83af3563726b4542e6258a22c3c
BLAKE2b-256 df735a432cdf2dada2e4777a40999a408f2de7c8d5ea07c14a267923e0437f88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page