How to index:
In your model, add the following to class Meta:
my_model(models.Model):
field1 = ..
...
class Meta:
indexes = [models.Index(fields=['field1'])]
What to index:
Most important rule: don't index everything! Take a close look at your queryset code in two or more different views:
view 1:
MyModel1.objects.filter(name=..)
view 2:
MyModel1.objects.filter(name=..)
We notice that both views contain a query for MyModel1, but most importantly, we notice that both queries use name parameter for filtering. And those parameters (that are most used for lookups) are prime candidates for put index on.
Important notes:
- Never index pk's - they are automatically indexed by Postgres
- Indexes take space - they are not free, use them wisely
