Here is my query:
SELECT count(1)
FROM qanda question
JOIN qanda answer ON question.Id = answer.related
WHERE answer.related IS NOT NULL
AND answer.author_id = 29
AND question.amount IS NULL
AND answer.id not in (
select post_id
from votes
where table_code = 15
group by post_id
having sum(value) < 0)
And this is the EXPLAIN
result of it:
As you see, all tables are using an index. Ok, now I need to add one more condition on the WHERE
clause of outer query. This is that condition:
... AND from_unixtime(answer.date_time) BETWEEN (now() - INTERVAL 1 year) AND (now() - INTERVAL 1 hour)
Well, after adding that new condition, this is the EXPLAIN
result of it:
See? answer
table doesn't use any index anymore. Why? And what index do I need to make query above faster and more efficient?