For our typical applications, we want to use InnoDB foreign keys based on unique indexes. This gives us the safety of foreign key constraints, for just a bit of extra time.
Details on how we set up these tests are below, after the table of results.
| Command | InnoDB | MyISAM |
|---|---|---|
| join without index | 2.34 sec | 4.00 sec |
| join with default index | 4.55 sec | 27.84 sec |
| join with unique index | 3.96 sec | 3.97 sec |
| join with foreign key on default index | 4.65 sec | na |
| join with foreign key on unique index | 2.62 sec | na |
Next we describe how we created the tables, queries, indexes, foreign keys, and constraints.
mysql> select count(foos.id) from foos join bars on foos.bar_id=bars.id;
+----------------+
| count(foos.id) |
+----------------+
| 1000000 |
+----------------+
1 row in set (4.00 sec)