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
sreenivasa kallu 1sreenivasa kallu 1 

Apex performance issue - Only happening first time 6 AM to 9 AM in EST

i am thinking following SOQL query cauisng timeout firstime.
 for(Question__c quest :[SELECT id,Question_Text__c,Picklist_Options__c ,
                                    Type__c,Order__c,recordTypeId,recordType.name,
                                    Display_Dependant_Value__c,Display_Dependant_Question__c
                                    FROM Question__c 
                                    WHERE Template_Question__c = null
                                    AND((recordType.name =: System.Label.Record_Type_Standard_Template)
                                    OR (Id in: compQuesSet AND recordType.name =:System.Label.Record_Type_Custom_Template)
                                    OR (recordType.name =: System.Label.Record_type_Form_Question 
                                        AND 
                                        Requisition__c =: requisition.Id))
                                    Order by Order__c
                                    ]){

Currently there are 1 million question_c objects in system.

here debug log statistics..
Number of SOQL queries: 2 out of 100
  Number of query rows: 5619 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 3 out of 150
  Number of DML rows: 47 out of 10000
  Maximum CPU time: 9433 out of 10000 ******* CLOSE TO LIMIT
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

I am not sure why it is only fisrt time happening to users? How to solve?
 
Denis IvancikDenis Ivancik

Hi,

I higly recomend to place query before for-loop, plus avoid using for-each, to improve your code performance.

So, instead of :

for(Question__c quest : [SELECT...])
try following:
List<Question__c> questions = [SELECT ...];
for(Integer i = 0, j = questions.size(); i < j; i++){
    Question__c question = questions[i];
    // rest of your code
}