mysql选择左连接优化

时间:2014-04-23 13:26:01

标签: mysql

我花了好几天时间,我希望得到一些帮助/建议。

这是我的sql,为简洁起见,我删掉了一些专栏。

SELECT  SQL_NO_CACHE 
detail.detailid,          
    weekid,dayid,
    detail_calculate.hours AS chours, 
    detail_calculate.paycategoryid AS cpaycategoryid,                  
    detail_calculate.hourstype AS chourstype,       
    detail_split.shiftsplitid,
    detail_split_calculate.hours AS schours,
    detail_flag.flagid
FROM (detail,day_details,employee)  
LEFT JOIN detail_calculate ON detail_calculate.detailid = detail.detailid  
LEFT JOIN (detail_split,detail_split_calculate) ON detail_split.detailid = detail.detailid
                  AND detail_split.shiftsplitid=detail_split_calculate.shiftsplitid  
LEFT JOIN (detail_flag,flag) ON detail.detailid = detail_flag.detailid 
          AND detail_flag.flagid = flag.flagid

WHERE  day_details.periodid= 11112
AND employee.bldgid =day_details.bldgid
AND detail.daylabel = day_details.daylabel  
AND detail.shiftid = day_details.shiftid  
AND detail.empid = employee.empid

sql平均在2388210中生成大约2000条记录,目前在详细信息表中, 详细信息表每周增加约40000条记录。

第一次运行此sql需要大约3到5秒 之后的其他运行大约需要0.1 - 0.5秒。

sql中有什么可以改进以减少执行时间吗? 我在这里看到过大约在同一时间内生成数百万条记录的报告。

请帮忙!

我的解释计划

select_type table   type     key    key_len rows    Extra   rows    Extra
SIMPLE  day_details  ref    card    4   41      41  
SIMPLE  employee     ref    PRIMARY 4   96  Using index 96  Using index
SIMPLE  detail  ref  daydata    19  1   Using where 1   Using where
SIMPLE  detail_calculate ref    FK_detail_calculate 8   1       1   
SIMPLE  detail_split     ref    FK_detail_split 8   1       1   
SIMPLE  detail_split_calculate  ref PRIMARY 8   1       1   
SIMPLE  detail_flag  ref    FK_detail_flag  8   1       1   
SIMPLE  flag    eq_ref  PRIMARY 4   1       1   

谢谢, 维克。

0 个答案:

没有答案