In several of my prior posts, I’ve given examples of semi-joins. Recall that semi-joins essentially return a row from one input if we can find at least one matching row from the other input. Here is a simple example:
create table T1 (a int, b int)
create table T2 (a int, b int)
set nocount on
declare @i int
set @i = 0
while @i < 10000
insert T1 values(@i, @i)
set @i = @i + 1
while @i < 100
insert T2 values(@i, @i)
select * from T1
where exists (select * from T2 where T2.a = T1.a)
Note that the optimizer chooses a hash join and correctly builds a hash table on the smaller input T2 which has only 100 rows and probes with the larger input T1 which has 10,000 rows.
Now, let’s suppose we want to create an index to speed up this query. Perhaps we’d like to get a plan with an index nested loops join. Should we create an index on T1 or T2? Keep in mind that nested loops join only supports left semi-join not right semi-join. If we get a nested loops semi-join plan, T1 will be the outer table and T2 will be the inner table. Thus, we might be tempted to create an index on T2:
create clustered index T2a on T2(a)
Unfortunately, this index does not change the plan. The optimizer has decided that 10,000 index lookups (one for each row of T1) is still more expensive than the hash join. Fortunately, we have another less obvious option. We can create an index on the larger T1 table:
create clustered index T1a on T1(a)
Now we get an index nested loops join:
But wait a minute! This plan has an inner join. What happened to the semi-join? Remember that the semi-join simply returns each row of T1 that matches at least one row of T2. We can use an inner join to find these matches so long as we do not return any row of T1 more than once. In this plan, we use the ordered clustered index scan of T2 and the stream aggregate to eliminate any duplicates values of T2.a. Then, when we join these T2 rows with T1, we know that we can match each T1 row exactly once. Note that unlike the original hash join plan which touched all 10,000 rows of T1, this plan performs only 100 distinct index lookups on T1.
When is this transformation a good idea?
Transforming the semi-join to an inner join helps when, as in this example, we have a large number of rows on the “preserved” side of the join (T1 in this example) and the transformation enables an index seek that would have otherwise been impossible. The transformation also makes sense if the number of rows on the “lookup” side of the join is very large but most of the rows are duplicates. Since the semi-join does not depend on the duplicates, we can eliminate them. For example, let’s drop the index on T1 and create a new 10,000 row table T3 that is all duplicates:
drop index T1.T1a
create table T3 (a int, b int)
insert T3 values(0, @i)
where exists (select * from T3 where T3.a = T1.a)
Notice that even though we have no indexes and use the hash join plan, we still transform the semi-join into an inner join. This time, since we do not have an index, we use a hash aggregate to eliminate duplicate values of T3.a. Without the transformation, the hash join would have built a hash table on all 10,000 rows in T3. With the transformation, it builds a hash table on a single row.
If we add a unique index so that duplicate values of T2.a are not possible, the optimizer no longer needs to eliminate duplicates and can always perform this transformation for free. For example:
create unique clustered index T2a on T2(a) with (drop_existing = on)
Since we dropped the index on T1 (see above), we get the hash join plan again. However, unlike the original right semi-join plan, now we get an inner join. There is no need for a semi-join because the optimizer knows that there can be no duplicates in T2 and that the semi-join and inner join plans are equivalent.
Stay tuned …
This post will probably be my last of the year, but I will be back in January to continue writing about parallelism, partitioned tables, and other query processing topics.