使用外键是否有任何性能(速度)优势?

发布于 2025-01-13 02:38:31 字数 2150 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2025-01-20 02:38:31

是和否。

这是一种性能优势,因为它在其中一个表中创建索引。这可能某些 SQL 语句有显着的好处。您可以通过自己添加 INDEX 来获得等效内容。

这是一个性能负担,因为每当执行 INSERT 时,它都会检查以确保有匹配的行。此外,还会对UPDATEDELETE 执行完整性检查。

这是如上所述的完整性检查。根据您正在执行的操作,该检查可能对您的代码来说是多余的。

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 for UPDATEs and DELETEs.

It is an integrity check as mentioned above. Depending on what you are doing, the check is likely to be redundant with your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文