Changes between Version 2 and Version 3 of Ticket #24082


Ignore:
Timestamp:
Jan 5, 2015, 11:10:12 PM (10 years ago)
Author:
djbug
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #24082 – Description

    v2 v3  
    3131}}}
    3232
    33 Please correct me if I'm wrong. My conclusion is that database enforce unique constraint by way of an index. Adding another index is a waste. There's some mention of this fact in an old bug report ([https://code.djangoproject.com/ticket/3030#comment:3 comment 3] & [https://code.djangoproject.com/ticket/3030#comment:6 comment 6] ) but it looks like the issue got dropped.
     33Please correct me if I'm wrong. My conclusion is that databases enforce unique constraint by way of an index. Adding another index is a waste. There's some mention of this fact in an old bug report ([https://code.djangoproject.com/ticket/3030#comment:3 comment 3] & [https://code.djangoproject.com/ticket/3030#comment:6 comment 6] ) but it looks like the issue got dropped.
    3434
    35 However, if the justification to add a second index is [https://code.djangoproject.com/ticket/12234 Bug Report 12234] then it might be more efficient to interpret a `unique=True` as
    36 
     35I've also verified this with the following create table statement in PostgreSQL (no explicit index). A `SELECT` on `name` uses an index scan instead of a sequential scan (which means there's an implicit index). So in this case, Django doesn't need to add a `CREATE INDEX` statement.
    3736
    3837{{{
    39 CREATE UNIQUE INDEX "book_name_like_idx" ON "book" ("name" text_pattern_ops);
    40 
     38CREATE TABLE book (
     39    id serial primary key,
     40    name text UNIQUE
     41);
    4142}}}
    4243
    43 instead of a `UNIQUE` constraint, an `INDEX` and an implicit index by the database.
     44However, if the justification to add a second index is to use `text_pattern_ops` ( [https://code.djangoproject.com/ticket/12234 Bug Report 12234] ) then it might be more efficient to interpret a `unique=True` in the above table as
     45
     46{{{
     47CREATE TABLE book (
     48    id serial primary key,
     49    name text
     50);
     51
     52CREATE UNIQUE INDEX book_name_like ON book USING btree (name text_pattern_ops);
     53}}}
     54
     55i.e. no `UNIQUE` constraint in the table, only a `UNIQUE INDEX`.
     56
Back to Top