You need to sign in to do that
Don't have an account?
Alper 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.
Thanks,
Alper Ozdamar
Senior Software Engineer
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
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
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.
I have gone through your code then I realized there is no need to check null condition because you already check size() > 0.
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
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.
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
Here is my code:
Still insert validSlots; line19 takes 7500 milliseconds.
So I think this is Salesforce problem. Salesforce Apex inserts are really slow.
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