Below is a location table
CREATE TABLE locations (
`id` int(11),
`user_id` int(11),
`timestamp` datetime
PRIMARY KEY (`id`),
KEY `index_on_timestamp` (`timestamp`),
KEY `index_on_user_id` (`user_id`)
);
I run the following query to retrieve a given user’s location for the past week:
SELECT * FROM locations
WHERE user_id=20
AND timestamp > NOW() - INTERVAL 7 DAY;
We have indexes on both user_id and timestamp, but, this query takes a pretty long time to run. need help fixing this.
EXPLAINstatement here. That will give us some indication of what the query is doing