django-querylizer


Namedjango-querylizer JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryAutomatic Django query performance analyzer and logger
upload_time2025-08-13 14:42:36
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords analyzer database django monitoring optimization orm performance query
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django Querylizer 🚀

[![PyPI version](https://badge.fury.io/py/django-querylizer.svg)](https://badge.fury.io/py/django-querylizer)
[![Python versions](https://img.shields.io/pypi/pyversions/django-querylizer.svg)](https://pypi.org/project/django-querylizer/)
[![Django versions](https://img.shields.io/pypi/djversions/django-querylizer.svg)](https://pypi.org/project/django-querylizer/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Django Querylizer** is a powerful, automatic query performance analyzer and logger for Django applications. It helps you identify slow queries, detect N+1 query problems, find duplicate queries, and monitor your database performance in real-time without any code changes to your views or models.

## ✨ Features

- 🔍 **Automatic Query Detection** - No code changes needed in your views/models
- ⚡ **Performance Monitoring** - Track slow queries and execution times
- 🔄 **N+1 Query Detection** - Automatically detect and log N+1 query patterns
- 📊 **Duplicate Query Detection** - Find and eliminate redundant database calls
- 📈 **Request-Level Analytics** - Monitor queries per request
- 🎯 **Smart Filtering** - Exclude migrations, admin queries, and other noise
- 📝 **Comprehensive Logging** - Detailed logs with customizable formats
- 🛠️ **Easy Configuration** - Flexible settings for different environments
- 🔧 **Development Friendly** - Include stack traces for debugging

## 📦 Installation

Install django-querylizer using pip:

```bash
pip install django-querylizer
```

## ⚙️ Quick Setup

### 1. Add to Installed Apps

Add `querylizer` to your `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # ... your other apps
    'querylizer',  # Add this
]
```

### 2. Add Middleware

Add the middleware to track queries per request:

```python
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # ... your other middleware
    'querylizer.middleware.QueryAnalyzerMiddleware',  # Add this
]
```

### 3. Configure Settings (Optional)

Add configuration to your `settings.py`:

```python
QUERY_ANALYZER = {
    'ENABLED': True,
    'SLOW_QUERY_THRESHOLD': 0.1,  # Log queries slower than 100ms
    'LOG_SLOW_QUERIES': True,
    'LOG_ALL_QUERIES': False,  # Set to True for development
    'LOG_DUPLICATE_QUERIES': True,
    'LOG_N_PLUS_ONE': True,
    'TRACK_QUERY_COUNT': True,
    'INCLUDE_TRACEBACK': True,  # Include stack traces for debugging
    'MAX_QUERY_LENGTH': 200,
}
```

That's it! Django Querylizer will automatically start monitoring your database queries.

## 📋 Example Output

### Slow Query Detection
```
2024-01-15 10:30:45 [WARNING] querylizer.queries: [default] SUCCESS 0.245s - SELECT * FROM products WHERE category_id = 1 ORDER BY created_at DESC LIMIT 20
```

### N+1 Query Detection
```
2024-01-15 10:31:20 [WARNING] querylizer.patterns: N+1 QUERY DETECTED: 25 similar queries | Path: /products/category/electronics/ | Pattern: SELECT * FROM reviews WHERE product_id = ?
```

### Request Summary
```
2024-01-15 10:32:10 [INFO] querylizer.requests: REQUEST SUMMARY: GET /products/ | Queries: 12 | Query Time: 0.156s | Total Time: 0.298s | User: 1001
```

### Duplicate Query Detection
```
2024-01-15 10:33:05 [WARNING] querylizer.patterns: DUPLICATE QUERIES DETECTED: 3 duplicates | Path: /dashboard/
  - SELECT COUNT(*) FROM orders WHERE user_id = 1001...
  - SELECT COUNT(*) FROM orders WHERE user_id = 1001...
  - SELECT COUNT(*) FROM orders WHERE user_id = 1001...
```

## ⚙️ Configuration Options

| Setting | Default | Description |
|---------|---------|-------------|
| `ENABLED` | `True` | Enable/disable the query analyzer |
| `AUTO_INSTRUMENT` | `True` | Automatically instrument database queries |
| `SLOW_QUERY_THRESHOLD` | `0.5` | Threshold for slow query detection (seconds) |
| `VERY_SLOW_QUERY_THRESHOLD` | `2.0` | Threshold for very slow queries (seconds) |
| `LOG_ALL_QUERIES` | `False` | Log every single query (can be noisy) |
| `LOG_SLOW_QUERIES` | `True` | Log only slow queries |
| `LOG_DUPLICATE_QUERIES` | `True` | Detect and log duplicate queries |
| `LOG_N_PLUS_ONE` | `True` | Detect and log N+1 query patterns |
| `ANALYZE_PATTERNS` | `True` | Enable query pattern analysis |
| `TRACK_QUERY_COUNT` | `True` | Track query count per request |
| `INCLUDE_TRACEBACK` | `True` | Include Python stack traces |
| `MAX_QUERY_LENGTH` | `200` | Maximum query length in logs |
| `EXCLUDE_MIGRATIONS` | `True` | Exclude Django migration queries |
| `DATABASES` | `['default']` | List of databases to monitor |
| `LOG_LEVEL` | `'INFO'` | Logging level for query analyzer |

## 🔧 Advanced Usage

### Programmatic Access

You can also access query statistics programmatically:

```python
from querylizer import QueryAnalyzer

# Get current request statistics
stats = QueryAnalyzer.get_current_request_stats()
print(f"Queries executed: {stats['query_count']}")
print(f"Total time: {stats['total_time']:.3f}s")
print(f"Slow queries: {len(stats['slow_queries'])}")

# Log custom metrics
QueryAnalyzer.log_custom_metric('cache_hits', 95, cache_type='redis')
```

### Custom Logging Configuration

You can customize the logging format and handlers:

```python
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'query_file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': 'queries.log',
        },
    },
    'loggers': {
        'querylizer.queries': {
            'handlers': ['query_file'],
            'level': 'INFO',
            'propagate': False,
        },
        'querylizer.patterns': {
            'handlers': ['query_file'],
            'level': 'WARNING',
            'propagate': False,
        },
    },
}
```

## 🎯 Use Cases

### Development Environment
- **Debug Performance Issues**: Quickly identify slow queries during development
- **Optimize Query Patterns**: Detect N+1 queries and unnecessary duplicates
- **Monitor Query Count**: Keep track of queries per request to avoid database overload

### Staging Environment
- **Performance Testing**: Monitor query performance under realistic conditions
- **Pattern Analysis**: Identify potential performance bottlenecks before production
- **Optimization Verification**: Ensure optimizations are working as expected

### Production Environment (Careful Configuration Needed)
- **Slow Query Monitoring**: Track only the slowest queries to avoid log spam
- **Performance Regression Detection**: Get alerted when queries become slower
- **Database Health Monitoring**: Monitor overall database performance trends

## 📊 Performance Impact

Django Querylizer is designed to have minimal performance impact:

- **Lightweight**: Only adds microseconds to query execution time
- **Efficient**: Smart filtering avoids logging unnecessary queries
- **Configurable**: Disable features you don't need to reduce overhead
- **Production Ready**: Can be safely used in production with proper configuration

## 🔍 Common Patterns Detected

### N+1 Queries
```python
# This code will trigger N+1 detection
posts = Post.objects.all()
for post in posts:
    print(post.author.name)  # Triggers individual queries

# Fix: Use select_related
posts = Post.objects.select_related('author').all()
```

### Duplicate Queries
```python
# This will trigger duplicate query detection
def get_user_stats(user_id):
    order_count = Order.objects.filter(user_id=user_id).count()
    total_spent = Order.objects.filter(user_id=user_id).aggregate(Sum('total'))['total__sum']

# Fix: Use a single query
def get_user_stats(user_id):
    result = Order.objects.filter(user_id=user_id).aggregate(
        order_count=Count('id'),
        total_spent=Sum('total')
    )
```

## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## 📝 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- Inspired by Django's built-in query debugging tools
- Built with ❤️ for the Django community

## 📞 Support

If you encounter any problems or have questions, please:

1. Check the [Issues](https://github.com/AvicennaJr/django-querylizer/issues) page
2. Create a new issue if your problem isn't already reported
3. Provide as much detail as possible, including Django version and configuration

---

**Made with ❤️ by [Fuad Habib](https://github.com/AvicennaJr)**

⭐ Star this repo if it helped you optimize your Django queries!

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-querylizer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "analyzer, database, django, monitoring, optimization, orm, performance, query",
    "author": null,
    "author_email": "Fuad Habib <fuad@subcode.africa>",
    "download_url": "https://files.pythonhosted.org/packages/37/5e/35c3abf6feaaac3633a35a05ba51403b21031ceca993bfd9a6ef774cc744/django_querylizer-0.1.0.tar.gz",
    "platform": null,
    "description": "# Django Querylizer \ud83d\ude80\n\n[![PyPI version](https://badge.fury.io/py/django-querylizer.svg)](https://badge.fury.io/py/django-querylizer)\n[![Python versions](https://img.shields.io/pypi/pyversions/django-querylizer.svg)](https://pypi.org/project/django-querylizer/)\n[![Django versions](https://img.shields.io/pypi/djversions/django-querylizer.svg)](https://pypi.org/project/django-querylizer/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**Django Querylizer** is a powerful, automatic query performance analyzer and logger for Django applications. It helps you identify slow queries, detect N+1 query problems, find duplicate queries, and monitor your database performance in real-time without any code changes to your views or models.\n\n## \u2728 Features\n\n- \ud83d\udd0d **Automatic Query Detection** - No code changes needed in your views/models\n- \u26a1 **Performance Monitoring** - Track slow queries and execution times\n- \ud83d\udd04 **N+1 Query Detection** - Automatically detect and log N+1 query patterns\n- \ud83d\udcca **Duplicate Query Detection** - Find and eliminate redundant database calls\n- \ud83d\udcc8 **Request-Level Analytics** - Monitor queries per request\n- \ud83c\udfaf **Smart Filtering** - Exclude migrations, admin queries, and other noise\n- \ud83d\udcdd **Comprehensive Logging** - Detailed logs with customizable formats\n- \ud83d\udee0\ufe0f **Easy Configuration** - Flexible settings for different environments\n- \ud83d\udd27 **Development Friendly** - Include stack traces for debugging\n\n## \ud83d\udce6 Installation\n\nInstall django-querylizer using pip:\n\n```bash\npip install django-querylizer\n```\n\n## \u2699\ufe0f Quick Setup\n\n### 1. Add to Installed Apps\n\nAdd `querylizer` to your `INSTALLED_APPS`:\n\n```python\nINSTALLED_APPS = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    # ... your other apps\n    'querylizer',  # Add this\n]\n```\n\n### 2. Add Middleware\n\nAdd the middleware to track queries per request:\n\n```python\nMIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n    # ... your other middleware\n    'querylizer.middleware.QueryAnalyzerMiddleware',  # Add this\n]\n```\n\n### 3. Configure Settings (Optional)\n\nAdd configuration to your `settings.py`:\n\n```python\nQUERY_ANALYZER = {\n    'ENABLED': True,\n    'SLOW_QUERY_THRESHOLD': 0.1,  # Log queries slower than 100ms\n    'LOG_SLOW_QUERIES': True,\n    'LOG_ALL_QUERIES': False,  # Set to True for development\n    'LOG_DUPLICATE_QUERIES': True,\n    'LOG_N_PLUS_ONE': True,\n    'TRACK_QUERY_COUNT': True,\n    'INCLUDE_TRACEBACK': True,  # Include stack traces for debugging\n    'MAX_QUERY_LENGTH': 200,\n}\n```\n\nThat's it! Django Querylizer will automatically start monitoring your database queries.\n\n## \ud83d\udccb Example Output\n\n### Slow Query Detection\n```\n2024-01-15 10:30:45 [WARNING] querylizer.queries: [default] SUCCESS 0.245s - SELECT * FROM products WHERE category_id = 1 ORDER BY created_at DESC LIMIT 20\n```\n\n### N+1 Query Detection\n```\n2024-01-15 10:31:20 [WARNING] querylizer.patterns: N+1 QUERY DETECTED: 25 similar queries | Path: /products/category/electronics/ | Pattern: SELECT * FROM reviews WHERE product_id = ?\n```\n\n### Request Summary\n```\n2024-01-15 10:32:10 [INFO] querylizer.requests: REQUEST SUMMARY: GET /products/ | Queries: 12 | Query Time: 0.156s | Total Time: 0.298s | User: 1001\n```\n\n### Duplicate Query Detection\n```\n2024-01-15 10:33:05 [WARNING] querylizer.patterns: DUPLICATE QUERIES DETECTED: 3 duplicates | Path: /dashboard/\n  - SELECT COUNT(*) FROM orders WHERE user_id = 1001...\n  - SELECT COUNT(*) FROM orders WHERE user_id = 1001...\n  - SELECT COUNT(*) FROM orders WHERE user_id = 1001...\n```\n\n## \u2699\ufe0f Configuration Options\n\n| Setting | Default | Description |\n|---------|---------|-------------|\n| `ENABLED` | `True` | Enable/disable the query analyzer |\n| `AUTO_INSTRUMENT` | `True` | Automatically instrument database queries |\n| `SLOW_QUERY_THRESHOLD` | `0.5` | Threshold for slow query detection (seconds) |\n| `VERY_SLOW_QUERY_THRESHOLD` | `2.0` | Threshold for very slow queries (seconds) |\n| `LOG_ALL_QUERIES` | `False` | Log every single query (can be noisy) |\n| `LOG_SLOW_QUERIES` | `True` | Log only slow queries |\n| `LOG_DUPLICATE_QUERIES` | `True` | Detect and log duplicate queries |\n| `LOG_N_PLUS_ONE` | `True` | Detect and log N+1 query patterns |\n| `ANALYZE_PATTERNS` | `True` | Enable query pattern analysis |\n| `TRACK_QUERY_COUNT` | `True` | Track query count per request |\n| `INCLUDE_TRACEBACK` | `True` | Include Python stack traces |\n| `MAX_QUERY_LENGTH` | `200` | Maximum query length in logs |\n| `EXCLUDE_MIGRATIONS` | `True` | Exclude Django migration queries |\n| `DATABASES` | `['default']` | List of databases to monitor |\n| `LOG_LEVEL` | `'INFO'` | Logging level for query analyzer |\n\n## \ud83d\udd27 Advanced Usage\n\n### Programmatic Access\n\nYou can also access query statistics programmatically:\n\n```python\nfrom querylizer import QueryAnalyzer\n\n# Get current request statistics\nstats = QueryAnalyzer.get_current_request_stats()\nprint(f\"Queries executed: {stats['query_count']}\")\nprint(f\"Total time: {stats['total_time']:.3f}s\")\nprint(f\"Slow queries: {len(stats['slow_queries'])}\")\n\n# Log custom metrics\nQueryAnalyzer.log_custom_metric('cache_hits', 95, cache_type='redis')\n```\n\n### Custom Logging Configuration\n\nYou can customize the logging format and handlers:\n\n```python\nLOGGING = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'handlers': {\n        'query_file': {\n            'level': 'INFO',\n            'class': 'logging.FileHandler',\n            'filename': 'queries.log',\n        },\n    },\n    'loggers': {\n        'querylizer.queries': {\n            'handlers': ['query_file'],\n            'level': 'INFO',\n            'propagate': False,\n        },\n        'querylizer.patterns': {\n            'handlers': ['query_file'],\n            'level': 'WARNING',\n            'propagate': False,\n        },\n    },\n}\n```\n\n## \ud83c\udfaf Use Cases\n\n### Development Environment\n- **Debug Performance Issues**: Quickly identify slow queries during development\n- **Optimize Query Patterns**: Detect N+1 queries and unnecessary duplicates\n- **Monitor Query Count**: Keep track of queries per request to avoid database overload\n\n### Staging Environment\n- **Performance Testing**: Monitor query performance under realistic conditions\n- **Pattern Analysis**: Identify potential performance bottlenecks before production\n- **Optimization Verification**: Ensure optimizations are working as expected\n\n### Production Environment (Careful Configuration Needed)\n- **Slow Query Monitoring**: Track only the slowest queries to avoid log spam\n- **Performance Regression Detection**: Get alerted when queries become slower\n- **Database Health Monitoring**: Monitor overall database performance trends\n\n## \ud83d\udcca Performance Impact\n\nDjango Querylizer is designed to have minimal performance impact:\n\n- **Lightweight**: Only adds microseconds to query execution time\n- **Efficient**: Smart filtering avoids logging unnecessary queries\n- **Configurable**: Disable features you don't need to reduce overhead\n- **Production Ready**: Can be safely used in production with proper configuration\n\n## \ud83d\udd0d Common Patterns Detected\n\n### N+1 Queries\n```python\n# This code will trigger N+1 detection\nposts = Post.objects.all()\nfor post in posts:\n    print(post.author.name)  # Triggers individual queries\n\n# Fix: Use select_related\nposts = Post.objects.select_related('author').all()\n```\n\n### Duplicate Queries\n```python\n# This will trigger duplicate query detection\ndef get_user_stats(user_id):\n    order_count = Order.objects.filter(user_id=user_id).count()\n    total_spent = Order.objects.filter(user_id=user_id).aggregate(Sum('total'))['total__sum']\n\n# Fix: Use a single query\ndef get_user_stats(user_id):\n    result = Order.objects.filter(user_id=user_id).aggregate(\n        order_count=Count('id'),\n        total_spent=Sum('total')\n    )\n```\n\n## \ud83e\udd1d Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n## \ud83d\udcdd License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## \ud83d\ude4f Acknowledgments\n\n- Inspired by Django's built-in query debugging tools\n- Built with \u2764\ufe0f for the Django community\n\n## \ud83d\udcde Support\n\nIf you encounter any problems or have questions, please:\n\n1. Check the [Issues](https://github.com/AvicennaJr/django-querylizer/issues) page\n2. Create a new issue if your problem isn't already reported\n3. Provide as much detail as possible, including Django version and configuration\n\n---\n\n**Made with \u2764\ufe0f by [Fuad Habib](https://github.com/AvicennaJr)**\n\n\u2b50 Star this repo if it helped you optimize your Django queries!\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Automatic Django query performance analyzer and logger",
    "version": "0.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/AvicennaJr/django-querylizer/issues",
        "Documentation": "https://github.com/AvicennaJr/django-querylizer#readme",
        "Homepage": "https://github.com/AvicennaJr/django-querylizer",
        "Repository": "https://github.com/AvicennaJr/django-querylizer"
    },
    "split_keywords": [
        "analyzer",
        " database",
        " django",
        " monitoring",
        " optimization",
        " orm",
        " performance",
        " query"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5bc8b32cf13c4bd5fcf790cb4af74fb3d833fed1d31316ea48e84fb129e36953",
                "md5": "b2d3915d0378725c111f7a9215287d2e",
                "sha256": "d30188106391cd0a75f8963702705f76b380464cd18296e7c1c8c01a6f88d365"
            },
            "downloads": -1,
            "filename": "django_querylizer-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b2d3915d0378725c111f7a9215287d2e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12558,
            "upload_time": "2025-08-13T14:42:34",
            "upload_time_iso_8601": "2025-08-13T14:42:34.959404Z",
            "url": "https://files.pythonhosted.org/packages/5b/c8/b32cf13c4bd5fcf790cb4af74fb3d833fed1d31316ea48e84fb129e36953/django_querylizer-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "375e35c3abf6feaaac3633a35a05ba51403b21031ceca993bfd9a6ef774cc744",
                "md5": "200eda3794ecfdd19e7cfbbe3a5180d2",
                "sha256": "f533d35be5afe6550aba00c1a6f27527cb53dcc34378c2f80cd013ff9d18aa60"
            },
            "downloads": -1,
            "filename": "django_querylizer-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "200eda3794ecfdd19e7cfbbe3a5180d2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 11624,
            "upload_time": "2025-08-13T14:42:36",
            "upload_time_iso_8601": "2025-08-13T14:42:36.196662Z",
            "url": "https://files.pythonhosted.org/packages/37/5e/35c3abf6feaaac3633a35a05ba51403b21031ceca993bfd9a6ef774cc744/django_querylizer-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-13 14:42:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AvicennaJr",
    "github_project": "django-querylizer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "django-querylizer"
}
        
Elapsed time: 1.02541s