I have an email marketing web application written in MVC 4 and Entity Framework 6. The application stores more than 10K email addresses along with contact information in a sql database on a web farm.
Everything works fine while the database is small, a few hundred records. However, when I fully populate the database, it is painfully slow retrieving and writing each individual record using "Foreach" statements. For some operations it can take up to 14 mins to complete. I have tried to minimize the number of records I work with at any one time, but it make the application clumsy.
I was wondering, are there other methods I could use to speed this up. Perhaps SQL stored procedures, or something on GitHub that would make it easier. If there are, please let me know where I can learn more about it. Any help would be appreciated. Here is one of the queries.
private int AddOrUpdateCampaigns(Campaign campaign
        , IEnumerable<AssignEmailContacts> assignEmailContacts)
{
    DataBaseContext db = new DataBaseContext();
    int TotalAssigned = 0;
    if (assignEmailContacts == null) return(TotalAssigned);
    if (campaign.CampaignId != 0)
    {   
      foreach (var emailContact 
                in assignEmailContacts.Where(a => a.Assigned == false))
      {   
        campaign.EmailContacts.Remove(
                db.EmailContacts.Find(emailContact.EmailContactId));
      }
      foreach (var emailContact 
                in assignEmailContacts.Where(a => a.Assigned))
      {                            
        campaign.EmailContacts.Add(
                db.EmailContacts.Find(emailContact.EmailContactId));
            TotalAssigned += 1;
      }
    }
    return (TotalAssigned);
}
 
     
     
     
    