Which is faster for checking whether a record exists: SOQL inside a loop or one SOQL query outside the loop? and what happens if you perform DML on 200 records one-by-one inside a loop?
#Apex
5 réponses
A single SOQL query outside the loop is faster and follows Apex best practices. SOQL inside a loop can quickly hit the SOQL governor limit.
Similarly, performing DML one-by-one inside a loop is not recommended. For 200 records, it can consume 200 DML statements and exceed the 150 DML statement limit.
The best approach is to query records once, process them in a loop, and perform DML once using a List. This makes the code bulkified and governor-limit safe.
Thank You.