A Django application that automatically captures, analyzes, and optimizes SQL queries using AI-powered suggestions. This tool helps developers identify slow queries and provides intelligent optimization recommendations.
- Automatic Query Capture: Middleware automatically captures SQL queries during request processing
- AI-Powered Analysis: Uses AI providers (Mistral, OpenAI, Anthropic) to analyze query performance
- Smart Filtering: Filter queries by date range, speed, analysis status, view name, and duration
- Optimization Suggestions: Get detailed recommendations for query optimization, index suggestions, and Django ORM improvements
- Beautiful Dashboard: Modern, responsive UI with dark mode support
- Configurable Monitoring: Set thresholds for slow queries and specify which models to watch
- Pagination: Efficient pagination with filter preservation
- Python 3.8+
- Django 3.2+
- Database (SQLite, PostgreSQL, MySQL)
clone the repository:
git clone <repository-url>
cd query-optimizer
pip install -r requirements.txt
Add query_optimizer
to your INSTALLED_APPS
:
INSTALLED_APPS = [
# ... other apps
'query_optimizer',
]
Add the QUERY_OPTIMIZER_CONFIG
to your Django settings:
QUERY_OPTIMIZER_CONFIG = {
"model": "mistral-large-latest", # or "gpt-4", "claude-3-sonnet"
"api_key": "your-api-key-here",
"provider": "mistral", # "mistral", "openai", or "anthropic"
"watched_models": ['your_app_model'], # Models to monitor
"excluded_paths": ['/admin/', '/static/', '/media/'], # Paths to exclude
"slow_threshold": 0.5 # Seconds threshold for slow queries
}
Add the query capture middleware to your MIDDLEWARE
:
MIDDLEWARE = [
# ... other middleware
'query_optimizer.middleware.QueryCaptureMiddleware',
]
Add the query optimizer URLs to your main urls.py
:
from django.urls import path, include
urlpatterns = [
# ... other URLs
path('query-optimizer/', include('query_optimizer.urls')),
]
python manage.py migrate
Navigate to /query-optimizer/
to access the main dashboard.
The dashboard shows:
- Total queries captured
- Slow queries count
- Analyzed queries count
- Filterable query list
Use the filter panel to:
- Filter by date range
- Filter by query speed (slow/fast)
- Filter by analysis status
- Filter by view name
- Filter by duration range
- Sort by various criteria
- Click on a query to view details
- Click "Analyze Query" to get AI-powered optimization suggestions
- View the analysis results with:
- Performance analysis
- Optimization suggestions
- Optimized query (if applicable)
- Index suggestions
- Django ORM improvements
Navigate to the "Analysis History" tab to view all previous analyses.
Use the @track_queries
decorator to manually track queries in specific views:
from query_optimizer.decorators import track_queries
@track_queries
def my_view(request):
# Your view logic here
pass
# With custom settings
@track_queries(threshold=1.0, capture_stack=True)
def slow_view(request):
# Your view logic here
pass
-
No queries being captured
- Check if middleware is properly configured
- Verify excluded paths don't match your URLs
- Check database permissions
-
AI analysis failing
- Verify API key is correct
- Check API provider configuration
- Ensure model name is valid
-
Performance impact
- Adjust
slow_threshold
to capture fewer queries - Use
watched_models
to limit monitoring - Consider excluding more paths
- Adjust
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue on GitHub
- Check the documentation
- Review the troubleshooting section
- Initial release
- Query capture middleware
- AI-powered analysis
- Web dashboard
- Filtering and pagination
- Support for multiple AI providers