Improve performance while updating entities in CRM 4.0 using C#

建议的答复 Improve performance while updating entities in CRM 4.0 using C#

  • 2012. május 2. 12:31
     
      Kódot tartalmaz

    I have a mass update application that updates the record of a particular entity, by updating some of it's fields based on some logic. I am using TargetUpdate for the record updating because I have a workflow attached to the update of the entity which I don't want to fire (in case I use normal update). But it's taking forever to update those records. Currently it takes around 1.5 hour to update 1K records. In actual scenario, it would need to update around 10K records in a time span of 5-6 hours maximum daily(nightly job). Is there any way I can improve the performance.

    Centity newentityobect;
    
    newentityobect= new Centity()
    {
    	asp_field1 = new Key()
    	{
    		Value = new Guid(field1Value)
    	},
    	asp_field2 = field2Value,
    	asp_field3 = field3Value,
    	asp_field3 = field4Value
    };
    
    TargetUpdateCentity objx = new TargetUpdateCentity();
    objx.Centity = newentityobect;
    UpdateRequest updateCentity = new UpdateRequest();
    updateCentity.Target = objx;
    UpdateResponse updatedCentity = (UpdateResponse)crmUpdateObject.Execute(updateCentity);

    The above is a small code snippet I am using to update records.

Az összes válasz

  • 2012. május 2. 13:50
     
     Javasolt válasz

    Hi,

    I will have two recommendations. 

    Make sure that crmUpdateObject is outside of your loop. You should only once initialize the crmUpdateObject and use it inside your loop and dispose when you finish.(This will not make a significant performance improvement but may help it to be a little faster.)

    My second recommendation will be to update tables directly from sql server. I know, it is not supported solution by CRM but it will definitely be faster. Only disadvantage is, it is not going to fire any plugins or workflows which as I understand you already dont want to fire anything.

    Hope this helps,


    Oğuz Erdeve

    • Válasznak javasolta: Oğuz Erdeve 2012. május 2. 13:50
    •  
  • 2012. május 2. 14:07
     
     

    Hi Oğuz Erdeve,

    Thanks for your response.

    1. I am initializing the crm object outside the loop, so it's initialized only once and I am using it inside the loop.

    2. I am not allowed to access the SQL server directly, which is why I am using C# and updating the records.

    Is there anything else I could do to improve the performance?

  • 2012. május 2. 14:12
     
     

    Is there plug in running on update of this entity? if yes you could try disbling it to see if this improves the performance.


    MayankP
    My Blog
    Follow Me on Twitter

  • 2012. május 2. 14:35
     
     

    Hi MayankP,

    There is indeed a plug in, but it's not getting triggered as I am using the Target Update (not normal update).

  • 2012. május 2. 14:45
     
     

    why you think it is not getting triggered? I guess even if you use target update all plug ins and workflow gets executed.


    MayankP
    My Blog
    Follow Me on Twitter

  • 2012. május 2. 15:06
     
     Javasolt válasz

    I dont think there is any difference between targetupdate and update methods in CRM framework. Both will fire any plugins and workflows attached to the entity that is updated.



    Oğuz Erdeve

    • Válasznak javasolta: Oğuz Erdeve 2012. május 2. 15:06
    •  
  • 2012. május 3. 5:23
     
     

    Hi MayankP and Oğuz Erdeve,

    My bad, workflow abd plugins do get fired for Target Update, had a bit of misconception about it. My idea says that Target Update updates only those fields I am trying to update contrary to normak update where all field are updated. But here in this scenario, there are two workflows associated with the entity I am using but they won't be triggered as they are based on other field values. So, I suppose, there isn't any performance issue due to the plugin/workflow.

  • 2012. május 3. 11:15
     
     

    If a plugin is registered on an update message it is indeed always fired regardless of how you update the record. Even if the plugin does not perform any actions it will be initialized and this will have an impact on the performance (probably a minor one but still). You can filter when it is fired using the filtering attributes. This will check if the update image contains one of the filtering attributes and only execute if it does, avoiding the intialization performance hit if it does not. Note that this only check if the attribute is present in the update image not if the value has changed from the stored value.

    Disabling the plugins if they do not get fired as MayankP suggest may also be a good idea. This avoid the need for CRM to check if they need to be executed in the first place. You can do this using the CRM web service so it is part of the process.

    A last suggestion to increase the processing speed is to use multiple threads. If you have 10k of records split the load according to the number of CPUs on the CRM server and let each thread execute one set. This will improve the performance (not 4x as fast for 4 threads but still). Important: Each thread must use its own crm webservice.


    Patrick Verbeeten
    www.patrickverbeeten.com
    www.wavextend.com