Wednesday, May 21, 2008

MySql straight join and index hint


SELECT STRAIGHT_JOIN p.NAME AS NAME,
    ROUND(AVG(r.VAL)) AS AVGVAL,
    MIN(r.MINVALUE) AS MINVAL,MAX(r.MAXVALUE) AS MAXVAL
FROM reports_hourly AS r force index (PRIMARY), polleddata AS p,wirelessaccesspoint ap
WHERE p.OID = ? AND p.ID=r.POLLID
  AND r.ttime >= ? AND p.name=ap.name AND ap.aprole=0
GROUP BY p.NAME
ORDER BY AVGVAL DESC
LIMIT 0,5

* Table reports_hourly is the biggest table, contains millions records. Its primary key index is on (ttime, pollid). If move it to the last table to join, the query would run much slower.

No comments: