qwen2.5-coder-3b-sql-doctor-mlx

A domain-adapted Small Language Model (SLM) for offline SQL performance diagnosis, query rewriting, and index recommendation on Apple Silicon.

Fine-tuned from mlx-community/Qwen2.5-Coder-3B-Instruct-4bit by Kartik Raktate.

MLX Model Size Precision License


๐ŸŽฏ Motivation: Privacy & Zero Cloud Dependency

Enterprise production databases contain sensitive customer schemas, confidential column names, and proprietary business logic. Sending production queries to third-party cloud LLMs creates compliance risks and ongoing API costs.

qwen2.5-coder-3b-sql-doctor-mlx runs 100% locally and offline on Apple Silicon via Apple's native MLX framework. It acts as an on-device Senior Database Administrator (DBA) for developer CLI tools, local IDEs, and offline CI/CD pipelines.


โšก Key Highlights

  • Edge Optimized: Quantized to 4-bit group-wise INT4 precision. Runs with only ~2.1 GB of RAM on Apple Silicon unified memory.
  • Strict Domain Output: Generates clean, structured technical reports with:
    1. Root-Cause Diagnosis: Exact explanation of why the query suffers from sequential scans, memory spills, or CPU bottlenecks.
    2. Optimized SQL: Rewritten query eliminating anti-patterns (SELECT *, unindexed subqueries, functions on indexed columns).
    3. Migration Index DDL: Ready-to-run CREATE INDEX CONCURRENTLY ... statement for PostgreSQL or MySQL.
  • Sub-Second Latency: Delivers instantaneous token generation locally using Metal GPU acceleration on Mac.

๐Ÿš€ Quickstart: Running Locally with MLX

1. Installation

pip install mlx-lm

2. Python Inference

from mlx_lm import load, generate

# Load model directly from Hugging Face Hub
model_id = "KarRak007/qwen2.5-coder-3b-sql-doctor-mlx"
model, tokenizer = load(model_id)

# Prompt with your slow query and table schema
user_prompt = """Query: SELECT * FROM orders WHERE YEAR(order_date) = 2024;
Schema: orders (id BIGINT, user_id BIGINT, total NUMERIC, status VARCHAR, order_date DATE)"""

messages = [
    {
        "role": "system",
        "content": "You are sql-doctor, an expert Database Administrator and SQL performance engineer. Analyze the query and provide: 1) Diagnosis, 2) Optimized Query, and 3) Recommended Index."
    },
    {"role": "user", "content": user_prompt}
]

prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
response = generate(model, tokenizer, prompt=prompt, max_tokens=350, verbose=False)
print(response)

3. Interactive Terminal Chat

You can test the model interactively directly in your terminal:

python3 -m mlx_lm.chat --model KarRak007/qwen2.5-coder-3b-sql-doctor-mlx

๐Ÿ’ก Example Evaluation

Input Prompt:

Query: SELECT * FROM audit_logs WHERE action LIKE '%login_failed%' ORDER BY created_at DESC LIMIT 50;
Schema: audit_logs (id BIGINT, action VARCHAR, entity_type VARCHAR, entity_id BIGINT, created_at TIMESTAMP)

Model Output:

### Diagnosis
1. `LIKE '%pattern%'` with a leading wildcard cannot leverage a standard B-Tree index, forcing an expensive full sequential scan over disk.
2. `SELECT *` incurs unnecessary I/O overhead reading unused column blocks.

### Optimized Query
```sql
SELECT id, action, entity_id, created_at 
FROM audit_logs 
WHERE action LIKE '%login_failed%' 
ORDER BY created_at DESC 
LIMIT 50;

Recommended Index

CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX CONCURRENTLY idx_audit_logs_action_gin ON audit_logs USING gin (action gin_trgm_ops);

---

## ๐Ÿ› ๏ธ Companion Tool: `sql-doctor` CLI

This model powers **`sql-doctor`**, an open-source hybrid static AST analyzer and database query doctor that runs locally in terminal workflows.

```bash
# Check query anti-patterns locally
sql-doctor check "SELECT * FROM users JOIN orders WHERE email LIKE '%@gmail.com'"

๐Ÿ“Š Technical Specifications

Parameter Specification
Base Architecture Qwen 2.5 Coder 3B Instruct
Quantization 4-bit INT4 Group-wise
Trained Layers LoRA Attention & MLP Adaptors ($r=8, \alpha=16$)
Context Window 32,768 tokens
Memory Footprint ~2.1 GB VRAM / Unified Memory
Inference Framework Apple MLX (mlx-lm)
Primary Dialects PostgreSQL, MySQL, ANSI SQL

๐Ÿ“„ License & Attribution

This model is licensed under the Apache 2.0 License.
Developed and maintained by Kartik Raktate.

Downloads last month
666
Safetensors
Model size
3B params
Tensor type
U32
ยท
F16
ยท
MLX
Hardware compatibility
Log In to add your hardware

4-bit

Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for KarRak007/qwen2.5-coder-3b-sql-doctor-mlx