Lukas Eder has a nice post explaining different forms of automatic join elimination:
We intended to fetch all customers and their addresses. But observe: We project only columns from the
CUSTOMER
table and we don’t have any predicates at all, specifically not predicates using theADDRESS
table. So, we’re completely ignoring any contributions from theADDRESS
table. We never really needed the JOIN in the first place!And in fact, the optimiser can prove this too, because of the
FOREIGN KEY
constraint onC.ADDRESS_ID
, which guarantees that everyCUSTOMER
record has exactly one correspondingADDRESS
record. The JOIN does not duplicate, nor remove anyCUSTOMER
rows, so it is unneeded and thus eliminated (by some, not all databases, will list each database at the end of the article).So, the database can rewrite the SQL statement to the following, equivalent SQL statement in the presence of said FOREIGN KEY
Read on for a comparison across different products as well.