When i do the relation for stat in an active record, the following SQL is executed:
SELECT `idto` AS `c`, COUNT(*) AS `s` FROM `message` WHERE (hasbeenread=0) GROUP BY `idto` HAVING `message`.`idto`=1;
But the following sql is 100 times faster (4s vs 40ms!!)
SELECT `idto` as `c`, count(*) as `s` FROM `message` WHERE idto=1 && (hasbeenread=0) GROUP BY idto
Thanks,