Я пытаюсь сделать этот код быстрее, он запрашивает базу данных с 200 000 + записей в нем.
Этот код занимает около 10 секунд, и я ищу любую помощь, пытаясь обрезатьпосмотрите, где я ошибся.
Я перепробовал множество подходов, которые я покажу сейчас.
Я пробовал MySQL Commands
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();
}
Я пытался использовать Entity Framework
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 занимает еще 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);
Кто-нибудь может увидеть, как я могу урезать это?Спасибо