>>the true headache begin when you have this situation:
>>where condition1 OR condition2
>>both condition1 condition2 that are optimized
>>but the engine generates a plan that run across the all table.
>>I solve with a UNION, which has zero readability,
>>but it can be thousands of times faster
>
>I would be interested to see an example of such command, making usage of the UNION for such approach.
>
>Thanks
It is not a fixed rule.
In some cases, the engine realizes the indexes and generates a good plan, in practice flowing the two indices and makes a union at the end.
On next example, the simplicity allows the engine to generate a good plan,
but if you compare the plans you see that the one with union is simpler.
DROP table #TT
create table #TT (AA INT PRIMARY KEY, BB INT)
CREATE INDEX IK ON #TT (BB)
INSERT #TT SELECT 2*ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
, 3*ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM sys.objects A,sys.objects B
SELECT * FROM #TT WHERE AA = 4 OR BB = 3000
SELECT * FROM #TT WHERE AA = 4
UNION
SELECT * FROM #TT WHERE BB = 3000
SELECT * FROM #TT WHERE AA = 4 OR BB = 6
SELECT * FROM #TT WHERE AA = 4
UNION
SELECT * FROM #TT WHERE BB = 6