2

I have a fundamental question about the how number of SOQL queries calulated in Salesforce.

I have inserted 50 Sample Accounts (Sample0, Sample2 ...Sample49) and trying to perform SOQL query:

List<Account> accs = [SELECT Id FROM Account WHERE Name LIKE 'Sam%'];

logs show the below limits:

Number of SOQL queries: 1 out of 100 Number of query rows: 50 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000

Now, when i try to query and delete accounts:

List<Account> accs = [SELECT Id FROM Account WHERE Name LIKE 'Sam%']; DELETE accs;

The logs show:

Number of SOQL queries: 2 out of 100 Number of query rows: 50 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 50 out of 10000

UPDATE: I am executing the code in Developer console with Execute Anonymous Window. Below is the code:

/*list<Account> iacc = new List<Account>(); for(integer i=0; i<50; i++) { iacc.add(new Account(Name = 'Sample' + i)); } insert iacc; */

List<Account> accs = [SELECT Id FROM Account WHERE Name LIKE 'Sam%']; Delete accs;

I would uncomment 'for loop' and insert statement for re-inserting the accounts later (i would comment SOQL and Delete statment that time)

I was expecting Number of SOQL queries to be still 1, but the limits show it as 2. I guess i am missing something fundamental about the behaviour of SOQL or DELETE. Can you please help me in understanding this?

Thanks in Advance

Arun
  • 47
  • 9

1 Answers1

2

I did copy-paste your code snippet on my home dev org and it showed only 1 SOQL for me. I would suggest you to check for:

  • Triggers for this object
  • Processes in process builder for this object (since process builder could run apex)
  • Workflows since they could cause trigger invokations.
  • not entirely sure but...flows?

I would suggest you to create a new dev org and try your code snippet there. It should show you only 1 query.

Edited: the author said that the additional SOQL query was because of before delete trigger(written in comments).

EvAzi
  • 1,041
  • 9
  • 23
  • 1
    also rollup summaries – kurunve Nov 24 '16 at 14:07
  • @kurunve do roll-up summaries count against query limits? O_o – EvAzi Nov 24 '16 at 14:34
  • no, but they may trigger one more update, which can eat query limit) – kurunve Nov 24 '16 at 14:43
  • @kurunve are you sure? Could you please give me a link to it. I thought that roll-up summaries never cause update triggers(and it's logically correct - in other way it would be insane perform trigger every time the child's updated. – EvAzi Nov 24 '16 at 15:25
  • 1
    http://salesforce.stackexchange.com/questions/4531/do-changes-to-rollup-summary-fields-cause-triggers-to-fire – kurunve Nov 24 '16 at 16:35
  • 1
    Thanks @EvAzi, i tried in new dev org. it showed me only one SOQL query. additional query was showing up because of a before delete trigger i had on Account. Thanks for pointing out. – Arun Nov 26 '16 at 07:07