SELECT a.emp_id,s.name, s.department,s.register, z.Compoff_Count as Extra, ifnull(COUNT(DISTINCT TO_DAYS(a.punchdate)),0) as Monthly_Count
FROM machinedata a left join
(SELECT a.emp_id, ifnull(COUNT(DISTINCT TO_DAYS(a.punchdate)),0) as Compoff_Count
FROM machinedata a
RIght JOIN time_dimension c on c.db_date = a.punchdate
where ( year(c.db_date) = 2016 and month(c.db_date) = 8 and (c.holiday_flag = 't' or c.weekend_flag ='t'))
GROUP BY a.emp_id) Z
on z.emp_id = a.emp_id
RIght JOIN time_dimension c on c.db_date = a.punchdate
left join emp s on s.emp_id = a.emp_id
where (year(c.db_date) = 2016 and month(c.db_date) = 8 and c.holiday_flag = 'f' and c.weekend_flag ='f')
GROUP BY emp_id
上述查询工作正常..但如果我在最后添加s.department =“是”,其中查询时间超过40秒。查询变得非常减慢,如果我加入其中
我该怎么做才能提高查询性能?
https://dev.mysql.com/doc/refman/5.5/en/where-optimizations.html –
涉及的表的大小是多少?什么是结构?你可以在查询中使用EXPLAIN并发布结果吗? – Loufylouf
可能您的请求没有部门=“是”只是缓存,请为两个查询提供EXPLAIN输出 – 2oppin