I'm having a hard time optimizing a query of mine. The query below will run once every few seconds so I need to fix it.
lastonline, lastcheck = unix timestamp
status = bool (int) (I'll change the type when finalizing)
SELECT *
FROM list
WHERE lastonline != 0
AND STATUS =0
AND lastcheck < '1262128371'
ORDER BY `lastcheck` ASC
LIMIT 1
EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE list ref lastcheck,status,lastonline,status_lastcheck,statu... status_lastcheck 4 const 7822 Using where
I have lastcheck, status, lastonline, status_lastcheck, status_lastonline indexes. I made the status_lastonline index as I thought of making a nested query which includes both lastonline and status. I'm not sure if this would be helpful as:
EXPLAIN SELECT *
FROM list
WHERE lastonline !=0
AND STATUS =0
LIMIT 1
1 SIMPLE list range status,lastonline,status_lastcheck,status_lastonli... status_lastonline 8 NULL 4043 Using where
Also, I don't see why does this needs 4043 rows?
Thanks, and Happy New Year! 😃