FK 上的联接是否比没有 FK 的联接更快?
假设我有两个表,a
和 b
:
a {
pk as int
fk as int
...
}
b {
pk as int
...
}
我想在查询中加入 a 和 b,如下所示:
FROM a
JOIN b on a.fk = b.pk
以下哪种情况会更快?
a.fk
设置为b.pk
上的外键 -b.pk
已索引a.fk 设置为
b.pk
上的外键 -b.pk
未建立索引,- 表之间没有关系 -
b.pk< /code> 已索引,
- 表之间没有关系 -
b.pk
未编入索引
额外问题 - 每种情况会快/慢多少?
如果您可以用参考文献来支持您的答案,那就太好了。谢谢你!
Say I have two tables, a
and b
:
a {
pk as int
fk as int
...
}
b {
pk as int
...
}
I want to join a and b in a query like so:
FROM a
JOIN b on a.fk = b.pk
Which of the following scenarios will be faster?
a.fk
is set up to be a foreign key onb.pk
-b.pk
is indexeda.fk
is set up to be a foreign key onb.pk
-b.pk
is not indexed- there is no relationship between the tables -
b.pk
is indexed - there is no relationship between the tables -
b.pk
is not indexed
Bonus question - how much faster/slower will each of these scenarios be?
If you could back up your answer with a reference then that'd be awesome. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最佳实践
从逻辑上讲,这给出了以下排名性能方面,
a.fk
设置为b.pk
上的外键 -b.pk
已索引,b.pk
已建立索引a.fk
设置为 b.pk 上的外键 -b.pk
未b.pk
未建立索引Best practice
Logically, this gives following ranking performance wise
a.fk
is set up to be a foreign key onb.pk
-b.pk
is indexedb.pk
is indexeda.fk
is set up to be a foreign key on b.pk -b.pk
is not indexedb.pk
is not indexed索引版本和非索引版本之间的性能差异最大,但是更快还是更慢将取决于它是选择还是插入。使用索引和外键约束会减慢插入速度,但会加快选择速度(索引)或使数据更可靠(FK)。由于通常大多数插入不会明显减慢(除非您正在进行大量插入),因此拥有 FK 和索引通常符合您的最佳利益。
The performance differnces would be greatest between the indexed and non indexed versions, however whether it would be faster or slower would depend on whether it was a select or an insert. Having indexes and foreign key constraints slow down inserts but speed up selects (the index) or make the data more reliable (the FK). Since generally most inserts are not noticably slowed (unless you are doing large bulk inserts), it is usually in your best interests to have the FK and the index.
我会同上利文的回答。只是为了回答您通过创建索引获得多少性能提升的额外问题,答案是“这取决于”。
如果一个或两个表都很小,并且它们是查询中仅有的两个表,则性能增益可能会小到零。当记录数量较少时,有时读取所有记录比使用索引更快。数据库引擎应该足够智能来解决这个问题——这就是“查询优化的全部内容”。
同样,如果涉及其他表和其他选择标准,数据库引擎可能决定不使用此索引,并且查找记录的其他方式更快。
在另一个极端,如果您有两个非常大的表,则在用于连接它们的字段上创建索引可以将运行时间缩短 99% 或更多。
这就是为什么学习阅读数据库引擎上的解释计划是一个好主意。如果查询需要很长时间,请运行解释计划并查看它在做什么。通常,创建一个好的索引可以显着改善查询。
I'll ditto Lieven's answer. Just to reply to your bonus question of how much of a performance boost you get from creating an index, the answer is, "That depends".
If one or both tables are small and they are the only two tables in the query, the performance gain might be small to zero. When the number of records is small, sometimes it's faster to just read all the records rather than use the index anyway. The database engine should be smart enough to figure this out -- that's what "query optimization is all about".
Likewise, if you have other tables involved and other selection criteria, the DB engine may decide not to use this index, and that some other way of finding the records is faster.
At the other extreme, if you have two very large tables, creating an index on the field used to join them can cut run time by 99% or more.
That's why it's a good idea to learn to read the explain plans on your DB engine. If a query takes a long time, run the explain plan and see what it's doing. Often, creating a good index can dramatically improve a query.