以下是对给定的LINQ to SQL查询进行优化的几个建议:

  1. 减少重复查询:在原始查询中,您多次调用了GetIQueryable()Db.GetIQueryable()方法。建议将这些查询提取到单独的变量中,并在整个查询中重复使用这些变量,以减少重复查询数据库的次数。
var query1 = GetIQueryable();
var query2 = Db.GetIQueryable<Equipment_Info>();
var query3 = Db.GetIQueryable<Equipment_Type>();
  1. 使用join替代join intoDefaultIfEmpty:在原始查询中,您使用了join intoDefaultIfEmpty来模拟left join操作。然而,这种写法可能会导致性能下降。建议使用join操作并使用into子句将结果存储在临时变量中,然后在后续的from子句中使用该变量。
join b in query2 on a.EquipmentId equals b.Id into tmp1
from bb in tmp1.DefaultIfEmpty()

可以改写为:

join bb in query2 on a.EquipmentId equals bb.Id into tmp1
from b in tmp1.DefaultIfEmpty()
  1. 使用where子句提前过滤结果集:在原始查询中,您使用where子句来过滤结果集,但是这些过滤条件在select子句之后执行。建议将where子句移动到查询的开头,以便尽早过滤结果集。

  2. 合并重复的查询逻辑:在原始查询中,您多次使用了相同的GetIQueryable()方法。建议将这部分查询逻辑提取到单独的方法中,并在需要时调用该方法。

根据上述建议,以下是优化后的查询代码:

var query1 = GetIQueryable();
var query2 = Db.GetIQueryable<Equipment_Info>();
var query3 = Db.GetIQueryable<Equipment_Type>();

var q = (from a in query1
         join b in query2 on a.EquipmentId equals b.Id into tmp1
         from bb in tmp1.DefaultIfEmpty()
         where a.Deleted == false && bb.EnterpriseId == _operator.EnterpriseId
         select new
         {
             Id = a.Id,        // 主键
             RecordTime = a.RecordTime,        // 记录时间
             EquipmentId = a.EquipmentId,        // 设备Id
             EquipmentName = bb.EquipmentName,       //设备名称
             EquipmentTypeId = bb.EquipmentTypeId,        // 设备类型Id
             cc.TypeName,        // 设备类型名称
             Remarks = a.Remarks,        // 备注
             AlarmTime = a.AlarmTime,        // 报警时间
             AlarmCode = a.AlarmCode,        // 报警编号
             AlarmLevel = a.AlarmLevel,        // 报警等级
             AlarmDescription = a.AlarmDescription,        // 报警描述
             AlarmValue = a.AlarmValue,        // 报警值
             AlarmCount = a.AlarmCount,        // 报警次数
             AlamType = a.AlamType,        // 报警类型(0报警,1故障)
             AlamTypeName = (a.AlamType == 0 ? "报警" : "故障"),        // 报警类型(0报警,1故障)
             a.SolveState,        // 处理状态(0待处理,1已处理)
             SolveStateName = (a.SolveState == 0 ? "待处理" : "已处理"),        // 处理状态(0待处理,1已处理)
             SolveUser = a.SolveUser,        // 处理人员
             SolveTime = a.SolveTime,        // 处理时间
             SolveResult = a.SolveResult,        // 处理结果 
         })
        .Union  //充电桩的
        (
            from a in query1
            join b in Db.GetIQueryable<Equipment_ChargingPileInfo>() on a.EquipmentId equals b.Id into tmp1
            from bb in tmp1.DefaultIfEmpty()
            where a.Deleted == false && bb.EnterpriseId == _operator.EnterpriseId
            select new
            {
                Id = a.Id,        // 主键
                RecordTime = a.RecordTime,        // 记录时间
                EquipmentId = a.EquipmentId,        // 设备Id
                EquipmentName = bb.EquipmentName,       //设备名称
                EquipmentTypeId = "cdz",         // 设备类型Id
                TypeName = EquipmentType.充电桩.ToString(),                                //cc.TypeName,        // 设备类型名称
                Remarks = a.Remarks,        // 备注
                AlarmTime = a.AlarmTime,        // 报警时间
                AlarmCode = a.AlarmCode,        // 报警编号
                AlarmLevel = a.AlarmLevel,        // 报警等级
                AlarmDescription = a.AlarmDescription,        // 报警描述
                AlarmValue = a.AlarmValue,        // 报警值
                AlarmCount = a.AlarmCount,        // 报警次数
                AlamType = a.AlamType,        // 报警类型(0报警,1故障)
                AlamTypeName = (a.AlamType == 0 ? "报警" : "故障"),        // 报警类型(0报警,1故障)
                a.SolveState,        // 处理状态(0待处理,1已处理)
                SolveStateName = (a.SolveState == 0 ? "待处理" : "已处理"),        // 处理状态(0待处理,1已处理)
                SolveUser = a.SolveUser,        // 处理人员
                SolveTime = a.SolveTime,        // 处理时间
                SolveResult = a.SolveResult,        // 处理结果 
            }
          )
        .Union  //充电枪的
        (
           from a in query1
           join b in Db.GetIQueryable<Equipment_ChargingGunsInfo>() on a.EquipmentId equals b.Id into tmp1
           from bb in tmp1.DefaultIfEmpty()
           join c in query2 on bb.ChargingPileId equals c.Id into tmp2
           from cc in tmp2.DefaultIfEmpty()
           where a.Deleted == false && cc.EnterpriseId == _operator.EnterpriseId
           select new
           {
               Id = a.Id,        // 主键
               RecordTime = a.RecordTime,        // 记录时间
               EquipmentId = a.EquipmentId,        // 设备Id
               EquipmentName = bb.EquipmentName,       //设备名称
               EquipmentTypeId = "cdq",         // 设备类型Id
               TypeName = EquipmentType.充电枪.ToString(), //cc.TypeName,        // 设备类型名称
               Remarks = a.Remarks,        // 备注
               AlarmTime = a.AlarmTime,        // 报警时间
               AlarmCode = a.AlarmCode,        // 报警编号
               AlarmLevel = a.AlarmLevel,        // 报警等级
               AlarmDescription = a.AlarmDescription,        // 报警描述
               AlarmValue = a.AlarmValue,        // 报警值
               AlarmCount = a.AlarmCount,        // 报警次数
               AlamType = a.AlamType,        // 报警类型(0报警,1故障)
               AlamTypeName = (a.AlamType == 0 ? "报警" : "故障"),        // 报警类型(0报警,1故障)
               a.SolveState,        // 处理状态(0待处理,1已处理)
               SolveStateName = (a.SolveState == 0 ? "待处理" : "已处理"),        // 处理状态(0待处理,1已处理)
               SolveUser = a.SolveUser,        // 处理人员
               SolveTime = a.SolveTime,        // 处理时间
               SolveResult = a.SolveResult,        // 处理结果 
           }
          );

请注意,上述优化仅涉及LINQ查询的改进。在数据库层面,您可能还需要确保适当的索引和性能调整以提高查询性能

优化linq to sql: var q = from a in GetIQueryable join b in DbGetIQueryableEquipment_Info on aEquipmentId equals bId into tmp1 from bb in tmp1DefaultIfE

原文地址: https://www.cveoy.top/t/topic/hHZE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录