The join elimination rewrite optimization reduces the join degree of the query by eliminating tables from the query when it is safe to do so. Typically, this optimization is applied for inner joins defined as primary key to foreign key joins, or primary key to primary key joins. The join elimination optimization can also be applied to tables used in outer joins, although the conditions for which the optimization is valid are much more complex.
This optimization does not eliminate tables that are updatable using UPDATE or DELETE WHERE CURRENT, even when it is correct to do so. This can negatively impact performance of the query. However, if your query is for read only, you can specify FOR READ ONLY in the SELECT statement, to ensure that the join eliminations are performed. Note that the tables appearing in subqueries or nested derived tables are inherently non-updatable, even though the tables in the main query block are updatable.
To summarize, there are three main categories of joins for which this rewrite optimization applies:
The join is a primary key to foreign key join, and only primary key columns from the primary table are referenced in the query. In this case, the primary key table is eliminated if it is not updatable.
The join is a primary key to primary key join between two instances of the same table. In this case, one of the tables is eliminated if it is not updatable.
The join is an outer join and the null-supplying table expression has the following properties:
the null-supplying table expression returns at most one row for each row of the preserved side of the outer join
no expression produced by the null-supplying table expression is needed in the rest of the query beyond the outer join.
For example, in the query below, the join is a primary key to foreign key join and the primary key table, Products, can be eliminated:
SELECT s.ID, s.LineID, p.ID FROM SalesOrderItems s KEY JOIN Products p FOR READ ONLY;
The query would be rewritten as:
SELECT s.ID, s.LineID, s.ProductID FROM SalesOrderItems s WHERE s.ProductID IS NOT NULL FOR READ ONLY;
The second query is semantically equivalent to the first because any row from the SalesOrderItems table that has a NULL foreign key to Products does not appear in the result.
In the following query, the OUTER JOIN can be eliminated given that the null-supplying table expression cannot produce more than one row for any row of the preserved side and none of the columns from Products is used above the LEFT OUTER JOIN.
SELECT s.ID, s.LineID FROM SalesOrderItems s LEFT OUTER JOIN Products p ON p.ID = s.ProductID WHERE s.Quantity > 5 FOR READ ONLY;
The query is rewritten as:
SELECT s.ID, s.LineID FROM SalesOrderItems s WHERE s.Quantity > 5 FOR READ ONLY;