2024-04-29 11:24:13 发布
网友
模型上的字段foo = models.ForeignKey(Foo)将自动为列添加数据库索引,以加快查找速度。这很好,但是Django的文档没有说明模型元的unique_together中的字段是否接受相同的处理。我碰巧有一个模型,其中unique_together中列出的一个char字段需要一个索引来快速查找。我知道在字段定义中添加一个重复的db_index=True不会有任何伤害,但我很好奇。
foo = models.ForeignKey(Foo)
unique_together
db_index=True
如果有人想知道他们是否需要index_together除了unique_together来获得索引的性能优势,Postgres的答案是no,它们是functionally the same.
index_together
如果unique_together确实添加了索引,那么它将是一个多列索引。
如果希望单独索引其中一列,我认为需要在字段定义中指定db_index=True。
unique_together不会自动为列表中包含的每个字段添加索引。
Django的新版本建议改用Index&constraint元选项:
https://docs.djangoproject.com/en/2.2/ref/models/options/#unique-together
https://docs.djangoproject.com/en/2.2/ref/models/options/#index-together
如果有人想知道他们是否需要
index_together
除了unique_together
来获得索引的性能优势,Postgres的答案是no,它们是functionally the same.如果
unique_together
确实添加了索引,那么它将是一个多列索引。如果希望单独索引其中一列,我认为需要在字段定义中指定
db_index=True
。unique_together
不会自动为列表中包含的每个字段添加索引。Django的新版本建议改用Index&constraint元选项:
https://docs.djangoproject.com/en/2.2/ref/models/options/#unique-together
https://docs.djangoproject.com/en/2.2/ref/models/options/#index-together
相关问题 更多 >
编程相关推荐