i've been scratching my head over the past hours to find an optimization for a query which seems to cost a lot of time:
SELECT d.deviceID ,e.latitude, e.longitude,MAX(e.timestamp) as timestamp
FROM Device as d INNER JOIN EventData as e ON d.deviceID=e.deviceID
WHERE e.accountID='$account' AND e.timestamp<=$time AND (e.deviceID IN (SELECT deviceID FROM DeviceList WHERE accountID='$account'))
GROUP BY (e.deviceID);
so i'm querying a set of devices and for each device the last associated event.
Note that $account/$time are variables obtained from a $_POST.
EDIT : Tables structure :
CREATE TABLE IF NOT EXISTS `Device` (
`deviceID` varchar(32) NOT NULL,
`equipmentType` varchar(40) DEFAULT NULL,
`description` varchar(128) CHARACTER SET utf8 DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS `DeviceList` (
`accountID` varchar(32) NOT NULL,
`groupID` varchar(32) NOT NULL,
`deviceID` varchar(32) NOT NULL,
);
CREATE TABLE IF NOT EXISTS `EventData` (
`accountID` varchar(32) NOT NULL,
`deviceID` varchar(32) NOT NULL,
`timestamp` int(10) unsigned NOT NULL,
`statusCode` int(10) unsigned NOT NULL,
`latitude` double DEFAULT NULL,
`longitude` double DEFAULT NULL,
`gpsAge` int(10) unsigned DEFAULT NULL,
`speedKPH` double DEFAULT NULL
);
explainon the query?