mysql - Optimizing the sql with subquery with group by -
could me optimize query? have table huge data. me optimize without using sub query?
select user_id, scheduled_on_date (select user_id, scheduled_on_date `calls` order scheduled_on_date desc) cinner group user_id output expected is: need rows of users last scheduled_date call.
you can rewrite query follows
select c.user_id, c.scheduled_on_date ,other_fields_max_per_group `calls` c join (select user_id, max(scheduled_on_date) scheduled_on_date `calls` group user_id) cc on(c.user_id =cc.user_id , c.scheduled_on_date =cc.scheduled_on_date) add compound index
alter table calls add index `test` (user_id ,scheduled_on_date ) if there want greatest date , user id can use
select user_id, max(scheduled_on_date) scheduled_on_date `calls` group user_id
Comments
Post a Comment