Django database index strategies

2854
0

Indexes dramatically improve query performance. I add indexes via Meta.indexes or db_index=True on frequently-filtered fields. Compound indexes help queries filtering on multiple fields. For text search, I use GinIndex on PostgreSQL. I index foreign keys automatically. The EXPLAIN query shows if indexes are used. Too many indexes slow down writes, so I profile first. For large tables, I create indexes concurrently in separate migrations. This prevents locking during deployment.