Implicit ForeignKey index and unique_together

262 views
Skip to first unread message

Dilyan Palauzov

unread,
Sep 16, 2016, 9:51:13 AM9/16/16
to django-d...@googlegroups.com
Hello,

according to the documentation models.ForeignKeys creates implicitly an index on the underlying database.

Wouldn't it be reasonable to change the default behaviour to only create implicit index, if there is no index_together or unique_together starting with the name of the foreign key? In such cases the implicit index is redundant, at least with Postgresql, as the value can be found fast using the _together index.

Greetings
Dilian

Tim Graham

unread,
Sep 16, 2016, 10:34:52 AM9/16/16
to Django developers (Contributions to Django itself), dpa-d...@aegee.org
Did you try to find anything related in Trac? Maybe https://code.djangoproject.com/ticket/24082?

I use this query in Google: postgresql unique index site:code.djangoproject.com

Cristiano Coelho

unread,
Sep 16, 2016, 8:48:15 PM9/16/16
to Django developers (Contributions to Django itself), dpa-d...@aegee.org
I think that the issue on Trac is actually something different, it talks about the need (or not) of an index, when defining a unique constraint. Most databases (if not all) will create an index automatically when a unique constraint is defined, and correct me if I'm wrong, but PostgreSQL (I don't about Oracle) is the only one that actually has constraints (unique ones included here) and indexes as a separate thing, but for SQLServer and MySQL the unique constraint is just an additional option of the index.

What Dilyan is talking about, and correct me if I'm wrong again, is about the redundancy of defining an index on a foreing key, if you already have that column as the left-most part of an index (unique or not). Most of the time it will be redundant to have an index A, and another one (A,B), since the latter will be also used for A queries. However this is up to debate since using the (A,B) index can be potentially slower than using just the A index due to the index being bigger, but you save space and insert/update/delete performance for not having two different indexes.

In my case, most of the time I end up with a db_index=False on foreing keys that I know I have a index/unique defined somewhere else to avoid the overhead of the additional index.

emo...@mozilla.com

unread,
Nov 30, 2016, 10:41:16 AM11/30/16
to Django developers (Contributions to Django itself), dpa-d...@aegee.org
I agree - #24082 is unrelated since it's PostgresSQL and text field specific, presumably resulting from:
https://github.com/django/django/blob/8eb56f3c786b4dd7f78a60145ae2e483e24b62c1/django/db/backends/postgresql/schema.py#L34-L48

The issue in this thread is specific to the interaction between unique_together and a ForeignKey, where unless an explicit `db_index=False` is set on the foreign key, Django creates a redundant index (under MySQL at least, haven't checked the others). However this appears fixed on master - I thought originally it might have been inadvertently fixed by this refactor:
https://github.com/django/django/commit/6bf7964023487f2a352084e74aca27aecb354d6c
...but that commit was backported to 1.10.x, and this reproduces on Django 1.10.3 so it can't be. (If anyone wanted to try and bisect, a reduced testcase is here: https://emorley.pastebin.mozilla.org/8933048)

As mentioned below, the workaround is to add an explicit `db_index=False` to the `ForeignKey`, however if that's done to an existing `ForeignKey`, the migration actually drops and recreates the constraint rather than just dropping the index, which is pretty bad since the latter is an performed as an online DDL operation with InnoDB, whereas adding a constraint is not. As such, I've filed:
https://code.djangoproject.com/ticket/27558

Best wishes,

Ed
Reply all
Reply to author
Forward
0 new messages