Reputation: 4900
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 on b.pk
- b.pk
is indexeda.fk
is set up to be a foreign key on b.pk
- b.pk
is not indexedb.pk
is indexedb.pk
is not indexedBonus 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!
Upvotes: 8
Views: 3550
Reputation: 27474
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.
Upvotes: 1
Reputation: 96572
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.
Upvotes: 1
Reputation: 58441
a.fk
is set up to be a foreign key on b.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 indexedUpvotes: 8