使用外键是否有任何性能(速度)优势?
我有 2 个模式:
-- borrowed from https://stackoverflow.com/q/7745609/808921
CREATE TABLE IF NOT EXISTS `books` (
`id` int(6) unsigned NOT NULL,
`book_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tags` (
`tag_id` int(6) unsigned NOT NULL,
`tag` varchar(100) NOT NULL,
`book_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`tag_id`)
);
INSERT INTO `books` (`id`, `book_name`) VALUES
('1', 'The earth is flat'),
('2', 'One hundred angels can dance on the head of a pin'),
('3', 'The earth is flat and rests on a bull\'s horn'),
('4', 'The earth is like a ball.');
INSERT INTO `tags` (`tag_id`, `tag`, `book_id`) VALUES
('1', 'earth', '1'),
('2', 'flat', '1'),
('3', 'ball', '5'),
('4', 'bull', '4');
现在有
-- borrowed from https://stackoverflow.com/q/7745609/808921
CREATE TABLE IF NOT EXISTS `books` (
`id` int(6) unsigned NOT NULL,
`book_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tags` (
`tag_id` int(6) unsigned NOT NULL,
`tag` varchar(100) NOT NULL,
`book_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`tag_id`),
FOREIGN KEY (`book_id`) REFERENCES books(`id`)
);
INSERT INTO `books` (`id`, `book_name`) VALUES
('1', 'The earth is flat'),
('2', 'One hundred angels can dance on the head of a pin'),
('3', 'The earth is flat and rests on a bull\'s horn'),
('4', 'The earth is like a ball.');
INSERT INTO `tags` (`tag_id`, `tag`, `book_id`) VALUES
('1', 'earth', '1'),
('2', 'flat', '1'),
('4', 'bull', '4');
一个查询:
select * from books
inner join tags as t
on books.id=t.tag_id
where book_name LIKE '%earth%'
带有外键的模式是否会比没有外键的模式更快?是的,我知道外键与速度无关——它们有助于验证值。但是,我担心这种情况下的速度,因为我的专有数据库不支持外键。
SQL 小提琴(带 fk)-> http://sqlfiddle.com/#!9/f25b99/3
SQL 小提琴 (没有 fk)-> http://sqlfiddle.com/#!9/2043b8/4
I have 2 schemas:
-- borrowed from https://stackoverflow.com/q/7745609/808921
CREATE TABLE IF NOT EXISTS `books` (
`id` int(6) unsigned NOT NULL,
`book_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tags` (
`tag_id` int(6) unsigned NOT NULL,
`tag` varchar(100) NOT NULL,
`book_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`tag_id`)
);
INSERT INTO `books` (`id`, `book_name`) VALUES
('1', 'The earth is flat'),
('2', 'One hundred angels can dance on the head of a pin'),
('3', 'The earth is flat and rests on a bull\'s horn'),
('4', 'The earth is like a ball.');
INSERT INTO `tags` (`tag_id`, `tag`, `book_id`) VALUES
('1', 'earth', '1'),
('2', 'flat', '1'),
('3', 'ball', '5'),
('4', 'bull', '4');
and
-- borrowed from https://stackoverflow.com/q/7745609/808921
CREATE TABLE IF NOT EXISTS `books` (
`id` int(6) unsigned NOT NULL,
`book_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tags` (
`tag_id` int(6) unsigned NOT NULL,
`tag` varchar(100) NOT NULL,
`book_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`tag_id`),
FOREIGN KEY (`book_id`) REFERENCES books(`id`)
);
INSERT INTO `books` (`id`, `book_name`) VALUES
('1', 'The earth is flat'),
('2', 'One hundred angels can dance on the head of a pin'),
('3', 'The earth is flat and rests on a bull\'s horn'),
('4', 'The earth is like a ball.');
INSERT INTO `tags` (`tag_id`, `tag`, `book_id`) VALUES
('1', 'earth', '1'),
('2', 'flat', '1'),
('4', 'bull', '4');
and now a query:
select * from books
inner join tags as t
on books.id=t.tag_id
where book_name LIKE '%earth%'
Is the schema with Foreign Key going to be faster than the one without? Yes, I know foreign keys are not about speed - they help in validation of the values. However, I am concerned about speed in this case because my proprietary db doesn't support foreign keys.
SQL fiddle (with fk) -> http://sqlfiddle.com/#!9/f25b99/3
SQL fiddle (without fk) -> http://sqlfiddle.com/#!9/2043b8/4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是和否。
这是一种性能优势,因为它在其中一个表中创建索引。这可能对某些 SQL 语句有显着的好处。您可以通过自己添加
INDEX
来获得等效内容。这是一个性能负担,因为每当执行
INSERT
时,它都会检查以确保有匹配的行。此外,还会对UPDATE
和DELETE
执行完整性检查。这是如上所述的完整性检查。根据您正在执行的操作,该检查可能对您的代码来说是多余的。
Yes and No.
It is a performance benefit in that it creates an index in one of the tables. This may be a significant benefit to some SQL statements. You could get the equivalent by including an
INDEX
yourself.It is a performance burden in that it will check to make sure there is a matching row whenever an
INSERT
is performed. Also integrity checks are performed forUPDATEs
andDELETEs
.It is an integrity check as mentioned above. Depending on what you are doing, the check is likely to be redundant with your code.