0

一切正常,我只是在尝试进一步优化它时遇到了麻烦。存储库方法就像我想要的那样工作,它既好又快。当您执行 Download Hunters 控制器操作时,所有 Temp 分配都会变得有点慢。特别是 HunterFields 区域,它是一个将 Hunters 连接到 Fields 的 Join Table。(这些是 JobHunters 和他们的类别,如果它有帮助的话)。我正在与 100k 猎人打交道,每个猎人在类别中几乎都有 3 到 14 个字段。

存储库方法

public List<Hunter> FetchAllHunters(Member user, DateTime? BeginDate = null, DateTime? EndDate = null, IEnumerable<int> FieldIDs = null)
{
StringBuilder SQLQuery = new StringBuilder();

// This main Query stringbuilder will be appended and formatted using checks against certain params. 
SQLQuery.AppendFormat("Select * FROM Hunters WHERE CompanyID = '{0}'", user.CompanyID);

if (BeginDate != null && EndDate != null) /* I like Between, that was I don't have to do Enddate >= CreateDate >= BeginDate */
    SQLQuery.Append(" AND Hunters.CreatedDate BETWEEN @BeginDate and @EndDate");

if (FieldIDs != null && FieldIDs.Count() > 0 && !FieldIDs.Contains(0))
    SQLQuery.AppendFormat(" AND EXISTS (SELECT 1 FROM [dbo].[FieldsHunters] AS [FieldHunt] WHERE ([Hunters].[ID] = [FieldHunt].[HunterID]) AND ([FieldHunt].[FieldID] IN ({0})))", String.Join(", ", FieldIDs));

return dbContext.Hunters.SqlQuery(SQLQuery.ToString(),
    new SqlParameter("@BeginDate", BeginDate),
    new SqlParameter("@EndDate", EndDate)).AsNoTracking().ToList();    
}

控制器(问题出在哪里)

public ActionResult DownloadHunters(HunterStatsViewModel VM)     
{

List<Hunter> Hunters = db.FetchAllHunters(user, VM.BegnDate, VM.EndDate, VM.FieldIDs);

List<List<string>> Temp = new List<List<string>>(){
    new List<string>() { "Hunter ID" } 
};
Temp[0].AddRange(Hunters.Select(x => x.ID.ToString()).ToList());

if (VM.ID == true)
    Temp = AddColumnRows(Temp, "ID", Hunters.Select(x => x.ID.ToString()).ToList());

if (VM.HunterName == true)
    Temp = AddColumnRows(Temp, "Hunter Name", Hunters.Select(x => x.FullName).ToList());

if (VM.Address == true)
    Temp = AddColumnRows(Temp, "Address", Hunters.Select(x => x.Address1).ToList());

if (VM.ContactPhone == true)
    Temp = AddColumnRows(Temp, "Home", Hunters.Select(x => x.Phone1).ToList());

if (VM.WorkPhone == true)
    Temp = AddColumnRows(Temp, "Work", Hunters.Select(x => x.WorkPhone).ToList());

if (VM.MobilePhone == true)
    Temp = AddColumnRows(Temp, "Cell", Hunters.Select(x => x.Phone2).ToList());

if (VM.ContactFax == true)
    Temp = AddColumnRows(Temp, "Fax", Hunters.Select(x => x.Fax).ToList());

if (VM.ContactEmail == true)
    Temp = AddColumnRows(Temp, "Email", Hunters.Select(x => x.Email).ToList());

/* This area takes 45 seconds! */
if (VM.EmploymentField == true)
    Temp = AddColumnRows(Temp, "Employment Field", Hunters.Select(x => String.Join(", ", x.Fields.Select(q => q.Field.Name).ToList())).ToList());

if (VM.Citizenship == true)
    Temp = AddColumnRows(Temp, "Status", Hunters.Select(x => x.HunterStatus != null ? x.HunterStatus.Name : " ").ToList());

if (VM.AvailDate == true)
    Temp = AddColumnRows(Temp, "Availability Date", Hunters.Select(x => String.Format("{0}/{1}", x.AvailableStartMonth.ToString(), x.AvailableStartYear.ToString())).ToList());


/* Takes about 5-10 seconds */
if (VM.Licensures == true)
    Temp = AddColumnRows(Temp, "Licensures", Hunters.Select(x => String.Join(", ", x.Tempes.Where(y => y.Licensed == true).Select(z => z.Tempe.Code).ToList())).ToList());

if (VM.Education == true)
    Temp = AddColumnRows(Temp, "Education", Hunters.Select(x => x.EducationDescription).ToList());

if (VM.Gender == true)
    Temp = AddColumnRows(Temp, "Gender", Hunters.Select(x => (x.Gender == true ? "Male" : (x.Gender == false ? "Female" : "N/A"))).ToList());

// Date doesn't need a check. 
Temp = AddColumnRows(Temp, "Date Poste", Hunters.Select(x => x.CreatedDate.ToString()).ToList());

TempData["Temp"] = Temp;
TempData["FileName"] = "HunterDownloads";

return RedirectToAction("Download");
}

打野方法

public List<List<string>> AddColumnRows(List<List<string>> Stats, string field, List<string> datas)
{
Stats.Add(new List<string>() { field });
Stats[Stats.Count() - 1].AddRange(datas);
return Stats;
}

下载视图

@model HunterStatsViewModel
@{
/* I'm being bad with tempdata and reassigning the data. Screw teh rules I have tea and crumpets. */
TempData["FileName"] = Model.FileName;   
TempData["ReportRange"] = Model.Report;
TempData["Temp"] = Model.Temp; 

}

@{ 
Response.ClearContent();
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", Model.FileName));
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

Response.Write("<table width='100%' border='1'>");
if (Model.Report != null)
{
    Response.Write(String.Format("<tr><td colspan='2'>{0}<td></tr>>", Model.Report)); // In case they want to add a report header or such. 
}
for (int j = 0; j < Model.Temp[0].Count(); j++)
{
    Response.Write("<tr>");
    for (int i = 0; i < Model.Temp.Count(); i++)
    {
        Response.Write(string.Format("<td>{0}</td>", Model.Temp[i][j]));
    }
    Response.Write("</tr>");       
}

Response.Write("</table>");
Response.End();
}

我一直在寻找几天来优化它的方法!甚至做了很多测试。但仍然需要大约 60 秒才能将统计数据很好地存储在 excel 文件中。我什至对 15 秒感到满意!谢谢你,强大的 SOers!

4

0 回答 0