Does it matter which way I order the criteria in the ON clause for a JOIN?
select a.Name, b.Status from a inner join b on a.StatusID = b.ID
versus
select a.Name, b.Status from a inner join b on b.ID = a.StatusID
Is there any impact on performance? What if I had multiple criteria?
Is one order more maintainable than another?
JOINorder can be forced by putting the tables in the right order in theFROMclause:MySQL has a special clause called
STRAIGHT_JOINwhich makes the order matter.This will use an index on
b.id:And this will use an index on
a.StatusID:Oracle has a special hint
ORDEREDto enforce theJOINorder:This will use an index on
b.idor build a hash table onb:And this will use an index on
a.StatusIDor build a hash table ona:SQL Server has a hint called
FORCE ORDERto do the same:This will use an index on
b.idor build a hash table onb:And this will use an index on
a.StatusIDor build a hash table ona:PostgreSQL guys, sorry. Your TODO list says:
As for the order in the comparison, it doesn’t matter in any
RDBMS, AFAIK.Though I personally always try to estimate which column will be searched for and put this column in the left (for it to seem like an
lvalue).See this answer for more detail.