Convert和Datediff日期轉換函式效率對比(第一次發帖)
阿新 • • 發佈:2019-02-14
select a.staffId,B.Name,
sum(case day(a.ProduceDate) when '1' then a.complement+a.ProduceTime else 0 end)'1',
sum(case day(a.ProduceDate) when '2' then a.complement+a.ProduceTime else 0 end)'2',
............
sum(case day(a.ProduceDate) when '31' then a.complement+a.ProduceTime else 0 end)'31'
from dbo .ProduceTimeBomForMaster a left join [MRP.NET.DB].dbo.Staff B
on a.staffId collate Chinese_PRC_CI_AS = B.StaffID collate Chinese_PRC_CI_AS
where datediff(month,ProduceDate,'2010-06-01') = 0--耗時2秒
--where convert(varchar(7),ProduceDate,120) ='2010-06' --耗時7秒
group by a.staffId,B.Name
在行轉列中,查詢條件用Convert(varchar(7),ProduceDate,120)來查詢,查詢結果集耗時7秒,
用Datediff(ProduceDate,'2010-06')來查詢結果集耗時2秒,
在相同的條件下datediff的效率高很多。