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 ?

  • Using a List of string would be definitely faster as there is it would take some time in constructing String.join
    – Raul
    Mar 13, 2017 at 9:01
  • @RahulSharma Thank you for that note, but want to know if there is a performance reason in the SOQL execution
    – benahm
    Mar 13, 2017 at 9:05
  • 1
    @beham you should use profiling for this scenario. The developer console can give you pretty good information on the performance. Mar 13, 2017 at 9:49
  • 2
    "Code 2" will also fail if one of the names includes a single quote character. Also its pretty difficult to read ad confirm that "Code 2" even adds the quotes correctly. Personally, I would always use the "Code 1" pattern. (Before considering performance, see Premature Optimization for some perspective.)
    – Keith C
    Mar 13, 2017 at 9:57
  • @KeithC yes I know, but I can still, escape the quotes with String.escapteSingleQuote() the purpose of the question is to know if there is a performance reason to use bind variable, I checked the performance see the results in the answers ...
    – benahm
    Mar 13, 2017 at 10:18

2 Answers 2


Please, do the following:

  1. Open your developer console.
  2. Execute on Code 1.
  3. Under the Log tab,
  4. double click on the last execution of code 1.
  5. Debug-> View Log Panels
  6. Select Stack Tree

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.

  • Nice, thanks @carlos, I was looking for the same as well! :)
    – Raul
    Mar 13, 2017 at 10:05
  • You welcome guys. I think it is a very fast and easy way to check performances. Mar 13, 2017 at 10:10

Followed the steps suggested by @Carlos (in his answer) to compare the performance of the two codes on the Developer Console

Below the results

(Tested using lists containing many values the Code 2 is always faster but uses more Heap space)

Code 1

Code 1

Code 2

Code 2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .