I have a log table which looks as follows:
logs (_id_, client, metric, value);
I am attempting to write the following query in MySQL from a PHP front end to get information out of a log table.
SELECT client, LAST(value) AS value
FROM logs
WHERE metric = 'free space'
GROUP BY client;
Except, of course, that LAST
is not a valid aggregate function.
My proposed solution is this:
SELECT client, value
FROM logs
WHERE id IN (
SELECT MAX(id)
FROM logs
WHERE metric = 'free space'
GROUP BY client);
However, for a very small table of 4,000 rows, this query takes in excess of 60 seconds to execute on my development machine.
Suggestions?