I have 2 tables facts and customer, with 422,000 and 350,000 rows.
When running this query, the joining used is Hash Match:
SELECT SUM(sales), [state], County
FROM pp.Facts f
INNER JOIN pp.Customer c ON (c.customerKey = f.customerKey)
WHERE c.County IN (N'Nassau', N'Westchester', N'Erie', N'Orange', N'Union',
N'Santa Clara', N'San Diego', N'Essex', N'Morris',
N'Dallas', N'Allegheny', N'Bucks')
GROUP BY [state], County
And it works great.
When running is query (same, but with more items in the filter), the joining changes to Nested Loops and never comes back (obviously)
SELECT SUM(sales), [state], County
FROM pp.Facts f
INNER JOIN pp.Customer c ON (c.customerKey = f.customerKey)
WHERE c.County IN (N'Nassau', N'Westchester', N'Erie', N'Orange', N'Union',
N'Santa Clara', N'San Diego', N'Essex', N'Morris',
N'Dallas', N'Allegheny', N'Bucks', N'New York',
N'Bergen', N'Montgomery', N'Harris', N'Delaware', N'San
Francisco', N'Suffolk', N'Travis', N'Middlesex',
N'Bexar', N'Tarrant', N'Los Angeles', N'Philadelphia')
GROUP BY [state], County
Why is the joining method changed?
There is a FK defined on the customerKey columns and a PK on customer.customerKey (no additional indexes)
Estimated Number of Rowsreturned from the Customer table.