Code 1
List<String> accNameList = new List<String>{'Acc1','Acc2','Acc3'};
List<Account> accountList = Database.query('SELECT Id FROM Account WHERE Name IN:accNameList');
Code 2
List<String> accNameList = new List<String>{'Acc1','Acc2','Acc3'}
List<Account> accountList = Database.query('SELECT Id FROM Account WHERE Name IN (\''+String.join(accNameList,'\',\'')+'\')')
The two codes above are equivalent (return the same list of accounts)
Beside of using the bind list variable to prevent any flaw of SOQL Injection
Is there a performance reason (or other reason) than the one cited ?
Best Answer
Please, do the following:
That will allow you to check Duration and Heap on the tab Performance Tree. This tab is on the Stack Tree panel.
Do the same for the Code 2 and compare.