Django 1.11中的数据库索引:db_true,索引和index_together之间的差异

时间:2022-04-25 20:27:44

Django 1.11 offers new ways to create database indexes. So far we had db_index=True in each field:

Django 1.11提供了创建数据库索引的新方法。到目前为止,我们在每个字段中都有db_index = True:

# example 1

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

Now we have models.Index and the possibility of declaring indexes within the class Meta block — or even index_together.

现在我们有models.Index和在类Meta块中声明索引的可能性 - 甚至是index_together。

That said I have two doubts:

那说我有两个疑问:

1. Is the code from example 1 doing the same thing as example 2 below?

1.示例1中的代码是否与下面的示例2完全相同?

# 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 index with multiple fields and index_together: are examples 3 and 4 below doing exactly the same thing?

2.具有多个字段和index_together的索引怎么样:下面的示例3和4做了完全相同的事情?

# 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 are the diferences between 1 and 2, and differences between 3 and 4? What am I missing? Many thanks.

1和2之间有什么不同,3和4之间的差异是什么?我错过了什么?非常感谢。

1 个解决方案

#1


3  

According to the docs, you can give indexes a name and you can set an order for each field in the index (but not all DBs are equally supported). But in reality the whole point of this is to bring extra flexibility to define other types of indexes where supported.

根据文档,您可以为索引指定名称,并且可以为索引中的每个字段设置顺序(但并非所有DB都受到同等支持)。但实际上,这一点的全部意义在于为定义支持的其他类型的索引带来额外的灵活性。

Currently as of Django 1.11 and only when using Postgres you can define GIN and BRIN indexes if that is something that would fit your needs. (see django.contrib.postgres.indexes at https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/)

目前从Django 1.11开始,只有在使用Postgres时,您才可以定义GIN和BRIN索引,如果它符合您的需求。 (参见https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/上的django.contrib.postgres.indexes)

Being able to define indexes as you described is just the most general case and unless you want to use a feature named above (naming indexes or ordering fields) you can probably use indexes as you've been doing until now (db_index and index_together).

能够像你描述的那样定义索引只是最常见的情况,除非你想使用上面提到的一个特性(命名索引或排序字段),你可以使用索引,就像你现在一直在做的那样(db_index和index_together)。

#1


3  

According to the docs, you can give indexes a name and you can set an order for each field in the index (but not all DBs are equally supported). But in reality the whole point of this is to bring extra flexibility to define other types of indexes where supported.

根据文档,您可以为索引指定名称,并且可以为索引中的每个字段设置顺序(但并非所有DB都受到同等支持)。但实际上,这一点的全部意义在于为定义支持的其他类型的索引带来额外的灵活性。

Currently as of Django 1.11 and only when using Postgres you can define GIN and BRIN indexes if that is something that would fit your needs. (see django.contrib.postgres.indexes at https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/)

目前从Django 1.11开始,只有在使用Postgres时,您才可以定义GIN和BRIN索引,如果它符合您的需求。 (参见https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/上的django.contrib.postgres.indexes)

Being able to define indexes as you described is just the most general case and unless you want to use a feature named above (naming indexes or ordering fields) you can probably use indexes as you've been doing until now (db_index and index_together).

能够像你描述的那样定义索引只是最常见的情况,除非你想使用上面提到的一个特性(命名索引或排序字段),你可以使用索引,就像你现在一直在做的那样(db_index和index_together)。