I have this query:
SELECT * FROM sample INNER JOIN test ON sample.sample_number = test.sample_number INNER JOIN result ON test.test_number = result.test_number WHERE sampled_date BETWEEN '2010-03-17 09:00' AND '2010-03-17 12:00'
The largest table here is RESULT, containing 11.1M records. Left 2 tables are about 1M.
This query is slow (over 10 minutes) and returns about 800 records. the execution plan displays a clustered scan of indexes (above it is PRIMARY KEY (result.result_number, which is not actually involved in the query)) for all 11M records. RESULT.TEST_NUMBER is the cluster primary key.
if I change 2010-03-17 09:00 - 2010-03-17 10:00 - I get about 40 entries. It runs for 300 ms. and the plan shows an index search ( indexed by result.test_number)
if I replaced * in the SELECT clause with result.test_number (with index) - then everything will be fast in the first case. this indicates hdd I / O problems, but does not specify plan changes.
So any ideas?
UPDATE: sampled_date is in the sample table and is covered by the index. other fields from this query: test.sample_number is also covered by index and result.test_number.
UPDATE 2: it is obvious that the sql server for some reason does not want to use the index.
I did a little experiment: I delete the INNER JOIN with the result, select all test.test_number and after that do
SELECT * FROM RESULT WHERE TEST_NUMBER IN (...)
it certainly works fast. but I canβt understand what the difference is and why the query optimizer chooses such an inappropriate way to select data in the first case.
UPDATE 3: after backing up the database and restoring the database with a new name - both queries work quickly, as expected, even on much wider ranges ...
So, are there any special commands for cleaning or optimization, whatever that means ?: - (