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?
You should do this with a join
You should also have a (unique if possible) index on
idfor the outer query, this enables the join to use the indexes. If optimisation is massively important you may also want an index onclient, id, or possiblyid, client, for the sub-select, though this isn’t as important as you have to scan the entire table anyway.Please also see SQL JOIN vs IN performance? and IN vs. JOIN with large rowsets for discussions around using
inorjoin.