EF架构~linq模拟left join的两种写法,性能差之千里!
对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行join时直接into到集合变量就可以了,但在赋值时,如果只需要集合的一条记录,那在写法上又会有两种,而这两种写法所产生的性能是相关千里的,下面来看一下.
首先是SQL的左外连接
SELECT [t6].[CourseID] , [t6].[UserID] , [t6].[CourseName] , [t6].[ResourceID] , [t6].[StudyTime] , [t6].[BeginTime] , [t6].[EndTime] , [t6].[value] AS [CategoryID] , [t6].[value2] AS [ClassHour] , [t6].[value3] AS [Percent] , [t6].[test] , [t6].[ID] , [t6].[SmallPicture] FROM dbo.User_Course AS t6 LEFT OUTER JOIN [User_StudyRecord] AS t3 ON t6.UserID = t3.UserID AND t3.ResourceID = t6.ResourceID
当它被翻译成LINQ之后,是分页产生的结果,所以感觉更很乱了,呵呵,(LINQ在翻译SQL时,本来就够乱的,再一分页,用上row_number,更乱了),但结果是一样的,
咱们就不管微软是怎么翻译的了
我们重要是看一下,实现时LINQ代码的写法
第一种,性能低下的,如果结果为20条记录,那它需要多连接20次
var linq = from _data in new User_Course(UnitOfWork).GetEntities() let list = new Res_Item(UnitOfWork).GetEntities().Select(t => new Entity.Res_Item { ID = t.ID, SmallPicture = t.SmallPicture, }).Where(i => i.ID == _data.ResourceID) let list2 = new User_StudyRecord(UnitOfWork).GetModel().Select(r => new Entity.User_StudyRecord { StudyContent = r.StudyContent, UserID = r.UserID, Res_ItemID = r.Res_ItemID, }).Where(i => i.Res_ItemID == _data.ResourceID && i.UserID == _data.UserID) from record in list2.DefaultIfEmpty() where _data.CategoryID != (int)CustomEnum.CategoryType.BroadcastProgram && _data.CategoryID != (int)CustomEnum.CategoryType.AskRoom select new Entity.User_Course { CourseID = _data.CourseID, UserID = _data.UserID, CourseName = _data.CourseName, ResourceID = _data.ResourceID, StudyTime = _data.StudyTime, BeginTime = _data.BeginTime, EndTime = _data.EndTime, CategoryID = _data.CategoryID ?? 2, Res_Item = list.FirstOrDefault(), ClassHour = _data.ClassHour ?? 0, Percent = _data.Percent ?? 0, Prev_ResourceName = record == null ? string.Empty : record.StudyContent, };
第二种,性能较好的
总结:对于第一种性能较差的写法产生的结果,类似于LINQ本身提交的延时加载,即结果集中,每条记录都去查询数据库,它不会一次将数据获出来,这种作用在特定场合是
有它的好处的,但不适用于所有,当返回结果集比较大时,不应该使用延时加载!