Database indexes in Django 1.11: difference between db_true, indexes and index_together

Django 1.11 offers new ways to create database indexes. So far, we have had db_index=Truein every field:

# example 1

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?

# example 2

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?

# example 3

class Person(models.Model):
    name = models.CharField()
    age = models.IntegerField()

    class Meta:
        indexes = [
            models.Index(fields=['name', 'age'])
        ]
# example 4

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.

+6
source share
1 answer

, ( DB ). , , .

Django 1.11 Postgres GIN BRIN, , . (. django.contrib.postgres.indexes https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/)

, , , ( ), , , , (db_index index_together).

+3

All Articles