Вот подготовленное MySQL заявление
SELECT
ag.`attendance_type`,
ag.`description`,
COUNT(a.`attendance`) attendance_count
FROM
`ems_attendance` a
RIGHT JOIN `ems_att_group` ag
ON ag.`id` = a.`attendance`
AND a.`added_date` BETWEEN '2011-06-01'
AND '2011-06-17'
AND a.`users_id` = '9'
GROUP BY a.`attendance`
ORDER BY ag.`id`;
и эквивалентная процедура хранения
DELIMITER $$
DROP PROCEDURE IF EXISTS `users_attendance_report` $$
CREATE PROCEDURE `users_attendance_report` (
IN users_id INT,
IN start_date DATE,
IN end_date DATE
)
BEGIN
SELECT
ag.`attendance_type`,
ag.`description`,
COUNT(a.`attendance`) attendance_count
FROM
`ems_attendance` a
RIGHT JOIN `ems_att_group` ag
ON ag.`id` = a.`attendance`
AND a.`added_date` BETWEEN start_date
AND end_date
AND a.`users_id` = users_id
GROUP BY a.`attendance`
ORDER BY ag.`id` ;
END $$
DELIMITER;
После выполнения запроса оба результата выдают одинаковые результаты.
Array
(
[0] => stdClass Object
(
[attendance_type] => present
[description] => Present
[attendance_count] => 10
)
[1] => stdClass Object
(
[attendance_type] => absent
[description] => Absent
[attendance_count] => 2
)
[2] => stdClass Object
(
[attendance_type] => other
[description] => Other
[attendance_count] => 0
)
[3] => stdClass Object
(
[attendance_type] => dayoff
[description] => Day Off
[attendance_count] => 2
)
)
Я внимательно смотрю на время выполнения, оба одинаковы. Когда и где один лучше и быстрее другого?