Django会自动为外键列生成索引吗? [英] Does Django automatically generate indexes for foreign keys columns?

查看:644
本文介绍了Django会自动为外键列生成索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django会自动为外键生成索引,还是仅仅依赖于底层DB策略?

解决方案

Django自动为所有 models.ForeignKey 列创建一个索引。



Django文档


ForeignKey 上自动创建数据库索引。您可以通过将 db_index 设置为 False 来禁用此功能。如果您正在创建一个外键以实现一致性而不是连接,或者您将创建一个替代索引(如部分或多个列索引),则可能希望避免索引的开销。



Does Django automatically generate indexes for foreign keys, or does it just depend on the underlying DB policy ?

解决方案

Django automatically creates an index for all models.ForeignKey columns.

From Django documentation:

A database index is automatically created on the ForeignKey. You can disable this by setting db_index to False. You may want to avoid the overhead of an index if you are creating a foreign key for consistency rather than joins, or if you will be creating an alternative index like a partial or multiple column index.

这篇关于Django会自动为外键列生成索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆