Django是否在PostgreSQL中索引Autofield / ID键? [英] Does Django index Autofield / ID keys in PostgreSQL?

查看:77
本文介绍了Django是否在PostgreSQL中索引Autofield / ID键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django的文档指出使用 AutoField 创建的 id 字段


我错过了什么吗?

解决方案

PostgreSQL自动为主键创建索引。来自文档


添加主键将自动在主键中列出的列或一组列上创建唯一的B树索引,并强制该列标记为NOT NULL。


似乎PGAdmin没有显示这些索引。 此邮件列表主题是最佳来源我可以找到。


Django's docs state that id fields created with AutoField are indexed:

id is indexed by the database and is guaranteed to be unique.

Similarly it applies an index to every FK relationship.

However, in PostgreSQL whilst FKs appear to be indexed, IDs are not. Here's an example:

class TestModelBase(models.Model):
    name = models.CharField(max_length=50)
    fkfield = models.ForeignKey(TestModelFK, blank=True, null=True,
                                on_delete=models.CASCADE)
    m2mfield = models.ManyToManyField(TestModelM2M, related_name='base_m2m')

This model appears to apply the fkfield index, but not the id autofield. From PGAdmin below:

Am I missing something?

解决方案

PostgreSQL automatically creates indexes for primary keys. From the docs:

Adding a primary key will automatically create a unique B-tree index on the column or group of columns listed in the primary key, and will force the column(s) to be marked NOT NULL.

It appears that PGAdmin does not show those indexes. This mailing list thread is the best source I could find.

这篇关于Django是否在PostgreSQL中索引Autofield / ID键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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