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
Rajesh RamanRajesh Raman 

SOQL query timeout exception

I am running below menioned SOQL query in my code- 

'select Id, Name,BILLINGSTREET, BILLINGCITY,'+ BILLINGSTATE,BILLINGPOSTALCODE,BILLINGCOUNTRY,CORPORATE_MEMBERSHIP_FORCED_LAPSE_DATE__C,'+
 'SHIPPINGSTREET,SHIPPINGCITY, SHIPPINGSTATE, SHIPPINGPOSTALCODE, SHIPPINGCOUNTRY, PHONE,FAX,'+ 'ACCOUNTSOURCE, Billing_Country_Code__c,Shipping_Country_Code__c,'+
        '(Select Id, MD_Head_of_Household__c, Salutation, firstName, LastName, MailingStreet,MailingCity, MailingState, MailingCountry,'  +
            'MailingPostalCode,Birthdate,Email,HomePhone,MobilePhone from Contacts) ' +
       'from Account where Sync__c = false AND Sync_Timestamp__c >=: startDateTime AND IsDeleted = FALSE order by Sync_Timestamp__c,Id ASC limit '+qLimit+' Offset '+qOffset';

This is query is working fine is TST sandbox but it is giving SOQL query timeout exception in UAT sandbox.
Both sandboxes are Full copy sandboxes and having same data.

Any hep/suggestion would be greatly appreciated.
Arun_KharbArun_Kharb
Hi Rajesh,
Have you tried limiting the no. of records? 
Because if you are having a full copy sanbox then it might return a hell lot of records due to which you are hitting standard execution time limit and your process is getting terminated before it could complete.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
Rajesh RamanRajesh Raman
yes I am using limit and offset
Arun_KharbArun_Kharb
try using limit in inner query as well
Rajesh RamanRajesh Raman
that wont be possbile as I need all contact records associated with account records I am retrieving..
Arun_KharbArun_Kharb
Then use might need a different approach.
have you tried StandardSetController ? Because it can increase your limit to some extent and can help you in pagination as well.
FYR - 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardsetcontroller.htm
Rajesh RamanRajesh Raman
This SOQL query is not in any Controller, it is in REST web service class, so cant use StandardSetController. And I am setting Limit as 50 so there shouldn't be any problem on Limit.
Arun_KharbArun_Kharb
can you share the log file? So that I can get a clear idea about your problem.