Feature request: avoid duplicating index for foreign key column if already covered as primary column by other indexes #6677

Open
opened 2026-01-22 15:36:51 +01:00 by admin · 0 comments
Owner

Originally created by @andig on GitHub (Apr 4, 2021).

I have the following simple data table:

/**
 * Data entity
 *
 * @Entity
 * @Table(
 * 	name="data",
 *	uniqueConstraints={@UniqueConstraint(name="data_unique", columns={"channel_id", "timestamp"})}
 * )
 */
class Data
{
	/**
	 * @Id
	 * @Column(type="integer", nullable=false)
	 * @GeneratedValue(strategy="IDENTITY")
	 */
	protected $id;

	/**
	 * Ending timestamp of period in ms since 1970
	 *
	 * @Column(type="bigint", nullable=false)
	 */
	protected $timestamp;

	/**
	 * @Column(type="float", nullable=false)
	 */
	protected $value;

	/**
	 * @ManyToOne(targetEntity="Channel", inversedBy="data")
	 * @JoinColumn(name="channel_id", referencedColumnName="id", nullable=false)
	 */
	protected $channel;

which gives

CREATE TABLE `data` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `channel_id` int(11) DEFAULT NULL,
  `timestamp` bigint(20) NOT NULL,
  `value` float NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `data_unique` (`channel_id`,`timestamp`),
  KEY `IDX_ADF3F36372F5A1AA` (`channel_id`)
)

Since data_unique already covers channel_id in the first position, the additional IDX_ADF3F36372F5A1AA key on channel_id should not be necessary and the on-disk space for that index could be saved.

Originally created by @andig on GitHub (Apr 4, 2021). I have the following simple `data` table: ``` /** * Data entity * * @Entity * @Table( * name="data", * uniqueConstraints={@UniqueConstraint(name="data_unique", columns={"channel_id", "timestamp"})} * ) */ class Data { /** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="IDENTITY") */ protected $id; /** * Ending timestamp of period in ms since 1970 * * @Column(type="bigint", nullable=false) */ protected $timestamp; /** * @Column(type="float", nullable=false) */ protected $value; /** * @ManyToOne(targetEntity="Channel", inversedBy="data") * @JoinColumn(name="channel_id", referencedColumnName="id", nullable=false) */ protected $channel; ``` which gives ``` CREATE TABLE `data` ( `id` int(6) NOT NULL AUTO_INCREMENT, `channel_id` int(11) DEFAULT NULL, `timestamp` bigint(20) NOT NULL, `value` float NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `data_unique` (`channel_id`,`timestamp`), KEY `IDX_ADF3F36372F5A1AA` (`channel_id`) ) ``` Since `data_unique` already covers `channel_id` in the first position, the additional `IDX_ADF3F36372F5A1AA` key on `channel_id` should not be necessary and the on-disk space for that index could be saved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6677