How to get Foreign Keys horribly wrong in Django
summary
Shows that Django’s foreign keys create implicit indexes that can bloat schemas and trigger expensive, blocking work: unique_together → use a unique constraint, db_index on FK is implicit so drop it only by using separate_database_and_state with reversible SQL, and create/drop indexes concurrently (atomic=False) to avoid locks. Demonstrates with 1,000,000 products and 100 users, dropping a 6MB index and replacing it with a partial index reduces 6MB to 32K, and explains using select_for_update(of="self") to avoid locking joined rows.
AI-written summary · about
chapters (12)
- 0:00Introduction and Data Model
- 2:18Unique Constraints
- 4:09Managing Implicit FK Indexes
- 5:55Migrations and Locking
- 9:45Concurrent Operations
- 11:14Indexes on Foreign Keys
- 12:24Performance of Deletes
- 16:33Partial Indexes
- 22:02Advanced Migration Techniques
- 26:48Concurrency and Locking
- 31:45Optimizing Locks
- 33:36Q&A and Conclusion
