I just observed that my query:
SELECT X.A, X.B, X.GroupName
FROM TableA X
INNER JOIN TableB Y -- Huge table
ON (X.A = Y.Name OR X.B = Y.Name)
TableB has a CLUSTERED INDEX ON column Name because of which this query was taking hours to run. So what I did was to rewrite the query as:
SELECT X.A, X.B, X.GroupName
FROM TableA X
INNER JOIN TableB Y -- Huge table
ON X.A = Y.Name
UNION
SELECT X.A, X.B, X.GroupName
FROM TableA X
INNER JOIN TableB Y -- Huge table
ON X.B = Y.Name
This one runs in a few seconds or in the worst case, minutes. While I understand the reason now after burning myself, I was wondering if there is a cleaner way to write this query. I was thinking of a CTE but then the ON X.A = Y.Name and ON X.B = Y.Name are like parameters and am not sure how to deal with this.
My actual query is very big so I want to avoid repeating it two times for the sake of having a UNION. Any suggestions?
In cases such as this it may be acceptable to use the
UNIONif the two conditions require using the index in different ways. By putting them asORin a single condition you may be removing the ability to use the index.This is the same as the problem:
By including both you may be borking a query plan’s use of the index as it tries to find the “best of both worlds” query rather than “the best of each world, individually, added together”
Here is a (outdated) link which illustrates my point:
http://code.cheesydesign.com/?p=279
http://richardfoote.wordpress.com/category/index-full-scan-minmax/