I have a webpage which auto-updates a list of users and their comments every 30 seconds.
There could be up to 300-400 users users on the site at a time.
So every 30 secs, a PHP script is called via AJA which runs this:
SELECT `user` .*, by_user.name as updated_by
"FROM `user`
"LEFT JOIN `user` by_user ON ( `user`.last_updated_by = by_user.user_id )
WHERE `user`.active= 1 AND `user`.group_id =.$group_id.
AND `user`.last_updated > (NOW() - INTERVAL 30 SECOND)
Would it be better to not use that date calculation (NOW and INTERVAL) and instead record the datetime of the last poll and then check against that?
NOW() - INTERVAL 30 SECONDis only done once for the query, and it’s blazing fast. The overhead to do anything else would almost surely incur worse impact on performance.If you have compound index (active, group_id, last_updated) it will make the performance optimal.
Since you do a u.* in the select, I wouldn’t try to create a covering index for the query.