我如何能够最大限度地减少和优化这个mysql查询,从错误中获取更快更安全,请有人帮助我,这样我就可以更快更有效地配置我的SQL查询,并最大限度地减少执行mysql查询的加载时间。加载时间的主要来源是attendance_03和working_schedule_03之间的连接连接,它们将两个表之间的每个ID和日期合作。请帮助我,我真的需要你的帮助。这是我的表格列
SELECT * FROM
(SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "07:00"
AND
(
HOUR(attendance_03.t_in) = "07"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "07:30"
AND (
(
HOUR(attendance_03.t_in) = "07"
AND MINUTE(attendance_03.t_in) BETWEEN 31
AND 59
)
OR (
HOUR(attendance_03.t_in) = "08"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 30
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "08:00"
AND
(
HOUR(attendance_03.t_in) = "08"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "08:30"
AND (
(
HOUR(attendance_03.t_in) = "08"
AND MINUTE(attendance_03.t_in) BETWEEN 31
AND 59
)
OR (
HOUR(attendance_03.t_in) = "09"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "09:00"
AND (
HOUR(attendance_03.t_in) = "09"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "10:00"
AND (
HOUR(attendance_03.t_in) = "10"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "13:00"
AND (
HOUR(attendance_03.t_in) = "13"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "15:00"
AND (
HOUR(attendance_03.t_in) = "15"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "16:00"
AND (
HOUR(attendance_03.t_in) = "16"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "17:00"
AND (
HOUR(attendance_03.t_in) = "17"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "19:00"
AND (
HOUR(attendance_03.t_in) = "19"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "19:30"
AND (
(
HOUR(attendance_03.t_in) = "19"
AND MINUTE(attendance_03.t_in) BETWEEN 31
AND 59
)
OR (
HOUR(attendance_03.t_in) = "20"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 30
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "20:00"
AND (
HOUR(attendance_03.t_in) = "20"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "21:00"
AND (
HOUR(attendance_03.t_in) = "21"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id` AND CONCAT(`working_schedule_03`.`m`,`working_schedule_03`.`d`,`working_schedule_03`.`y`) = CONCAT(`attendance_03`.`mm`,`attendance_03`.`dd`,`attendance_03`.`yy`)
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE `working_schedule_03`.`t_in` = "23:45"
AND (
(
HOUR(attendance_03.t_in) = "23"
AND MINUTE(attendance_03.t_in) BETWEEN 46
AND 59
)
OR (
HOUR(attendance_03.t_in) = "24"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 45
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1) AS g
ORDER BY g.loc,
g.dept_id,
g.last_name ASC
这是我的表出勤率_03
CREATE TABLE `attendance_03` (
`a_id` varchar(99) NOT NULL,
`t_in` varchar(99) NOT NULL,
`t_b1_out` varchar(99) NOT NULL,
`t_b1_in` varchar(99) NOT NULL,
`t_lb_out` varchar(99) NOT NULL,
`t_lb_in` varchar(99) NOT NULL,
`t_b2_out` varchar(99) NOT NULL,
`t_b2_in` varchar(99) NOT NULL,
`t_out` varchar(99) NOT NULL,
`mu` varchar(99) NOT NULL,
`status` varchar(99) NOT NULL,
`mm` varchar(99) NOT NULL,
`dd` varchar(99) NOT NULL,
`yy` varchar(99) NOT NULL,
`d_out` int(2) NOT NULL,
`d_b1_out` int(2) NOT NULL,
`d_b1_in` int(2) NOT NULL,
`d_lb_out` int(2) NOT NULL,
`d_lb_in` int(2) NOT NULL,
`d_b2_out` int(2) NOT NULL,
`d_b2_in` int(2) NOT NULL,
`emp_id` varchar(99) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
这是我的working_schedule_03表
CREATE TABLE `working_schedule_03` (
`id` mediumint(99) NOT NULL AUTO_INCREMENT,
`start_date` datetime NOT NULL,
`end_date` datetime NOT NULL,
`username` varchar(255) NOT NULL,
`m` varchar(9) NOT NULL,
`d` varchar(9) NOT NULL,
`y` varchar(9) NOT NULL,
`emp_id` varchar(99) NOT NULL,
`eve` varchar(99) NOT NULL,
`t_in` varchar(99) NOT NULL,
`t_out` varchar(99) NOT NULL,
`emp_file` varchar(99) NOT NULL,
`dt_plot` varchar(99) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=572136 DEFAULT CHARSET=latin1
答案 0 :(得分:0)
不将DATE拆分为yy,mm,dd。 不使用VARCHAR作为数值。同上时间组件。
将所有相同的JOIN拉出UNION。
索引您正在加入的字段。
请勿使用LEFT
,除非'正确'表可能缺少行。
解决这些问题,然后再回来进行另一次颠簸。
答案 1 :(得分:0)
这是我做的优化sql查询。
SELECT
`last_name`,
`first_name`,
`attendance_03`.`emp_id`,
`departments`.`dept_name`,
`departments`.`dept_id`,
`employee_location`.`loc`,
`working_schedule_03`.`t_in` AS mt_in,
COUNT(attendance_03.a_id) AS Cymon
FROM
(`attendance_03`)
LEFT JOIN
(SELECT
*
FROM
`working_schedule_03`
WHERE `eve` = "Regular"
AND `y` = 2015
GROUP BY `emp_id`) AS `working_schedule_03`
ON `working_schedule_03`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_personal_info`
ON `employee_personal_info`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `employee_information`
ON `employee_information`.`emp_id` = `attendance_03`.`emp_id`
LEFT JOIN `departments`
ON `departments`.`dept_id` = `employee_information`.`department`
LEFT JOIN `employee_location`
ON `employee_location`.`l_id` = `employee_information`.`loc`
WHERE
(SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "07:00"
AND (
HOUR(attendance_03.t_in) = "07"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "07:30"
AND (
(
HOUR(attendance_03.t_in) = "07"
AND MINUTE(attendance_03.t_in) BETWEEN 31
AND 59
)
OR (
HOUR(attendance_03.t_in) = "08"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 30
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "08:00"
AND (
HOUR(attendance_03.t_in) = "08"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "08:30"
AND (
(
HOUR(attendance_03.t_in) = "08"
AND MINUTE(attendance_03.t_in) BETWEEN 31
AND 59
)
OR (
HOUR(attendance_03.t_in) = "09"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "09:00"
AND (
HOUR(attendance_03.t_in) = "09"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "10:00"
AND (
HOUR(attendance_03.t_in) = "10"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "13:00"
AND (
HOUR(attendance_03.t_in) = "13"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "15:00"
AND (
HOUR(attendance_03.t_in) = "15"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "16:00"
AND (
HOUR(attendance_03.t_in) = "16"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "17:00"
AND (
HOUR(attendance_03.t_in) = "17"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "19:00"
AND (
HOUR(attendance_03.t_in) = "19"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "19:30"
AND (
(
HOUR(attendance_03.t_in) = "19"
AND MINUTE(attendance_03.t_in) BETWEEN 31
AND 59
)
OR (
HOUR(attendance_03.t_in) = "20"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 30
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "20:00"
AND (
HOUR(attendance_03.t_in) = "20"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "21:00"
AND (
HOUR(attendance_03.t_in) = "21"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 59
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1
UNION
SELECT
*
FROM
(`attendance_03`)
WHERE `working_schedule_03`.`t_in` = "23:45"
AND (
(
HOUR(attendance_03.t_in) = "23"
AND MINUTE(attendance_03.t_in) BETWEEN 46
AND 59
)
OR (
HOUR(attendance_03.t_in) = "24"
AND MINUTE(attendance_03.t_in) BETWEEN 01
AND 45
)
)
AND attendance_03.yy = 2015
AND (attendance_03.dd BETWEEN 01
AND 15)
AND `employee_location`.`l_id` = 2
GROUP BY `attendance_03`.`emp_id`
HAVING COUNT(attendance_03.emp_id) > 1)