我正在尝试使此代码更快,它查询的数据库中包含200,000条记录。
这段代码需要大约10秒钟的时间来执行,我正在寻找任何帮助来缩小它的规模,看看我哪里出了问题。
我已经尝试了许多方法,现在将显示这些方法。
我已经尝试过MySQL命令
using (MySqlConnection con = new MySqlConnection(connectionstring))
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
con.Open();
var daysago = DateTime.Now.AddDays(-7);
cmd.CommandText = @"SELECT job_ref,UniqNo,volumes_env,postout_deadline from jobs where recieved_data >= """ + daysago.ToString("yyyy-MM-dd HH:mm:ss") + @""" and lsm_status is null and despatched_time is null";
var reader = cmd.ExecuteReader();
int x = 0;
while (reader.Read())
{
jobrefss.Add(reader["job_ref"].ToString());
Tester.Add(new SpecialClassTest { job_ref = reader["job_ref"].ToString(),
UniqNo = reader["UniqNo"].ToString(),
volumes_env = (int)reader["volumes_env"],
postout_deadline = (DateTime)reader["postout_deadline"] });
}
jobrefss = jobrefss.Distinct().ToList();
jobrefss = jobrefss.Where(z => !z.ToUpper().Contains("C4") &&
!z.ToUpper().Contains("ARC") &&
!z.ToUpper().Contains("EMAIL") &&
!z.ToUpper().Contains("EBILL") &&
!z.ToUpper().Contains("POD") &&
!z.ToUpper().Contains("GALLAHER") &&
!z.ToUpper().Contains("SCAN") &&
!z.ToUpper().Contains("ARCHIVE") &&
!z.ToUpper().Contains("SHELL") &&
!z.ToUpper().Contains("IRISH") &&
!z.ToUpper().Contains("INTER") &&
!z.ToUpper().Contains("BREAK") &&
!z.ToUpper().Contains("1ST") &&
!z.ToUpper().Contains("HAND") &&
z != "AAH_Monthly_StatColDup_Cut" &&
z != "APH_Inv_Cut" &&
z != "APH_Stat_Cut" &&
z != "BSS_BuckHickConsInv_Cut" &&
z != "BSS_BuckHickInv_Cut" &&
z != "BSS_BuckHickStat_Cut" &&
z != "Marstons_Inv_Cut" &&
z != "RexelGroup_M3Inv_Cut" &&
z != "RexelGroup_M3Stat_Cut" &&
z != "Schneider_Inv_Cut" &&
z != "Schneider_Stats_Cut" &&
z != "XLN_InvC3_ColDup_Cut" &&
z != "XLN_ManualCOPYInvoices_Cut" &&
z != "XLN_ManCOPYInv_ColDup_Cut" &&
z != "AAH_Mon_EntStColDup_Cut_HAN" &&
z != "OBT_Inv_Cut" &&
z != "XLN_InvC7_ColDup_Cut" &&
z != "Marstons_MBCStat_Cut" &&
z != "XLN_GiroC7_ColDup_Cut"
).ToList();
}
我尝试使用实体框架
var jobRefs = context.jobs.Where(j => j.LSM_Status == null &&
j.despatched_time == null &&
!j.job_ref.Contains("C4") &&
!j.job_ref.Contains("Arc") &&
!j.job_ref.Contains("Email") &&
!j.job_ref.Contains("Ebill") &&
!j.job_ref.Contains("POD") &&
!j.job_ref.Contains("Gallaher") &&
!j.job_ref.Contains("scan") &&
!j.job_ref.Contains("Archive") &&
!j.job_ref.Contains("shell") &&
!j.job_ref.Contains("irish") &&
!j.job_ref.Contains("inter") &&
!j.job_ref.Contains("break") &&
!j.job_ref.Contains("1st") &&
!j.job_ref.Contains("hand") &&
!j.job_ref.Contains("Hand") &&
j.job_ref != "AAH_Monthly_StatColDup_Cut" &&
j.job_ref != "APH_Inv_Cut" &&
j.job_ref != "APH_Stat_Cut" &&
j.job_ref != "BSS_BuckHickConsInv_Cut" &&
j.job_ref != "BSS_BuckHickInv_Cut" &&
j.job_ref != "BSS_BuckHickStat_Cut" &&
j.job_ref != "Marstons_Inv_Cut" &&
j.job_ref != "RexelGroup_M3Inv_Cut" &&
j.job_ref != "RexelGroup_M3Stat_Cut" &&
j.job_ref != "Schneider_Inv_Cut" &&
j.job_ref != "Schneider_Stats_Cut" &&
j.job_ref != "XLN_InvC3_ColDup_Cut" &&
j.job_ref != "XLN_ManualCOPYInvoices_Cut" &&
j.job_ref != "XLN_ManCOPYInv_ColDup_Cut" &&
j.job_ref != "AAH_Mon_EntStColDup_Cut_HAN" &&
j.job_ref != "OBT_Inv_Cut" &&
j.job_ref != "XLN_InvC7_ColDup_Cut" &&
j.job_ref != "Marstons_MBCStat_Cut" &&
j.job_ref != "XLN_GiroC7_ColDup_Cut"
);
var jobrefss = jobRefs.Select(z => z.job_ref);
但是它仍然花费太长时间。
(以下代码将在之后执行)
var UpdatedRefs = context.customerslas.Where(c => jobrefss.Contains(c.job_ref) &&
(c.invoiced == 1 || c.invoiced == 2) &&
c.active == 1)
.Select(c => c.job_ref)
.ToList();
var FinalRefs = Tester.Where(f => UpdatedRefs.Contains(f.job_ref))
.OrderBy(f => f.job_ref)
.ToList();
有人可以看到我可以修剪一些脂肪并节省一些时间的地方吗?
编辑
通过在第一个sql查询中完成我的工作中的繁琐部分,我设法节省了一些时间。
cmd.CommandText = @"SELECT job_ref,UniqNo,volumes_env,postout_deadline from jobs where recieved_data >= """ + daysago.ToString("yyyy-MM-dd HH:mm:ss") + @""" and lsm_status is null and despatched_time is null and job_ref not like '%c4%' and job_ref not like '%Arc%' and job_ref not like '%email%' and job_ref not like '%ebill%' and job_ref not like '%pod%' and job_ref not like '%gallaher%' and job_ref not like '%Scan%' and job_ref not like '%Archive%' and job_ref not like '%shell%' and job_ref not like '%irish%' and job_ref not like '%inter%' and job_ref not like '%break%' and job_ref not like '%1st%' and job_ref not like '%hand%' and job_ref != 'AAH_Monthly_StatColDup_Cut' and job_ref != 'APH_Inv_Cut' and job_ref != 'APH_Stat_Cut' and job_ref != 'BSS_BuckHickConsInv_Cut' and job_ref != 'BSS_BuckHickInv_Cut' and job_ref != 'BSS_BuckHickStat_Cut' and job_ref != 'Marstons_Inv_Cut' and job_ref != 'RexelGroup_M3Inv_Cut' and job_ref != 'RexelGroup_M3Stat_Cut' and job_ref != 'Schneider_Inv_Cut' and job_ref != 'Schneider_Stats_Cut' and job_ref != 'XLN_InvC3_ColDup_Cut' and job_ref != 'XLN_ManualCOPYInvoices_Cut' and job_ref != 'XLN_ManCOPYInv_ColDup_Cut' and job_ref != 'AAH_Mon_EntStColDup_Cut_HAN' and job_ref != 'OBT_Inv_Cut' and job_ref != 'XLN_InvC7_ColDup_Cut' and job_ref != 'Marstons_MBCStat_Cut' and job_ref != 'XLN_GiroC7_ColDup_Cut'";
这个大命令现在只需要花费一秒钟,而不是花费很多,现在的问题是我的Linq / Entity Framework部分需要花4-5秒才能运行
var UpdatedRefs = context.customerslas.Where(c => jobrefs2s.Contains(c.job_ref) &&
(c.invoiced == 1 ||
c.invoiced == 2) &&
c.active == 1)
.Select(c => c.job_ref);
var FinalRefs = Tester.Where(f => UpdatedRefs.Contains(f.job_ref))
.OrderBy(f => f.job_ref);
有人能看到我可以减少这一点的方法吗?谢谢