Opened 11 years ago
Last modified 8 months ago
#22125 new Cleanup/optimization
Unnecessary creation of index for ManyToManyField — at Initial Version
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | db-indexes |
Cc: | emorley@…, Phil Krylov, Dan LaManna | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
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)