Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are joins on an FK faster than joins without an FK?

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?

  1. a.fk is set up to be a foreign key on b.pk - b.pk is indexed
  2. a.fk is set up to be a foreign key on b.pk - b.pk is not indexed
  3. there is no relationship between the tables - b.pk is indexed
  4. 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!

like image 372
Brian Beckett Avatar asked Jun 27 '11 13:06

Brian Beckett


People also ask

Are joins faster with foreign keys?

Foreign key indexes can significantly improve performance for queries that involve joins between the parent and child tables.

Is foreign key necessary for joins?

A primary key is not required. A foreign key is not required either. You can construct a query joining two tables on any column you wish as long as the datatypes either match or are converted to match. No relationship needs to explicitly exist.

Are joins on primary keys faster?

In terms of performance, fewer columns means that the database can process through the primary key faster since there is less data, and it also means that table joins will be faster since primary keys, together with foreign keys, are often used as the joining condition.

Which join is the fastest?

In case there are a large number of rows in the tables and there is an index to use, INNER JOIN is generally faster than OUTER JOIN. Generally, an OUTER JOIN is slower than an INNER JOIN as it needs to return more number of records when compared to INNER JOIN.


2 Answers

Best practice

  1. Foreign Keys are a relational integrity tool, not a performance tool. You should always create indexes on FK columns to reduce lookups. SQL Server does not do this automatically.
  2. As stated here Foreign keys boost performance

Logically, this gives following ranking performance wise

  1. a.fk is set up to be a foreign key on b.pk - b.pk is indexed
  2. there is no relationship between the tables - b.pk is indexed
  3. a.fk is set up to be a foreign key on b.pk - b.pk is not indexed
  4. there is no relationship between the tables - b.pk is not indexed
like image 154
Lieven Keersmaekers Avatar answered Nov 09 '22 10:11

Lieven Keersmaekers


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.

like image 23
HLGEM Avatar answered Nov 09 '22 09:11

HLGEM