I have the following query which works as it should, but I'd like to know how I can make it run faster.
For info, tables cl
, jo
and mi
all have column ref
as the auto_inc primary key and ID
as the main identification number. Therefore I use the ref
s to keep a history of changes for ID
. So for the query I have used SELECT MAX(ref) FROM [xx] GROUP BY ID
to get the most recent cl
jo
and mi
.
SELECT mi.ref, jo.ID, mi.ID AS mID, mt.mi_type, jo.ref, jt.jo_type, cl.cl_name
FROM mi
INNER JOIN mt ON mt.ref = mi.mi_type
INNER JOIN jo ON jo.ID = mi.job
INNER JOIN cl ON cl.ID = jo.cl
INNER JOIN jt ON jt.ref = jo.jo_type
WHERE mi.ref IN (SELECT MAX(ref)
FROM mi
GROUP BY ID)
AND jo.ref IN (SELECT MAX(ref)
FROM jo
GROUP BY ID)
AND cl.ref IN (SELECT MAX(ref)
FROM cl
GROUP BY ID)
ORDER BY cl.cl_name, jt.jo_type, mt.mi_type ASC