加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 为什么Django显式地在唯一字段上创建索引

发布时间:2020-12-12 07:48:35 所属栏目:MsSql教程 来源:网络整理
导读:更新:进一步简化实验psql的Q: 对于以下Django模型: class Book(models.Model): name = models.TextField(unique=True) pg_dump(PostgreSQL 9.3)显示下表限制: CREATE TABLE book ( id integer NOT NULL,name text NOT NULL,);ALTER TABLE ONLY book ADD C
更新:进一步简化实验psql的Q:

对于以下Django模型:

class Book(models.Model):
    name = models.TextField(unique=True)

pg_dump(PostgreSQL 9.3)显示下表&限制:

CREATE TABLE book (
    id integer NOT NULL,name text NOT NULL,);

ALTER TABLE ONLY book ADD CONSTRAINT book_name_key UNIQUE (name);

CREATE INDEX book_name_like ON book USING btree (name text_pattern_ops);

但是PostgreSQL documentation说:

PostgreSQL automatically creates a unique index when a unique
constraint […] is defined for a table.

[…] there’s
no need to manually create indexes on unique columns; doing so would
just duplicate the automatically-created index.

问题:为什么Django会在一个唯一的列上创建索引呢?也许理由是它使用运算符类text_pattern_ops,因此Django需要添加另一个索引.如果是这种情况,更好的方法是将Django解释为unique = True约束,如下所示:

CREATE UNIQUE INDEX book_name_like ON book USING btree (name text_pattern_ops);

根本没有列中的UNIQUE约束.因此,带有text_pattern_ops的单个UNIQUE INDEX将导致DB不为UNIQUE约束创建隐式索引.

解决方法

问题的核心是 Django documentation中的这种保证:

Note that when unique is True you don’t need to specify db_index,because unique implies the creation of an index.

因此,通过Django的契约,unique = True意味着db_index = True,而db_index = True意味着Django必须创建text_pattern_ops索引以支持所有查找类型(参见ticket 12234).

至于仅使用一个唯一索引,PostgreSQL documentation表示不会涵盖所有查找类型:

Note that you should also create an index with the default operator class if you want queries involving ordinary <,<=,>,or >= comparisons to use an index. Such queries cannot use the xxx_pattern_ops operator classes.

您可以尝试添加unique = True和db_index = False.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读