QueryOpt
SQL slow query risk estimator and index advisor — detects N+1 patterns, missing indexes, expensive JOINs, and slow query risks using rule-based analysis + GBM ONNX model. MCP-ready for AI coding agents.
Installation
pip install queryopt
Quick Start
from queryopt import QueryOptimizer
qo = QueryOptimizer()
# Analyze a query
result = qo.analyze("SELECT * FROM users WHERE email = 'x@example.com'")
print(result["slow_risk"]) # "low"
print(result["tips"]) # [{"category": "performance", "message": "Avoid SELECT *..."}]
# Complex query
result = qo.analyze("""
SELECT * FROM orders o
JOIN users u ON o.user_id = u.id
JOIN products p ON o.product_id = p.id
JOIN categories c ON p.category_id = c.id
WHERE LOWER(u.email) LIKE '%test%'
""")
print(result["slow_risk"]) # "high"
print(result["n_plus_one"]) # False
print(result["missing_index"]) # True
print(result["estimated_cost"]) # "expensive (>100ms)"
print(result["index_suggestions"]) # ["CREATE INDEX idx_..."]
Features
| Feature | Description |
|---|---|
| Slow Query Risk | low / medium / high — GBM ONNX model |
| N+1 Detection | Identify N+1 query patterns |
| Index Advisor | Suggest missing indexes from WHERE/JOIN columns |
| Cost Estimation | fast (<10ms) / moderate (10-100ms) / expensive (>100ms) |
| Tips | Actionable optimization suggestions |
| MCP Tool | Ready for AI coding agent integration |
🤖 ML Model (v1.0 — GBM + ONNX)
GradientBoosting trained on 10K synthetic SQL queries with realistic JOIN/subquery distributions.
from queryopt.ml_model import SlowQueryModelLoader
loader = SlowQueryModelLoader()
result = loader.predict({
"query_length": 450, "join_count": 5, "subquery_depth": 2,
"has_select_star": 1, "has_no_index_hint": 1,
"where_complexity": 3, "table_count": 4, "group_by_count": 1
})
# {"slow_risk": "high", "risk_score": 0.84, "model": "onnx_v1.0"}
Model details:
- Training: 10K synthetic SQL queries
- Features: query_length, table_count, join_count, has_select_star, subquery_depth, where_complexity, group_by_count, has_no_index_hint
- Architecture: GradientBoostingClassifier → ONNX (skl2onnx)
- Model:
teyfikoz/queryopt-sql-model - Lazy-loaded on first call → cached at
~/.cache/queryopt/
Index Advisor
from queryopt import IndexAdvisor
adv = IndexAdvisor()
sql = "SELECT * FROM orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'pending'"
for suggestion in adv.suggest(sql):
print(suggestion)
# CREATE INDEX idx_status ON <table>(status)
# CREATE INDEX idx_user_id_join ON <table>(user_id)
MCP Usage
pip install "queryopt[mcp]"
# Add to your MCP config:
# {"command": "python", "args": ["-m", "queryopt.mcp_server"]}
License
MIT — Teyfik Öz
Metadata
Release files for queryopt 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| queryopt-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for queryopt-1.0.0-py3-none-any.whl |
Release files / queryopt-1.0.0-py3-none-any.whl
| Download URL | queryopt-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0382785c0314a230ef1706bc58647740dc8867066e80214732d5bc109e4d4d3a
|
|
BLAKE2b-256 checksum How to use checksums |
4eb2d5d3537e6a069602bcdeb2fa1661531637208236f1f4a597b0b2d9163dd5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for queryopt 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.