4

I'm writing APEX REST API that creates OpportunityLineItem, Product2, Quote objects, updates Opportunity etc. based on data in JSON format received from external system.

During testing I experienced some performance glitches so decided to profile my code and isolate the problems.

I came to conclusion that simple DELETE statement which deletes previously associated Opportunity Line Items takes over 80% of execution time! It takes over 6 seconds to delete 200 rows.

There're no triggers attached to OppLineItem, no Flows, as testing is done in plain development environment.

Is this expected timing on Force.com platform to delete 200 rows? I'm coming from .NET/SQL projects so this looks very slow to me.

Here's my sample code:

public static void deleteOpportunityLineItems(String opportunityId, List<String> messages){
    List<OpportunityLineItem> oppLineItems = [select ID from OpportunityLineItem where OpportunityId = :opportunityId];
    if(!oppLineItems.isEmpty()){
        delete oppLineItems;
        messages.add('Line items successfully deleted.');            
    }                
}

Also, here's the screenshot of Developer Console Log view:

timeline

Do you have any idea why this operation is performing so slow? Am I missing something?

Thank you!

  • Deleting OLI will automatically cause Oppo.amount to be recalculated. If there are triggers or workflows on Oppo, their time is included in the transaction time. – cropredy Jan 15 '16 at 4:27
  • That's the thing - no triggers or active flows/rules. E.g. inserting 50-100 Product2 or OLI records take significantly less time, there's definitely something weird with delete operation. – Nikola J. Jan 15 '16 at 13:02
3

I think this is standard time that SFDC take to perform delete operation and there is nothing wrong with your code.

I have tested this with following code

List<OpportunityLineItem> loli = new List<OpportunityLineItem>();
for (integer i=0; i<200; i++){
    OpportunityLineItem old = new OpportunityLineItem();
    old.Description ='Description'+i;
    old.OpportunityId = '0069000000VZsuRAAT';
    old.PricebookEntryId = '01u90000006FU8YAAW';
    old.UnitPrice = 10;
    old.Quantity = 7;
    loli.add(old);
}    
insert loli;

And then deleted all line items by:

List<OpportunityLineItem> loli = [select id from OpportunityLineItem where OpportunityId = '0069000000VZsuRAAT'];
delete loli;

Using anonymous block and it took around 6 seconds to perform that operations(just like yours). So i don't think anything wrong in the code.

DB is not included in your CPU time (which is 10 sec) so we should be fine with it.

2

You have done good enough research to be able to log a case with Salesforce, especially if the issue is reproducible in a clean org.

I used to work for Salesforce Developer Support so I can tell you they can run an SQL trace on the database and pull hourly performance reports as well; with these 2 combined they can tell if the internal query and its path is inefficient, and if so, they could log a bug with the database team. I doubt something like this would be fixed any time soon, but 6 seconds is indeed a long time to delete database rows.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.