귀하의 질문을 이해하면 이런 일이 발생하지 않습니다. 아래에서 price_history
에는 중복되는 db 엔진 자동 생성 색인이 없습니다. 적절한 키 (예를 들어, 복합)가 존재하는 경우
create table A
( `product_id` INT NOT NULL,
`category_id` INT NOT NULL,
`priceitem_id` INT NOT NULL,
PRIMARY KEY (`product_id`, `category_id`, `priceitem_id`)
)ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS `price_history` (
`amount` DECIMAL NULL,
`date_start` DATE NULL,
`date_end` DATE NULL,
`product_id` INT NOT NULL,
`category_id` INT NOT NULL,
`priceitem_id` INT NOT NULL,
PRIMARY KEY (`product_id`, `category_id`, `priceitem_id`),
FOREIGN KEY `f` (`product_id`, `category_id`, `priceitem_id`) references A(`product_id`, `category_id`, `priceitem_id`)
)ENGINE = InnoDB;
show create table price_history;
CREATE TABLE `price_history` (
`amount` decimal(10,0) DEFAULT NULL,
`date_start` date DEFAULT NULL,
`date_end` date DEFAULT NULL,
`product_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`priceitem_id` int(11) NOT NULL,
PRIMARY KEY (`product_id`,`category_id`,`priceitem_id`),
CONSTRAINT `price_history_ibfk_1` FOREIGN KEY (`product_id`, `category_id`, `priceitem_id`)
REFERENCES `a` (`product_id`, `category_id`, `priceitem_id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `price_history` (
`amount` DECIMAL NULL,
`date_start` DATE NULL,
`date_end` DATE NULL,
`product_id` INT NOT NULL,
`category_id` INT NOT NULL,
`priceitem_id` INT NOT NULL,
PRIMARY KEY (`product_id`, `category_id`, `priceitem_id`),
FOREIGN KEY `f` (`product_id`, `category_id`, `priceitem_id`) references A(`product_id`, `category_id`, `priceitem_id`)
)ENGINE = InnoDB;
show create table A;
CREATE TABLE `a` (
`product_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`priceitem_id` int(11) NOT NULL,
PRIMARY KEY (`product_id`,`category_id`,`priceitem_id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `price_historyBBB` (
id int auto_increment primary key,
`amount` DECIMAL NULL,
`date_start` DATE NULL,
`date_end` DATE NULL,
`product_id` INT NOT NULL,
`category_id` INT NOT NULL,
`priceitem_id` INT NOT NULL,
FOREIGN KEY `g` (`product_id`, `category_id`, `priceitem_id`) references A(`product_id`, `category_id`, `priceitem_id`)
)ENGINE = InnoDB;
show create table price_historyBBB;
CREATE TABLE `price_historybbb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(10,0) DEFAULT NULL,
`date_start` date DEFAULT NULL,
`date_end` date DEFAULT NULL,
`product_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`priceitem_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `g` (`product_id`,`category_id`,`priceitem_id`),
CONSTRAINT `price_historybbb_ibfk_1` FOREIGN KEY (`product_id`, `category_id`, `priceitem_id`)
REFERENCES `a` (`product_id`, `category_id`, `priceitem_id`)
) ENGINE=InnoDB;
show indexes from price_history;
show indexes from price_historyBBB;
그래서, 왼쪽-대부분은 다시 사용할만큼 충분한 덩어리, 다음 DB 엔진은 도우미 인덱스를 자동으로 생성되지 않습니다.
예를 들어 (col1, col2, col3, col5)와 (col1, col2)의 사용을 위해 호출 된 FK의 조합 인 키 (PK 등)가있는 경우 새 색인은 다음과 같습니다. 자동 생성되지 않습니다.
FK가 필요한 경우 (colX, col1, col2) 위의 col1, col2, col3, col5는 유용하지 않으므로 (가장 왼쪽 우선 순위) db 엔진이 FK를 만들어야합니다 도우미 색인.
인덱스는 기본적으로 중복됩니다. 특정 '오름차순'이외의 레코드는 속임수이므로 레코드를 추가/변경/삭제할 때마다 DB가 수행해야하는 작업이 기본적으로 두 배로 늘어납니다. –
기본 키 인덱스 내부에있는 테이블에'id' 필드가있을 수 있습니다. 중복을 강조하기 위해 그것을 제거했습니다. 다른 말로하면 .. 일반적으로 관계를 식별하는 것은 이러한 종류의 복제를 만들어서 작업을 두 배로 늘립니다. 나는 그 수수께끼를 발견. – Dennis
필드에는 원하는대로 하나의 필드 인덱스 또는 복합 필드 인덱스를 가질 수 있습니다. 그것이 초래할 수있는 유일한 해는 그들을 모두 유지해야하는 추가 오버 헤드입니다. ** IF ** ** ** 색인의 일부 필드 만 사용할 수있는 검색어는 필요하지 않습니다. 예 : 'where category_id = ... and priceitem_id = ...', 복합 키를 자유롭게 할 수 있습니다. 하지만 부분 조합으로 해당 필드에 액세스해야하는 경우 필드의 개별 인덱스를 사용하는 것이 더 나을 것입니다. 여러 개의 겹쳐진 인덱스가 아니라 필드에 별도의 인덱스를 갖는 것이 좋습니다. –