我正在使用MVC5和EF6来开发应用程序。我正在使用SQL Server Express作为数据库。 我在数据库中有两个表/实体。
我的VehicleLog表目前有大约20K的记录,从特定车辆的表中获取数据大约需要80秒。 例如:我尝试获取最后一条记录以显示车辆的当前状态(即移动或停止),并且需要超过1分半钟。
车辆日志表中的记录数量会随着时间的推移而增加。
当我尝试使用Server explorer打开表时,它会在5-10秒内显示所有数据。 任何人都可以帮助我在页面上快速加载详细信息。
感谢阅读和关注这个问题。
我的代码:
public ActionResult Dashboard()
{
ApplicationUser au = db.Users.Find(currentUser);
var myVehicles = from vehicle in au.Vehicles.ToList()
where vehicle.License.ExpiryDate >= DateTime.Now && !vehicle.IsDeleted
select new CurrentVehicleStatus
{
VehicleName = vehicle.Name,
DriverName = vehicle.Driver != null ? vehicle.Driver.Name : "No driver",
DriverId=vehicle.Driver != null ? vehicle.Driver.DriverId : 0,
VehicleId = vehicle.VehicleId,
VehicleStatus = GeoUtils.GetStatusOf(vehicle.GsmDeviceLogs.Last())
};
return PartialView("Dashboard", myVehicles);
}
public static VehicleStatus GetStatusOf(GSMDeviceLog deviceLog)
{
VehicleStatus currentStatus = VehicleStatus.Stop;
if (deviceLog != null)
{
//Considering DigitalInputLevel1 as Ignition. Not a correct way to do it as DigitalInputLevel1
//is device dependent. Must change in future.
if (deviceLog.DigitalInputLevel1)
currentStatus = VehicleStatus.Idle;
if (deviceLog.DigitalInputLevel1 && deviceLog.Speed > ZERO_SPEED)
currentStatus = VehicleStatus.Moving;
else if (!deviceLog.DigitalInputLevel1 && deviceLog.Speed >= ZERO_SPEED)
currentStatus = VehicleStatus.Towed;
if ((DateTime.Now - deviceLog.DateTimeOfLog).TotalMinutes > 2)
currentStatus = VehicleStatus.Unreachable;
}
else
currentStatus = VehicleStatus.Unreachable;
return currentStatus;
}
如果我评论最后一行(VehicleStats = ....)页面的加载时间低于1秒。但如果它在2分钟左右发表评论。
型号:
public class Vehicle
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VehicleId
{ get; set; }
[Display(Name = "Vehicle name")]
[StringLength(100)]
public String Name
{ get; set; }
[Required]
public String VehicleType
{ get; set; }
[Required]
[StringLength(20)]
[Display(Name = "Registration no.")]
public String RegNo
{ get; set; }
[Required]
[StringLength(100)]
public String Manufacturer
{ get; set; }
[StringLength(20)]
[Display(Name = "Model or Year")]
public String Year
{ get; set; }
[StringLength(100)]
[Display(Name = "Service provider")]
public String ServiceProvider
{ get; set; }
[DataType(DataType.Date)]
[Display(Name = "Insurance date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime InsuranceDate
{ get; set; }
[DataType(DataType.Date)]
[Display(Name = "Last serviced on")]
public DateTime LastServicedOn
{ get; set; }
[Display(Name = "Last serviced at (km)")]
public int LastServicedAt
{ get; set; }
[Display(Name = "Next service at (km)")]
public int NextServiceAt
{ get; set; }
[DataType(DataType.Date)]
[Display(Name = "PUC expiry date")]
public DateTime PUCExpiryDate
{ get; set; }
[Display(Name = "Vehicle Ownership document")]
[DataType(DataType.ImageUrl)]
public virtual List<OwnershipPaper> OwnershipPapers
{ get; set; }
//[Display(Name = "Vehicle status")]
//public VehicleStatusType VehicleStatus
//{ get; set; }
[Display(Name = "Target Utilization (km) per day")]
public int TargetUtilizationPerDay
{ get; set; }
public virtual Driver Driver
{ get; set; }
[Display(Name = "Vehicle Group")]
[Required]
public virtual VehicleGroup VehicleGroup
{ get; set; }
public string IMEI
{ get; set; }
[Display(Name="Fuel tank capacity")]
[Required]
public double FuelTankCapacityLitres
{ get; set; }
public virtual License License { get; set; }
public virtual ICollection<GSMDeviceLog> GsmDeviceLogs { get; set; }
public virtual Policy Policy { get; set; }
public virtual ApplicationUser User { get; set; }
public bool IsDeleted { get; set; }
}
public class GSMDeviceLog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int GSMDeviceLogId { get; set; }
public string IMEI { get; set; }
public string Message { get; set; }
public string ProfileName { get; set; }
public bool GPSStatus { get; set; }
public int SignalStrength { get; set; }
public DateTime DateTimeOfLog { get; set; }
//public string TimeOfLog { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public float Altitude { get; set; }
public float Speed { get; set; }
public float Direction { get; set; }
public int NoOfSatelite { get; set; }
public float GPSPositionAccuracyIndication { get; set; }
public float MilageReading { get; set; }
public string Cell { get; set; }
public float AnalogInputVoltage1 { get; set; }
public float AnalogInputVoltage2 { get; set; }
public float AnalogInputVoltage3 { get; set; }
public float AnalogInputVoltage4 { get; set; }
public bool DigitalInputLevel1 { get; set; }
public bool DigitalInputLevel2 { get; set; }
public bool DigitalInputLevel3 { get; set; }
public bool DigitalInputLevel4 { get; set; }
public bool DigitalOutputLevel1 { get; set; }
public bool DigitalOutputLevel2 { get; set; }
public bool DigitalOutputLevel3 { get; set; }
public bool DigitalOutputLevel4 { get; set; }
[Display(Name="Address")]
public string Location { get; set; }
public int InfoNumber { get; set; }
//Reperesent harsh accelration and deaccelration
public bool HarshDetecation { get; set; }
//RFID Tag Number
[StringLength(15)]
public string RFID { get; set; }
//public virtual Policy Policy { get; set; }
public virtual ICollection<Violation> Violations { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
答案 0 :(得分:4)
问题是你对ToList()的调用。 这将评估查询并从数据库中检索每一行。
然后在循环中调用导航属性。这将导致为数据库中的每个项执行新查询。这是运行缓慢的原因。
如果您只是将'ToList()'调用移到'select'语句之后,那么所有问题都将得到解决。
myVehicles = from vehicle in au.Vehicles
where vehicle.License.ExpiryDate >= DateTime.Now && !vehicle.IsDeleted
select new
{
VehicleName = vehicle.Name,
DriverName = vehicle.Driver.Name ?? "No driver",
DriverId= vehicle.Driver == null ? 0 : vehicle.Driver.DriverId,
VehicleId = vehicle.VehicleId,
GsmDeviceLogs = vehicle.GsmDeviceLogs.LastOrDefault()
}.ToList()
.Select({vehicle => new CurrentVehicleStatus
VehicleName = VehicleName,
DriverName = DriverName,
DriverId= DriverId,
VehicleId = VehicleId,
VehicleStatus = GeoUtils.GetStatusOf(GsmDeviceLogs)
});
编辑:将三元运算符更改为空合并以使它们对EntityFramework友好(如果Driver为null则无关紧要,因为这是一个在后台转换为SQL的表达式)。
编辑:看过GeoUtils的代码,更新了答案。它肯定不会在表达式树中解析。此外,更改了对LastOrDefault()的调用。
答案 1 :(得分:2)
您的查询实际上是LINQ to Objects in memory而不是数据库查询(LINQ to Entities),因为源集合是au.Vehicles
,它是内存中的集合。但是,由于延迟加载,涉及多个数据库查询,这些查询加载的数据远远超过您在幕后所需的数据:
au.Vehicles
已延迟加载,即用户au
的所有车辆 - &gt; 1个数据库查询Vehicle.License
已延迟加载 - &gt;每辆车1个数据库查询(满足where
过滤器)Vehicle.Driver
已延迟加载 - &gt;每辆车1个数据库查询(满足where
过滤器)Vehicle.GsmDeviceLogs
已延迟加载,即所有车辆日志 - &gt;每辆车1个数据库查询(满足where
过滤器)所有这些延迟加载查询很可能是性能不佳的原因。
为确保整个内容只是一个只加载所需数据的数据库查询,您必须尽可能多地从DbSet<T>/IQueryable<T>
编写查询。在您的情况下,尤其要避免Find
的{{1}}。它看起来像这样:
ApplicationUser
正如Max所提到的,var myVehicles = db.Users
.Where(u => u.UserName == currentUser)
.SelectMany(u => u.Vehicles)
.Where(v => v.License.ExpiryDate >= DateTime.Now && !v.IsDeleted)
.Select(v => new
{
VehicleName = v.Name,
DriverName = v.Driver != null ? v.Driver.Name : "No driver",
DriverId = v.Driver != null ? v.Driver.DriverId : 0,
VehicleId = v.VehicleId,
LastGsmDeviceLog = v.GsmDeviceLogs
.OrderByDescending(gdl => gdl.CreateDateTime)
.FirstOrDefault()
})
.AsEnumerable()
.Select(x => new CurrentVehicleStatus
{
VehicleName = x.VehicleName,
DriverName = x.DriverName,
DriverId = x.DriverId,
VehicleId = x.VehicleId,
VehicleStatus = GeoUtils.GetStatusOf(x.LastGsmDeviceLog)
});
无法转换为SQL,因此您必须使用GeoUtils.GetStatusOf
结束数据库查询(其内存开销小于AsEnumerable()
)并执行最终ToList()
并在内存中调用Select
。
答案 2 :(得分:0)
它会做很多提取。尝试Max方法,但将LastOrDefault()更改为无法转换为SQL到OrderBy和FirstOrDefault。像这样的东西
myVehicles = from vehicle in au.Vehicles
where vehicle.License.ExpiryDate >= DateTime.Now && !vehicle.IsDeleted
select new
{
VehicleName = vehicle.Name,
DriverName = vehicle.Driver.Name ?? "No driver",
DriverId= vehicle.Driver == null ? 0 : vehicle.Driver.DriverId,
VehicleId = vehicle.VehicleId,
GsmDeviceLogs = vehicle.GsmDeviceLogs.OrderByDescending(l => l.Id).FirstOrDefault()
}.ToList()
.Select({vehicle => new CurrentVehicleStatus
VehicleName = VehicleName,
DriverName = DriverName,
DriverId= DriverId,
VehicleId = VehicleId,
VehicleStatus = GeoUtils.GetStatusOf(GsmDeviceLogs)
});
结果应该是'外部应用'SQL语法。