Skip to main content
Pranjal Budhlakoti (360 Cloud Solutions) 님이 #Apex에 질문했습니다

 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개
  1. 어제 오전 6:34

    Hi @Pranjal Budhlakoti

     

     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.

0/9000