Django database indexes for query performance

2358
0

Database indexes dramatically speed up queries. I add indexes to frequently-filtered fields via db_index=True or Meta.indexes. Compound indexes help queries filtering on multiple fields together. For text search on PostgreSQL, I use GinIndex with SearchVector. I check if indexes are used with EXPLAIN. Too many indexes slow down writes, so I profile before adding. Partial indexes with condition reduce index size. For large tables, I create indexes concurrently in production. This is the first optimization for slow queries.