Finding Slow Queries
Install django-debug-toolbar in development and enable the SQL panel. In production, log queries slower than 100ms using Django's DATABASE logging configuration.
N+1 Queries
The most common Django performance killer. Always use select_related() for ForeignKey and OneToOne fields, and prefetch_related() for ManyToMany and reverse FKs.
Indexes
Add db_index=True on fields used in WHERE, ORDER BY, and JOIN clauses. Use partial indexes and composite indexes for complex query patterns. Run EXPLAIN ANALYZE to confirm the index is being used.
Connection Pooling
Use PgBouncer or django-db-geventpool in production. Without pooling, each Django process opens a new database connection — catastrophic under load.
select_for_update and Transactions
Use select_for_update() with atomic() blocks for race-condition-sensitive operations like inventory updates and payments.
Comments (0)
No comments yet
Be the first to share your thoughts!