title: Plugin performance in Microsoft Dynamics CRM 2013/2015
body: <p>Time to leave the shy mode behind and make my first post on stackoverflow. 
  After doing loads of research (plugins, performance, indexes, types of update, friends) and after trying several approaches I was unable to find a proper answer/solution. <br/><br/>
  So if possible I would like to get your feedback/help in a Microsoft Dynamics CRM 2013/2015 plugin performance issue (or coding technique)</p>
  
  <h2>Scenario:</h2>
  
  <p>Microsoft Dynamics CRM 2013/2015<br/>
  2 Entities with Relationship 1:N <br/>
  EntityA <br/>
  EntityB <br/></p>
  
  <p>EntityB has the following columns:<br/>
  Id | EntityAId | ColumnDemoX (decimal) | ColumnDemoY (currency)</p>
  
  <p>Entity A has: 500 records<br/>
  Entity B has: 150 records per each Entity A record. So 500*150 = 75000 records.<br/></p>
  
  <h2>Objective:</h2>
  
  <p>Create a Post Entity A Plugin Update to "mimic" the following SQL command<br/></p>
  
  <pre><code>Update EntityB
  Set ColumnDemoX = (some quantity), ColumnDemoY = (some quantity) * (some value)
  Where EntityAId = (some id)
  </code></pre>
  
  <p>One approach could be:</p>
  
  <pre><code>using (var serviceContext = new XrmServiceContext(service)) 
  {
    var query = from a in serviceContext.EntityASet
                where a.EntityAId.Equals(someId)
                select a;
  
    foreach (EntityA entA in query)
    {
      entA.ColumnDemoX = (some quantity);
      serviceContext.UpdateObject(entA);
    }
  
    serviceContext.SaveChanges();
  }
  </code></pre>
  
  <h2>Problem:</h2>
  
  <p>The foreach for <strong><em>150 records</em></strong> in the post plugin update will take <strong><em>20 secs or more</em></strong>.
  <br/>
  While the <br/>
  Update EntityB Set ColumnDemoX = (some quantity), ColumnDemoY = (some quantity) * (some value) Where EntityAId = (some id)<br/>
  it will take <strong><em>0.00001</em></strong> secs</p>
  
  <h2>Any suggestion/solution?</h2>
  
  <p><br/>
  Thank you all for reading.  <br/>
  H</p>