DDC-2464: useless index for the middle table of many-to-many relationship #3092

Open
opened 2026-01-22 14:12:01 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (May 21, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user scourgen:

I have entity A and B, the relationship between A and B is many-to-many. which means Doctrine2 will generate a middle table called AB for me.

entity A:

class Station {
    /****
     * @ORM\ManyToMany(targetEntity="Fun", mappedBy="stations")
     */
    protected $funs;
}

entity B:

class Fun {
    /****
     * @ORM\ManyToMany(targetEntity="Station", inversedBy="funs")
     * @ORM\JoinTable(name="stations*have*funs")
     */
    protected $stations;
}

the schema of middle table stations_have_funs:

CREATE TABLE `stations*have*funs` (
  `fun_id` int(11) NOT NULL,
  `station_id` int(11) NOT NULL,
  PRIMARY KEY (`fun*id`,`station*id`),
  KEY `IDX*45C921911CA4BE49` (`fun*id`),
  KEY `IDX*45C9219121BDB235` (`station*id`),
  CONSTRAINT `FK*45C921911CA4BE49` FOREIGN KEY (`fun*id`) REFERENCES `funs` (`id`) ON DELETE CASCADE,
  CONSTRAINT `FK*45C9219121BDB235` FOREIGN KEY (`station*id`) REFERENCES `stations` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8*unicode*ci;

I noticed that there are 2 useless index(fun_id and station_id). Since fun_id and station_id are the primary key of this table. Do we really need 2 extra/duplicated index ?

Originally created by @doctrinebot on GitHub (May 21, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user scourgen: I have entity A and B, the relationship between A and B is many-to-many. which means Doctrine2 will generate a middle table called AB for me. entity A: ``` class Station { /**** * @ORM\ManyToMany(targetEntity="Fun", mappedBy="stations") */ protected $funs; } ``` entity B: ``` class Fun { /**** * @ORM\ManyToMany(targetEntity="Station", inversedBy="funs") * @ORM\JoinTable(name="stations*have*funs") */ protected $stations; } ``` the schema of middle table stations_have_funs: ``` CREATE TABLE `stations*have*funs` ( `fun_id` int(11) NOT NULL, `station_id` int(11) NOT NULL, PRIMARY KEY (`fun*id`,`station*id`), KEY `IDX*45C921911CA4BE49` (`fun*id`), KEY `IDX*45C9219121BDB235` (`station*id`), CONSTRAINT `FK*45C921911CA4BE49` FOREIGN KEY (`fun*id`) REFERENCES `funs` (`id`) ON DELETE CASCADE, CONSTRAINT `FK*45C9219121BDB235` FOREIGN KEY (`station*id`) REFERENCES `stations` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8*unicode*ci; ``` I noticed that there are 2 useless index(fun_id and station_id). Since fun_id and station_id are the primary key of this table. Do we really need 2 extra/duplicated index ?
admin added the Improvement label 2026-01-22 14:12:01 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3092