The following query gets the last report (where latitude and longitude and secs are <> 0
) associated with the specified units:
SELECT
reports.*
FROM
reports
INNER JOIN
units
ON units.id = reports.unit_id
WHERE
reports.unit_id IN (1111, 1112, 1113)
AND
(
reports.id =
(
SELECT reports.id
FROM reports
WHERE reports.unit_id = units.id
AND
reports.time_secs != 0
AND
reports.latitude != 0.0
AND
reports.longitude != 0.0
ORDER BY time desc
LIMIT 1
)
)
This query took several minutes to run, and I was wondering if there is an optimization I can do to it.