My query of interest is below
SELECT COUNT(a.id)
FROM users AS a
RIGHT JOIN date_log AS b
ON a.id = b.user_id
WHERE
b.meeting_date BETWEEN '2012-06-01'
AND DATE_ADD('2012-06-01', INTERVAL 1 MONTH)
GROUP BY a.id
However, it's too slow when I try to select data from a large database. I added 'group by' terms in the query for removing duplicated ids.
Is there better way to do this job?
First check you have indexes setup for date_log.user_id and meeting_date.
Then have you considdered using 'distinct' instead of group?
Select count(distinct a.id)
If you just need the count, you can make this query a left-join, and see if MySQL has a better time executing it (I'm not sure how it actually performs RIGHT JOINs, but I can imagine it in ways which are inefficient compared to LEFTs...
SELECT users.id, count(users.id) as user_count
FROM
date_log
LEFT JOIN users ON date_log.user_id = users.id
WHERE
date_log.meeting_date BETWEEN '2012-06-01' AND DATE_ADD('2012-06-01', INTERVAL 1 MONTH)
GROUP BY users.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.