You need to sign in to do that
Don't have an account?
Possible inefficient SOQL queries HELP
Hello,
I have created a page that is really just a big list of Processes (custom object) where the user can go and take ownership of the process, it is then removed from the master queue.
The page has 5 buttons to change the view View All, View Mine (open), View Mine (Completed), View Oither (open), View Others (Completed). All this is really doing is changes a controller property that sets the visibility of my tables.
The problem is it takes FOREVER to render the different lists when pressing buttons. It works fine testing in the sandbox with only a few dummy processes, but in production with 1000 or so, it takes forever. I think I am doing something wrong with the SOQL queries perhaps. Is there a more efficient way of accomplishing this?
VF
<apex:sectionHeader title="Insurance Profile Campaign" subtitle="Master List"/> <apex:form > <apex:outputpanel id="container"> <apex:outputpanel id="table"> <apex:pageblock > <apex:pageBlockButtons location="top"> <apex:commandLink action="{!viewAll}" value="View All Unassigned" styleClass="btn" style="text-decoration:none" id="all" rerender="table,mine,container"/> <apex:commandLink action="{!viewMine}" value="View Mine (Open)" styleClass="btn" style="text-decoration:none" id="viewMine" rerender="table,mine,container"/> <apex:commandLink action="{!viewOthers}" value="View Others (Open)" styleClass="btn" style="text-decoration:none" id="others" rerender="table,mine,container"/> <apex:commandLink action="{!viewMyCompleted}" value="View Mine (Completed)" styleClass="btn" style="text-decoration:none" id="myComp" rerender="table,mine,container"/> <apex:commandLink action="{!viewOtherCompleted}" value="View Others (Completed)" styleClass="btn" style="text-decoration:none" id="otherComp" rerender="table,mine,container"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!allProcesses}" var="pro" style="width:100%; border:1px solid #D4DADC;" rendered="{!Filter == 'Show All'}"> <apex:column headerValue="Contact" value="{!pro.Contact__c}"/> </apex:pageBlockTable> <apex:pageBlockTable value="{!myProcesses}" var="pro" style="width:100%; border:1px solid #D4DADC;" rendered="{!Filter == 'Show Mine'}"> <apex:column headerValue="Contact" value="{!pro.Contact__c}"/> </apex:pageBlockTable> <apex:pageBlockTable value="{!otherProcesses}" var="pro" style="width:100%; border:1px solid #D4DADC;" rendered="{!Filter == 'Show Others'}"> <apex:column headerValue="Contact" value="{!pro.Contact__c}"/> </apex:pageBlockTable> <apex:pageBlockTable value="{!myCompletedProcesses}" var="pro" style="width:100%; border:1px solid #D4DADC;" rendered="{!Filter == 'Show My Completed'}"> <apex:column headerValue="Contact" value="{!pro.Contact__c}"/> </apex:pageBlockTable> <apex:pageBlockTable value="{!otherCompletedProcesses}" var="pro" style="width:100%; border:1px solid #D4DADC;" rendered="{!Filter == 'Show Other Completed'}"> <apex:column headerValue="Contact" value="{!pro.Contact__c}"/> </apex:pageBlockTable> </apex:pageblock> </apex:outputpanel> </apex:form> </apex:page>
Controller
List<Process__c> allPros; List<Process__c> myPros; List<Process__c> otherPros; List<Process__c> myCompletedPros; List<Process__c> otherCompletedPros; Transient List<Task> acts; Public Process__c selectedProcess {get;set;} Public String Filter {get;set;} Public boolean showStep {get;set;} Public Process_Step__c step; Public Contact con {get;set;} public InsuranceCampaignMasterController() { acts = new List<Task>(); selectedProcess = new Process__c(); step = new Process_Step__c(); Filter = 'Show All'; ShowStep = false; con = new Contact(); } public List<Process__c> getAllProcesses() { if(allPros == null) { allPros = [Select Id, Name, Process_Owner__r.Name, Contact__c, Contact__r.MailingStreet, Contact__r.MailingCity, Contact__r.Phone, Process_Template__r.Name, Progress__c, Contact__r.Reason_for_no_Insurance__c From Process__c Where Process_Template__r.Name = 'Insurance Profile Campaign' and Process_Owner__r.Name = 'Chris Duncombe' and Status__c != 'Completed' Order By Contact__r.MailingCity ASC]; }return allPros; } public List<Process__c> getMyProcesses() { if(myPros == null) { myPros = [Select Id, Name, Process_Owner__r.Name, Contact__c, Contact__r.MailingStreet, Contact__r.MailingCity, Contact__r.Phone, Process_Template__r.Name, Progress__c, Contact__r.Reason_for_no_Insurance__c, Start_Date__c From Process__c Where Process_Template__r.Name = 'Insurance Profile Campaign' and Process_Owner__c =: userinfo.getUserId() and Status__c != 'Completed' Order By Start_Date__c ASC]; }return myPros; } public List<Process__c> getOtherProcesses() { if(otherPros == null) { otherPros = [Select Id, Name, Process_Owner__r.Name, Contact__c, Contact__r.MailingStreet, Contact__r.MailingCity, Contact__r.Phone, Process_Template__r.Name, Progress__c, Contact__r.Reason_for_no_Insurance__c From Process__c Where Process_Template__r.Name = 'Insurance Profile Campaign' and Process_Owner__c !=: userinfo.getUserId() and Process_Owner__r.Name != 'Chris Duncombe' and Status__c != 'Completed' Order By Process_Owner__r.Name, Start_Date__c ASC]; }return otherPros; } public List<Process__c> getMyCompletedProcesses() { if(myCompletedPros == null) { myCompletedPros = [Select Id, Name, Process_Owner__r.Name, Contact__c, Contact__r.MailingStreet, Contact__r.MailingCity, Contact__r.Phone, Process_Template__r.Name, Progress__c, Contact__r.Reason_for_no_Insurance__c, Start_Date__c, Completion_Date__c, Total_Process_Time__c, Outcome__c From Process__c Where Process_Template__r.Name = 'Insurance Profile Campaign' and Process_Owner__c =: userinfo.getUserId() and Status__c = 'Completed' Order By Completion_Date__c]; }return myCompletedPros; } public List<Process__c> getOtherCompletedProcesses() { if(otherCompletedPros == null) { otherCompletedPros = [Select Id, Name, Process_Owner__r.Name, Contact__c, Contact__r.MailingStreet, Contact__r.MailingCity, Contact__r.Phone, Process_Template__r.Name, Progress__c, Contact__r.Reason_for_no_Insurance__c, Start_Date__c, Completion_Date__c, Total_Process_Time__c, Outcome__c From Process__c Where Process_Template__r.Name = 'Insurance Profile Campaign' and Process_Owner__c !=: userinfo.getUserId() and Process_Owner__r.Name != 'Chris Duncombe' and Status__c = 'Completed' Order By Process_Owner__r.Name, Completion_Date__c ASC]; }return otherCompletedPros; } public void takeOwnership() { selectedProcess = [Select Id, Name, Process_Owner__c, Start_Date__c, Completion_Date__c, Total_Process_Time__c, Outcome__c From Process__c Where Id =: system.currentPageReference().getParameters().get('proId')]; step = [Select Id, Name, Assigned_To__c From Process_Step__c Where Process__c =: selectedProcess.id]; selectedProcess.Process_Owner__c = userinfo.getUserId(); selectedProcess.Start_Date__c = system.today(); step.Assigned_To__c = userinfo.getUserId(); update selectedProcess; update step; myPros = null; } public void setStep() { showStep = true; selectedProcess = [Select Id, Name, Contact__c, Process_Owner__c, Start_Date__c, Completion_Date__c, Total_Process_Time__c, Outcome__c From Process__c Where Id =: system.currentPageReference().getParameters().get('proId')]; step = [Select Id, Name, Process__c, Complete_Step_From_Process__c, Log_Activity_From_Process__c, Create_Task_From_Process__c, Status__c, Completed_By__c, Date_Completed__c, Assigned_To__c From Process_Step__c Where Process__c =: selectedProcess.id]; con = [Select Id, Name From Contact Where id =: SelectedProcess.Contact__c]; } public process_Step__c getMyStep() { return step; } public List<Task> getStepActivities() { acts = [Select Id, Subject, Navigator_Subject__c, ActivityDate, Description, WhoId, OwnerId, WhatId From Task Where WhatId =: step.id]; return acts; } public void saveProcessChanges() { update selectedProcess; } public void viewMine() { Filter = 'Show Mine'; showStep = false; } public void viewAll() { Filter = 'Show All'; showStep = false; } public void viewOthers() { Filter = 'Show Others'; showStep = false; } public void viewMyCompleted() { Filter = 'Show My Completed'; showStep = false; } public void viewOtherCompleted() { Filter = 'Show Other Completed'; showStep = false; } }
Any help would be greatly appreicated
Chris
I found that it was definitely a rendering issue, not a SOQL issue. The constant rerendering of a DataTable with 1000 or so records was causing very slow ajax rerendering.
I was able to solve the problem through the suggestion of the dynamic query, as well as adding some pagination by using the OFFSET in my SOQL. I was able to do this rather easily using this great blog post from RedPointSolutions
http://blog.redpointsolutions.com/bid/182738/Add-Pagination-to-your-Visualforce-Pages-using-the-SOQL-OFFSET-Clause
Heres the final code if anyone might be interested
VF
Controller
Thanks for the responses ~ sfdcfox ~, you definitely got me going in the right direction with the dynamic table and the dynamic SOQL.
Chris
All Answers
Your page is very inefficient, I expect you have a large(ish) view state as well.
How about this:
If you went with dynamic queries, you could get this code even smaller. But I think my current suggestions will provide a pretty heavy increase in speed.
Thank you for the reply. I took your advice and went a step further and built the SOQL dynamically, but the page is still just as slow
VF
Controller
I agree this is must more neat and concise, but it really takes forever to query when you try to change the list. Any ideas?
Chris
Try using the Developer Console to see how long the query is taking, and how much time is spent rendering. It should help you figure out what's going on.
I generally use Eclipse. How do I use the developer console to see query time vs. rendering time. I have a feeling the time is in the rendering, not the query. If that is the case, is there a better solution. Would pagination help?
Chris
I found that it was definitely a rendering issue, not a SOQL issue. The constant rerendering of a DataTable with 1000 or so records was causing very slow ajax rerendering.
I was able to solve the problem through the suggestion of the dynamic query, as well as adding some pagination by using the OFFSET in my SOQL. I was able to do this rather easily using this great blog post from RedPointSolutions
http://blog.redpointsolutions.com/bid/182738/Add-Pagination-to-your-Visualforce-Pages-using-the-SOQL-OFFSET-Clause
Heres the final code if anyone might be interested
VF
Controller
Thanks for the responses ~ sfdcfox ~, you definitely got me going in the right direction with the dynamic table and the dynamic SOQL.
Chris