回 帖 发 新 帖 刷新版面

主题:查询的速度问题

SELECT
(CASE LeaveApply.LeaveCode WHEN 5 THEN DayoffDaySet.DayoffDays 
            WHEN 4 THEN DayoffDaySet.OutdoorDays  
            WHEN 3 THEN DayoffDaySet.RestDays 
            WHEN 2 THEN DayoffDaySet.HolidayDays 
        ELSE NULL END) AS LeaveDays
FROM LeaveApply 
INNER JOIN  
(
SELECT LeaveApply.EmployeeID,LeaveApply.ApplyDateTimeFrom,
       SUM(DayoffDay) AS DayoffDays,SUM(OutdoorDay) AS OutdoorDays,
       SUM(RestDay) AS RestDays,SUM(HolidayDay) AS HolidayDays
FROM AttendDay 
LEFT OUTER JOIN 
LeaveApply ON AttendDay.EmployeeID=LeaveApply.EmployeeID  
WHERE MONTH(AttendDay.AttendDate) = MONTH(2006-6)
GROUP BY LeaveApply.EmployeeID,LeaveApply.ApplyDateTimeFrom
) AS DayoffDaySet ON LeaveApply.EmployeeID=DayoffDaySet.EmployeeID 
AND LeaveApply.ApplyDateTimeFrom=DayoffDaySet.ApplyDateTimeFrom
                        
LeaveApply表 + AttendDay表

CASE LeaveApply.LeaveCode WHEN 5 THEN  求  AttendDay.DayoffDay     的和
               WHEN 4 THEN 求  AttendDay.OutdoorDay      的和 
               WHEN 3 THEN 求  AttendDay.RestDay      的和
               WHEN 2 THEN 求  AttendDay.HolidayDay      的和
这样查询速度很慢,询求更快的方法

回复列表 (共2个回复)

沙发

少用函数,少用子查询,少用参数......
多用where...

板凳

创建索引

我来回复

您尚未登录,请登录后再回复。点此登录或注册