前两天做系统调优的时候,想着把一系列的表联合查询换成视图查询,这样速度就会快些,但是效果却不是很理想。下面我把相关实验过程记录下来。
原sql语句是一个表联合查询,WorkOrder表数据有200多万条。下面代码是一个是联合查询,一个是将联合查询建成一个视图。
declare @d1 datetime declare @d2 datetime set @d1=getdate() --联合查询 SELECT WO.WorkOrderCode,WO.WorkOrderType,WO.Status,WO.CustName,WO.CustTel1,WO.CustTel2,WO.CustIdentityNo,WO.CardId,WO.CustSex,WO.CustAge,WO.CustBirthday, WO.IsMember,WO.AccountLevel,WO.MemberDate,WO.AccountScore,WO.DestTarget,WO.DestTargetDesc,WO.Source,WO.ToType,WO.Dept,WO.HotelId,WO.HotelName, WO.HotelCreateDate,WO.HotelType,WO.HotelBrand,WO.HotelTel,WO.HotelBigArea,WO.HotelArea,WO.HotelCityGroup,‘‘ AS HotelCity,WO.Title,WO.SelectTitle,WO.Content,WO.IsAccept,WO.CompensateDept, WO.CompensateAmountType,WO.CompensateAmount,WO.ReferDept,WO.ResponsiblePerson,W2.Attribute3 AS ResponsiblePersonName,WO.ResponsibleDate,WO.ClosePerson, WO.CloseDate,WO.CatePerson,WO.CateDate,WO.ScorePerson,WO.ScoreDate,WO.CreateUser,WO.CreateDate,WO.UpdateUser,WO.UpdateDate,W1.Attribute3 AS UserName FROM WorkOrder WO WITH (NOLOCK) LEFT JOIN WSCUser W1 ON WO.CreateUser=W1.UserID LEFT JOIN WSCUser W2 ON WO.ResponsiblePerson=W2.UserID WHERE 1=1 AND WO.STATUS=3 AND WO.WorkOrderType=‘T‘ select [语句执行花费时间(毫秒)]=datediff(ms,@d1,getdate()) set @d2=getdate() --视图查询 SELECT WO.WorkOrderCode,WO.WorkOrderType,WO.Status,WO.CustName,WO.CustTel1,WO.CustTel2,WO.CustIdentityNo,WO.CardId,WO.CustSex,WO.CustAge,WO.CustBirthday, WO.IsMember,WO.AccountLevel,WO.MemberDate,WO.AccountScore,WO.DestTarget,WO.DestTargetDesc,WO.Source,WO.ToType,WO.Dept,WO.HotelId,WO.HotelName, WO.HotelCreateDate,WO.HotelType,WO.HotelBrand,WO.HotelTel,WO.HotelBigArea,WO.HotelArea,WO.HotelCityGroup,‘‘ AS HotelCity,WO.Title,WO.SelectTitle,WO.Content,WO.IsAccept,WO.CompensateDept, WO.CompensateAmountType,WO.CompensateAmount,WO.ReferDept,WO.ResponsiblePerson, ResponsiblePersonName,WO.ResponsibleDate,WO.ClosePerson, WO.CloseDate,WO.CatePerson,WO.CateDate,WO.ScorePerson,WO.ScoreDate,WO.CreateUser,WO.CreateDate,WO.UpdateUser,WO.UpdateDate, WO.UserName FROM V_WorkOrder_WSCUser wo WHERE 1=1 AND WO.STATUS=3 AND WO.WorkOrderType=‘T‘ select [语句执行花费时间(毫秒)]=datediff(ms,@d2,getdate())
我执行了5次这段代码,5次结果如下。
第一次执行 | 第二次执行 | 第三次执行 | 第四次执行 | 第五次执行 | |
表联合 | 1983ms | 3266ms | 2530ms | 3796ms | 3076ms |
视图 | 1903ms | 2580ms | 3070ms | 3343ms | 3260ms |
看到这五次执行时间的对比,发现视图对查询效率并没有提高,再看执行计划,也是一样。
所以总结一下,视图对于查询效率的提升相对于联合查询来说,并没有提升,对开发效率有提升。
最后,国庆节中秋节,双节快乐。
时间: 2024-10-30 02:23:22