function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alper OzdamarAlper Ozdamar 

Apex insert takes too much time. Why?

Hello,
We have a problem while we are inserting small bunch of data to Salesforce via Apex code. It takes 7500 milliseconds to insert it to database. Is this normal? Normally in Java you can insert 67 records into database less than 100 milliseconds. Are we doing something wrong? What is the best approach? 

Here is the code that where we insert Slot objects.  
//validSlots has only 67 records. But takes 7500 ms
if(validSlots != null && validSlots.size() > 0) {
                insert validSlots;
}

Thanks,

Alper Ozdamar
Senior Software Engineer


 
Satya Prakash ChoudharySatya Prakash Choudhary
HI Alper,

If alread cheking the size > 0, why still checking the null condition. Can you once try removing the null condition check in IF block.

Also, you can check the debug log, if the INSER statement (line 3) taking the time, or line 2.

Please let me know if above help.

Thanks,
Satya
Alper OzdamarAlper Ozdamar

Hello Satya,

This is the best practice you can't call size() method without being sure that object(validSlots) is not null. Because if validSlots is null and if you call it's method without null check you will get an NullPointerException. 

And also I already calculated the line3 taking the time not line2. line2 takes 0 ms. 

Deepali KulshresthaDeepali Kulshrestha
Hi Alper,
 
I have gone through your code then I realized there is no need to check null condition because you already check size() > 0.
Check below code

if(validSlots.size() > 0) {
                insert validSlots;
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Alper OzdamarAlper Ozdamar
Hello gals,

This performance problem/topic is not related with that size() check. Ok? Some one should tell me the reason that why it is taking to much time to insert only 67 records. 
Ajay K DubediAjay K Dubedi
Hi Alper,

    if(validSlots != null && validSlots.size() > 0) 
    {
        insert validSlots;
    }
    
    If you are using a nested loop then I think that takes to much time during insertion. 
    Please share your code it might give us some insight of your code

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Alper OzdamarAlper Ozdamar
Hello Ajay,

Here is my code: 
 
//this takes 7800 ms.
     public static void createAvailabilitySlots(List<Counselor_Availability_Definition__c> availabilityList) {
        
        long startTime = system.currentTimeMillis();
        System.debug('Test.createAvailabilitySlots.startTime:'+startTime); 
        
        //createSlots takes 209 ms.
        List<Counsellor_Availability__c> slotList = createSlots(availabilityList);
        List<Counsellor_Availability__c> validSlots;
        Boolean hasValidSlots = false;
        if(slotList != null) {
        	//this takes 46 ms. 
        	validSlots = validateOverlappingSlotsForCoach(slotList,availabilityList);
            
            long insertStartTime = system.currentTimeMillis();
            System.debug('Test.validSlots.size():'+validSlots.size()); 
            //this takes 7509 ms.            
            if(validSlots != null && validSlots.size() > 0) {
                insert validSlots;
            }
			long insertEndTime = system.currentTimeMillis();            
            System.debug('Test.insert validSlots.duration:'+(insertEndTime-insertStartTime)); 
            
            long forStartTime = system.currentTimeMillis();
        	System.debug('Test.For.availabilityList.startTime:'+startTime); 
            //if an availability does NOT have even one valid slot, throw an error.
            for(Counselor_Availability_Definition__c availability: availabilityList) { //This for takes only 11 ms.
                hasValidSlots = false;
                for(Counsellor_Availability__c validSlot : validSlots){
                    if(availability.Id == validSlot.Availability__c){
                        hasValidSlots = true;
                    }
                }
                if(hasValidSlots == false){
                    availability.addError('You have overlapping slots.');
                }
            }
            long forEndTime = system.currentTimeMillis();
        	System.debug('Test.For.availabilityList.endTime:'+forEndTime); 
            System.debug('Test.For.availabilityList.duration:'+(forEndTime-forStartTime));
        }
         
        long endTime = system.currentTimeMillis();
        System.debug('Test.createAvailabilitySlots.endTime:'+endTime);
        System.debug('Test.createAvailabilitySlots.duration:'+(endTime-startTime));         
   
     }




 
Alper OzdamarAlper Ozdamar
By the way, even I know it is not related but I removed this line completely to make all of you happy. = > if(validSlots != null && validSlots.size() > 0) 
Still   insert validSlots; line19 takes 7500 milliseconds.
So I think this is Salesforce problem. Salesforce Apex inserts are really slow.  
 
Satya Prakash ChoudharySatya Prakash Choudhary
It's hard to accept that Salesforce Apex Insert is slow and take time for just this object 'Counsellor_Availability__c' . We need to narrow down here:
Can try below?

Create a CSV file with same set of data (which is getting inserted from apex) and try to Insert using dataloader/workbench. If possible make any available trigger/process builder/workflow inactive on the object. 

Their is some thing else which is consuming the time which is getting invoked on the insert operation is happeing, need to identify that.

Thanks & Regards,
Satya