#22125: Unnecessary creation of index for ManyToManyField
-------------------------------------+-------------------------------------
Reporter: tbhtan3@… | Owner: bwreilly
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: db-indexes | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):
* needs_better_patch: 0 => 1
Old description:
> Suppose I have the following model:
> class Food(models.Model):
> restaurants = models.ManyToManyField(Restaurant)
>
> The following table is created:
>
> CREATE TABLE "main_food_restaurants" (
> "id" integer NOT NULL PRIMARY KEY,
> "food_id" integer NOT NULL,
> "restaurant_id" integer NOT NULL,
> UNIQUE ("food_id", "restaurant_id")
> )
>
> and the indexes:
>
> CREATE INDEX "main_food_restaurants_0899c464" ON "main_food_restaurants"
> ("food_id");
> CREATE INDEX "main_food_restaurants_be4c8f84" ON "main_food_restaurants"
> ("restaurant_id");
>
> Notice that the single index on food_id is not needed due to the unique
> index (food_id, restaurant_id)
New description:
Suppose I have the following model:
{{{
class Food(models.Model):
restaurants = models.ManyToManyField(Restaurant)
}}}
The following table is created:
{{{
CREATE TABLE "main_food_restaurants" (
"id" integer NOT NULL PRIMARY KEY,
"food_id" integer NOT NULL,
"restaurant_id" integer NOT NULL,
UNIQUE ("food_id", "restaurant_id")
)
}}}
and the indexes:
{{{
CREATE INDEX "main_food_restaurants_0899c464" ON "main_food_restaurants"
("food_id");
CREATE INDEX "main_food_restaurants_be4c8f84" ON "main_food_restaurants"
("restaurant_id");
}}}
Notice that the single index on food_id is not needed due to the unique
index `(food_id, restaurant_id)`.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/22125#comment:24>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/075.7ac8a970fec42955e35af96b617971b4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.