Below is my SQL query.
SELECT `left_table`.`right_table_id`, MAX(left_table.add_time) AS max_add_time
FROM `left_table`
LEFT JOIN `right_table` ON `left_table`.`right_table_id` = `right_table`.`id`
WHERE left_table.add_time <= NOW()
AND (
(right_table.some_id = 1 AND right_table.category != -2)
OR
(right_table.another_id = 1 AND right_table.category != -1)
) AND NOT(right_table.category = -3)
AND NOT(right_table.category = -4)
GROUP BY `right_table_id`
ORDER BY `max_add_time` DESC, `left_table`.`id` DESC
LIMIT 12
It takes 5356.6ms
to execute this query. It takes too long to me. I have been trying and trying to speed up the execution time. But no result. How can I improve the execution time for the above query?
left_table.add_time
indexed? That would speed up the efficiency of theMAX()
if it isn't.