A Django app that analyzes PostgreSQL query performance and recommends indexes.
Project description
🔍 Django Index Optimizer
Django Index Optimizer is a plug-and-play Django management command that analyzes your PostgreSQL query statistics and recommends indexes based on actual query patterns. It's designed to help beginners and early-stage developers optimize their applications before scaling, with minimal setup.
🚀 Features
- 📊 Analyzes
pg_stat_statementsto find slow or frequent queries. - 🧠 Extracts filter patterns (
WHERE,ORDER BY,GROUP BY) to identify indexing opportunities. - 💡 Recommends relevant single-column indexes.
- ⚙️ Optionally creates missing indexes automatically.
- 🔁 Can be run regularly or manually via
manage.py.
📦 Installation
- Install the package:
pip install django-index-optimizer
- Add it to your Django project:
# settings.py
INSTALLED_APPS = [
...
"optimizer",
]
- Ensure PostgreSQL has
pg_stat_statementsenabled:
Add this to your postgresql.conf (or via cloud provider settings):
shared_preload_libraries = 'pg_stat_statements'
Then restart PostgreSQL and run:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
⚡ Usage
Once installed, you can run the management command to optimize your indexes:
python manage.py optimize_indexes
This will:
- Fetch frequent or slow queries from
pg_stat_statements. - Analyze which columns are used most often in queries.
- Recommend indexes and attempt to create them (using
CREATE INDEX CONCURRENTLY).
📁 Project Structure
optimizer/
├── __init__.py
├── analyzer.py # Fetches and parses query stats
├── recommender.py # Suggests columns to index
├── applier.py # Applies indexes if needed
└── management/
└── commands/
└── optimize_indexes.py # Main management command file
🤝 Contributing
Contributions are welcome and encouraged — especially from beginners!
Whether it's:
- Improving index detection
- Supporting multi-column indexes
- Adding tests or docs
- Reporting bugs or edge cases
You're welcome to open issues or submit pull requests.
🛠 To develop locally:
git clone https://github.com/yassinbahri/django-index-optimizer.git
cd django-index-optimizer
pip install -e .
Then, add "optimizer" to a Django project and run tests or the management command.
📃 License
This project is licensed under the MIT License — see the LICENSE file for details.
🙌 A Note to Beginners
This tool was built with you in mind. Indexing is a powerful and often overlooked step in web app performance — by learning it early, you’ll save yourself many headaches down the line. 💡
Happy optimizing!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_index_optimizer-0.1.0.tar.gz.
File metadata
- Download URL: django_index_optimizer-0.1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3898f94d5a869ef552d791430e55f41dd85cfb849dc409fce4118ae3a6dbf7e3
|
|
| MD5 |
fafa159abadd3d565435e538be825fe2
|
|
| BLAKE2b-256 |
0cc3d06cd6c92b6a3006a0c282ce36ca6c5f7b976deb6fafe6eb787e7e592b38
|
File details
Details for the file django_index_optimizer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_index_optimizer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c90ce8bb869d5486e7a4933d1a1d315822b8dbeb1688e4bfd1a67903f5da3d0d
|
|
| MD5 |
8fce1aa8d9901815ec6860d2ae6634dd
|
|
| BLAKE2b-256 |
f4af95860ad73c2eb72ce9b2e79ebce07c9f2747afb32c8ace350a56244556c3
|