我对架构的设计有点困惑,但在我开始之前,让我先向您展示架构,
CREATE TABLE Person
(
PersonID INT NOT NULL PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
-- some columns here..
CONSTRAINT tb_idF INDEX (FirstName),
CONSTRAINT tb_idL INDEX (LastName)
-- or
-- CONSTRAINT tb_idL INDEX (FirstName, LastName)
-- other constraints ...
);
CREATE TABLE JobDescription
(
JobDescriptionID INT NOT NULL PRIMARY KEY,
JobDescriptionName VARCHAR(50) UNIQUE
-- some columns here..
-- constraints ...
);
混乱就在这里,表的映射表:Person
和JobDescription
。目前,我有这个设计,
CREATE TABLE Person_JobDescription
(
RECID INT AUTO_INCREMENT PRIMARY KEY, -- for some special reasons
-- I need to map to other table
PersonID INT,
JobDescriptionID INT,
StartYear INT, -- year JobDescription was Appointed
EndYear INT,
CONSTRAINT tb_fk1 FOREIGN KEY (PersonID)
REFERENCES Person(PersonID),
CONSTRAINT tb_fk2 FOREIGN KEY (JobDescriptionID)
REFERENCES JobDescription(JobDescriptionID),
CONSTRAINT tb_uq UNIQUE (PersonID, JobDescriptionID)
);
但我有另一个想法,映射表的结构将是这样的
CREATE TABLE Person_JobDescription
(
PersonID INT, -- map these two columns on the other table
JobDescriptionID INT, -- ^^
StartYear INT, -- year JobDescription was Appointed
EndYear INT,
CONSTRAINT tb_fk1 FOREIGN KEY (PersonID)
REFERENCES Person(PersonID),
CONSTRAINT tb_fk2 FOREIGN KEY (JobDescriptionID)
REFERENCES JobDescription(JobDescriptionID),
CONSTRAINT tb_pk PRIMARY KEY (PersonID, JobDescriptionID)
);
当我针对上面的表创建和测试查询时,它们都返回相同的结果,并且性能也与我在小型数据库(具有 50k 条记录)上测试的性能相同。我想知道这两个查询是否在大型数据库上表现如何。
问题
Person_JobDescription
在大型数据库中,您更喜欢映射表 ( ) 的两种模式中的哪一种?
按照指示,我不允许在and上创建UNIQUE
约束。但我已经在这两列上提供了一个索引。FirstName
LastName
- 我将在 table 上使用什么类型的索引
Person
?FirstName
每列的索引或和的复合索引LastName
? - 我什么时候会使用单索引
INDEX (Col1)
及INDEX (Col2)
以上INDEX (Col1, Col2)
?
感谢您花时间阅读这个问题。
最好的祝福,
德里克·弗洛斯