In an application I have some Apex code that does quite a lot of computation and saves the results in several objects. If the results have been generated before, the code first deletes the existing objects. On adding some timing code, I was surprised at the distribution of the times, particularly how expensive the deletes are and how cheap the reads (selects) are.
Here is the data (values in milliseconds and dealing with a few dozen objects in each case) for a couple of variations:
The parts are:
- Find: doing a SOQL select to identify the “ids” of the objects to delete
- Delete: a single Apex “delete ids” call
- Calculate: Apex “new” calls and various calculations to populate the objects
- Insert: a single Apex “insert newIds” call to insert the calculated objects