Opened 7 years ago

Last modified 4 months ago

#22125 new Cleanup/optimization

Unnecessary creation of index for ManyToManyField — at Initial Version

Reported by: tbhtan3@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: db-indexes
Cc: emorley@…, Phil Krylov Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


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)

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top