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
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati 

Apex CPU time limit exceeded : SOQL Consuming CPU Time.

Hi All,

When i execute the below SOQL query with around 100 fields from  the VF page,  it is counting the CPU Time. Where the SOQL is returning around  11K lineitems. 
System.debug('Start CPU Time===>'+Limits.getCpuTime());
Map<Id, OpportunityLineitem> oppMap = new Map<Id, OpportunityLineitem>([select Id, OpportunityId, ListPrice, CreatedDate, CurrencyIsoCode from OpportunityLineItem where OpportunityId = '006XXXXXXXXXXXX' ]);
System.debug('End CPU Time===>'+Limits.getCpuTime());
Due to security constraint i have removed remaining fields from the above code snippet.

Below is the screenshot of CPU Time consumed for the above SOQL code :
User-added image

User-added image

I am aware that the maximum synchronous CPU Time limit is 10000 milliseconds, due to this limit i am facing the Apex CPU Time limit exceeded error.
Can some one help me why it is consuming the CPU time for SOQL execution? 

As per the salesforce article (https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1), it is mentioned that CPU Time is not counted for SOQL Queries.

P.S. As per business requirement, i don't want to do any changes to SOQL query (like reducing the no. of fields and adding additional filter conditions) and want to execute it from VF page(Syncronous).
  
Jayanth ThathapudiJayanth Thathapudi
Hi Satyanarayana,

Please let me know if found any workaround on this:)

Thanks,
Jay
Deepali KulshresthaDeepali Kulshrestha
Hi V V Satyanarayana,
Greetings to you!
    
- I read your problem 'CPU time limit exceeded'.
- In the salesforce limit of fetch records from the database is 10000.
- So in the query, you have to use LIMIT 10000.
- Please use below code [Solved] : -

    System.debug('Start CPU Time===>'+Limits.getCpuTime());
    Map<Id, OpportunityLineitem> oppMap = new Map<Id, OpportunityLineitem>([select Id, OpportunityId, ListPrice, CreatedDate, CurrencyIsoCode from OpportunityLineItem where OpportunityId = '006XXXXXXXXXXXX' LIMIT 10000]);
    System.debug('End CPU Time===>'+Limits.getCpuTime());
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Reply from Salesforce Support indicates the below:
1). Execution of SOQL will be counted against the apex CPU Time limit.

Article posted above has bug. Salesforce is going to resove soon, by explaining the behaviour in same documentation.


Thanks
Satyanarayana M.