Django 1.11 offers new ways to create database indexes. So far, we have had db_index=Truein every field:
class Person(models.Model):
name = models.CharField(db_index=True)
age = models.IntegerField(db_index=True)
Now we are models.Indexable to announce indexesin the block class Meta- or even index_together.
I have two doubts:
1. Is the code from example 1 the same as example 2 below?
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
class Meta:
indexes = [
models.Index(fields=['name']),
models.Index(fields=['age'])
]
2. What about indexmultiple fields and index_together: are examples 3 and 4 shown that are done in exactly the same way?
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
class Meta:
indexes = [
models.Index(fields=['name', 'age'])
]
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
class Meta:
index_together = [['name', 'age']]
What is the difference between 1 and 2, and the difference between 3 and 4? What am I missing? Many thanks.
source
share