I have this SQL statement that works but take a long time to process
I have an a_log table and people table. I need to find the last activity and the associated user for each id for the given person in the people table.
SELECT p.`id`, activity, appphone, appname, dateadd
FROM people p
LEFT JOIN a_log a
ON p.id = a.id
WHERE ( dateadd >= '2011/09/13 00:00' and dateadd <= '2011/09/13 23:59')
AND (a.date_time = (SELECT MAX(a1.date_time)
FROM activity_log a1
WHERE a.id = a1.id
GROUP BY id) OR date_time IS NULL)
ORDER BY `id` desc limit 0, 100
I have a non unique index on date_time field and id field in the a_log table
I have a primary index on id field and a non unique index on dateadd field in people table
How can i get a shorter execution time on this query?
Thank you
This should work for you: