function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Antoine LELEU 10Antoine LELEU 10 

SOQL unusually long during Trigger Execution

Hello,

I identified a SOQL request unusually long during an Execution Trigger :
17:53:37.3 (894756684)|SOQL_EXECUTE_BEGIN|[13]|Aggregations:0|SELECT Id, AccountId, StartDate, Date_de_fin__c, CONTRAT_EN_COURS_form__c FROM Contract WHERE (CONTRAT_EN_COURS_form__c = TRUE AND AccountId IN :tmpVar1) LIMIT 50000 17:53:50.852 (13852270691)|SOQL_EXECUTE_END|[13]|Rows:0 

I don't understand why this request is so long, about 13s.
For information, there is no Contract record in the org. All the contract are deleted each night.

Any ideas ?
Thank for your help.

Antoine 
Best Answer chosen by Antoine LELEU 10
Amit Chaudhary 8Amit Chaudhary 8
It just because of multiple operator in your query also.  Please check below post how to Optimize Query
1) http://help.salesforce.com/apex/HTViewSolution?id=000181277&language=en_US
2) https://help.salesforce.com/apex/HTViewSolution?urlname=Force-com-Query-Optimizer-FAQ

User-added image
NOTE:- In operator will also include the solf deleted record , That is , deleted record that are still in th Recycle BIN


For best performance, SOQL queries must be selective, particularly for queries inside of triggers. To avoid long execution times, the system can terminate nonselective SOQL queries Please check below post for more information.
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm
2) https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-make-my-SOQL-query-selective&language=en_US
3) https://developer.salesforce.com/page/Developing_Selective_Force.com_Queries_through_the_Query_Resource_Feedback_Parameter_Pilot

Let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Its totaly depend on number of record on Contact object and sever performance.
Antoine LELEU 10Antoine LELEU 10
Thanks Amit,

thanks for your answer, i know this but as explained : 
For information, there is no Contract record in the org. All the contract are deleted each night.

Antoine
Amit Chaudhary 8Amit Chaudhary 8
It just because of multiple operator in your query also.  Please check below post how to Optimize Query
1) http://help.salesforce.com/apex/HTViewSolution?id=000181277&language=en_US
2) https://help.salesforce.com/apex/HTViewSolution?urlname=Force-com-Query-Optimizer-FAQ

User-added image
NOTE:- In operator will also include the solf deleted record , That is , deleted record that are still in th Recycle BIN


For best performance, SOQL queries must be selective, particularly for queries inside of triggers. To avoid long execution times, the system can terminate nonselective SOQL queries Please check below post for more information.
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm
2) https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-make-my-SOQL-query-selective&language=en_US
3) https://developer.salesforce.com/page/Developing_Selective_Force.com_Queries_through_the_Query_Resource_Feedback_Parameter_Pilot

Let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Antoine LELEU 10Antoine LELEU 10
Thanks Amit,

your informations are very helpfull. 
Do you know what is the best way to exclude deleted records from soql query ?

Thanks a lot,
Antoine
 
Amit Chaudhary 8Amit Chaudhary 8
One idea is already posted for same. Please vote on same idea.
1) https://success.salesforce.com/ideaView?id=08730000000iXJ1AAM

There is an IsDeleted system field (checkbox for TRUE/FALSE) on the records.  You can include IsDeleted=FALSE in your query.

But that will still search record in deleted record as well.
http://www.salesforcegeneral.com/salesforce-articles/including-archived-and-deleted-records-in-your-soql-query.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm

Sorry no solution for same now. Simply detele all record from recycle bin also

Let us know if this will help you
 
Antoine LELEU 10Antoine LELEU 10
Thanks Amit for all this détails about SOQL query.

Antoine