Skip to main content

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

Project description

⚠️ PROPRIETARY SOFTWARE - This is not open source. Contact for licensing.

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.3.tar.gz (34.1 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.3-py3-none-any.whl (34.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: query_optimizer-0.1.3.tar.gz
  • Upload date:
  • Size: 34.1 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.3.tar.gz
Algorithm Hash digest
SHA256 2a34733e6fd335bdf33b0d673b4cff83d8568a43e6d9124782a273e4f0630764
MD5 87023599906c66299b992ffc1bec3806
BLAKE2b-256 35805ce1bb4db0bfdf066ba1d1b60f3f3423874524854c55a3dee07836853534

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for query_optimizer-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9132eeb493f365891bfc4bd5a184a2475dc2a907ce65984b7774ecb53d1dac8d
MD5 9ec5e1107f5f35102fa9b673bf6cb616
BLAKE2b-256 f0f089a4a61c49f65631ac134bd11f95a3bcb0763a7b721e9f07880b1adbf1fc

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