我有一个MVC应用程序,我的主页显示信息的速度非常慢。我认为这可能是我的linq查询。我检查了数据库并运行了类似的SQL,但速度却并不慢。
我将数据从生产环境移到我的开发笔记本电脑上进行测试,并且页面在开始的几分钟内在笔记本电脑上运行非常快,但是如果我继续使用该应用程序超过5分钟,即使在笔记本电脑上,页面也会变得非常慢!其他页面似乎运行良好。
如何解决此问题?
基本上,我想做的是显示当日作业列表和子记录的摘要。
public class Assignment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public string UserName { get; set; }
public DateTime CreatedDate { get; set; }
public string Status { get; set; }
public bool IsArchived { get; set; }
public int SortFlag { get; set; }
public virtual ICollection<Adjustment> Adjustments { get; set; }
}
public class Adjustment
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public int AssignmentID { get; set; }
public string Description { get; set; }
public string UPC { get; set; }
[Display(Name= "LicPlt")]
public int? ExpectedLicensePlateID { get; set; }
[Display(Name = "Product")]
public string ExpectedProductID { get; set; }
[Display(Name = "ExpCDate")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? ExpectedCodeDate { get; set; }
[Display(Name = "ExpQTY")]
public int ExpectedQty { get; set; }
[Display(Name = "LotNumber")]
public string ExpectedLotNumber { get; set; }
[Display(Name = "ExpCool")]
public string ExpectedCOOL { get; set; }
[Display(Name = "LicPlt")]
public int? ActualLicensePlateID { get; set; }
public string ActualProductID { get; set; }
[Display(Name = "CDate")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? ActualCodeDate { get; set; }
[Display(Name = "QTY")]
public int ActualQty { get; set; }
[Display(Name = "LotNumber")]
public string ActualLotNumber { get; set; }
[StringLength(3, ErrorMessage = "Lenght should be 3")]
[Display(Name = "Cool")]
public string ActualCOOL { get; set; }
[Display(Name = "Location")]
public string LocationID { get; set; }
public string UserName { get; set; }
public DateTime UpdatedDateTime { get; set; }
public DateTime ImportedDateTime { get; set; }
public string Status { get; set; }
[StringLength(2, ErrorMessage = "Lenght should be 2")]
[Display(Name = "Reason")]
public string ReasonCodeID { get; set; }
public string Notes { get; set; }
public string ReviewedBy { get; set; }
public DateTime? ReviewedDateTime { get; set; }
public string ReviewNotes { get; set; }
public bool IsCodeDated { get; set; }
public bool IsDescriptionConfirmed { get; set; }
public bool IsCodeDateConfirmed { get; set; }
public virtual Assignment Assignment { get; set; }
public ReasonCode ReasonCode { get; set; }
public string LocationCategory { get; set; }
}
基本上,我试图显示每天的作业,其中包含调整项的小计
public class AssignmentsController : Controller
{
private CycleConterContext db = new CycleConterContext();
// GET: Assignments
public ActionResult Index(DateTime? StartDate)
{
// return View(db.Assignments.ToList());
var detailsGroup = from a in db.Adjustments
group a by a.AssignmentID into g
select new
{
ID = g.Key,
StartingLocation = g.Min(t => t.LocationID),
EndingLocation = g.Max(t => t.LocationID),
TotalLocations = g.Count(t => t.LocationID != null),
TotalLicensePlates = g.Count(t => t.ExpectedLicensePlateID != null),
TotalAdjCompleted = (g.Count(t => t.Status == "C"))
};
string currUser = Utility.CleanedUser();
int isSuper = 0;
var supervisor = db.AppUsers.Where(a => a.ID == currUser && a.RoleType == RoleType.Supervisor).Count() == 1;
var assignmenTotal = new AssignmentUser
{
IsSupervisor = supervisor,
AssignmentTotals = (from a in db.Assignments
where (StartDate.HasValue) ? DbFunctions.TruncateTime(a.CreatedDate) == StartDate : a.IsArchived == false
join
b in detailsGroup on a.ID equals b.ID
select new AssignmentTotals
{
ID = a.ID,
UserName = a.UserName,
Status = a.Status,
ImportedDate = DbFunctions.TruncateTime(a.CreatedDate),
StartingLocation = b.StartingLocation,
EndingLocation = b.EndingLocation,
TotalLocations = b.TotalLocations,
TotalLicensePlates = b.TotalLicensePlates,
TotalAdjCompleted = b.TotalAdjCompleted,
IsSameUser = (currUser == a.UserName ? true : false),
IsArchived = a.IsArchived
}).ToList()
};
ViewBag.Message = TempData["Message"];
return View(assignmenTotal);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
答案 0 :(得分:0)
问题出在linq最小和最大聚合中。不知道为什么还可以,但是数据量可能是多少?
无论如何,我修改了查询并且运行非常快。从最小/最大更改为子查询
var assignmenTotal = new AssignmentUser
{
IsSupervisor = supervisor,
AssignmentTotals = (from a in db.Assignments
where (StartDate.HasValue) ? DbFunctions.TruncateTime(a.CreatedDate) == StartDate : a.IsArchived == false
join b in db.Adjustments on a.ID equals b.AssignmentID
group b by new {a.ID,a.UserName,a.Status,a.CreatedDate,a.IsArchived} into g
select new AssignmentTotals
{
ID = g.Key.ID,
UserName = g.Key.UserName,
Status = g.Key.Status,
ImportedDate = DbFunctions.TruncateTime(g.Key.CreatedDate),
StartingLocation = (db.Adjustments.Where(x=>x.AssignmentID == g.Key.ID).OrderBy(x=>x.LocationID).Select(x=>x.LocationID).FirstOrDefault()),
EndingLocation = (db.Adjustments.Where(x => x.AssignmentID == g.Key.ID).OrderByDescending(x => x.LocationID).Select(x => x.LocationID).FirstOrDefault()),
TotalLocations = g.Count(x => x.LocationID != null),
TotalLicensePlates = g.Count(x => x.ExpectedLicensePlateID != null),
TotalAdjCompleted = g.Count(x => x.Status == "C"),
IsSameUser = (currUser == g.Key.UserName ? true : false),
IsArchived = g.Key.IsArchived
}).ToList()
};