Quick and easy question I hope. I have a query that uses something like the following in MS SQL Server 2000:
select t.* from tbl t
where
(t.void = 1 and t.dateStarted = '1/14/2009')
OR
(t.finalized = 1 and t.dateCompleted = '1/14/2009')
There is an index on both dateStarted and dateCompleted. When I run this query, it appears from the execution plan that no index is used and it takes about 33 seconds to run.
When I split this into a UNION making each OR the WHERE clause, it clearly uses the indexes and returns in under 2 seconds.
So question is, should the optimizer be able to split the OR into logical parts and take advantage of the index or is the correct approach to split this into a UNION to force the index? Is there something I can do to force the OR statement to use the relevant indexes for each side of the OR condition?
TIA!
J
select t.* from tbl t
where
(t.void = 1 and t.dateStarted = '1/14/2009')
OR
(t.finalized = 1 and t.dateCompleted = '1/14/2009')
There is an index on both dateStarted and dateCompleted. When I run this query, it appears from the execution plan that no index is used and it takes about 33 seconds to run.
When I split this into a UNION making each OR the WHERE clause, it clearly uses the indexes and returns in under 2 seconds.
So question is, should the optimizer be able to split the OR into logical parts and take advantage of the index or is the correct approach to split this into a UNION to force the index? Is there something I can do to force the OR statement to use the relevant indexes for each side of the OR condition?
TIA!
J