Find your content:

Search form

You are here

How to test SOQL Query performance for Communities User

 
Share

I am experiencing performance issues on my Production instance with SOQL queries being executed by a Communities user. I'd like to finetune the SOQL Query performance and have done some tests in the Execute Anonymous window.

However, the problem seems to be in user-specific authorizations and/or sharing settings.

So my question is: How can I run SOQL queries as a Communities user without having to deploy dummy code into my Production environment?


Attribution to: Guy Clairbois

Possible Suggestion/Solution #1

In the end I resolved this by creating a 'soqltester' vf page/controller. I can give a partner user (even on production) temporary access and run any query I want.

vf page

<apex:page showHeader="true" sidebar="true" controller="SOQLTesterController" title="SOQL tester">
    <apex:form >

    <apex:inputTextarea value="{!theQuery}" cols="200"/>
    <apex:commandButton action="{!doQuery}" value="Do Query" rerender="resultsPanel" status="queryStatus"/>
            <apex:actionStatus id="queryStatus"  stopText="">
                <apex:facet name="start">
                   <img src="/img/loading.gif"/> Querying....
                </apex:facet> 
            </apex:actionStatus>        
    <br />
    <br />
    <br />
    <apex:outputPanel id="resultsPanel">
        <apex:pageMessages />
        <apex:pageBlock rendered="{!soqlResult != null}">
            <apex:outputLabel value="Query results:" />
            <apex:pageBlockTable var="item" value="{!soqlResult}">
                <apex:repeat var="fieldname" value="{!fields}">
                    <apex:column >
                        <apex:facet name="header">
                            {!fieldname}
                        </apex:facet>
                        {!item[fieldname]}
                    </apex:column> 
                </apex:repeat>
            </apex:pageBlockTable>              
        </apex:pageBlock>
    </apex:outputPanel>
</apex:form>

controller:

public with sharing class SOQLTesterController {

public SOQLTesterController() {

}

public String theQuery {get;set;}

public pageReference doQuery(){

    try{
        soqlResult = Database.query(theQuery);
    } catch (Exception e){
        Apexpages.addMessage(New Apexpages.Message(ApexPages.severity.ERROR,e.getMessage()));
        return null;
    }
    system.debug(soqlResult);

    if(!soqlResult.isEmpty()){
        Map<String, Object> queriedFieldValues = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(soqlResult[0]));
        System.debug(queriedFieldValues);   
        fields = new List<String>();
        dumpFields('', queriedFieldValues);
        system.debug(fields);
    }

    return null;
}

public List<String> fields {get;set;}

public List<sObject> soqlResult {get;set;}

public void dumpFields(String relatedField, Map<String, Object> queriedFieldValues)
{
    for(String queriedFieldName : queriedFieldValues.keySet())
    {
        system.debug(queriedFieldName);
        // Skip this information, its not a field
        if(queriedFieldName.equals('attributes'))
            continue;
        // Check if the queried value represents a related field reference?
        Object queriedFieldValue = queriedFieldValues.get(queriedFieldName);
        if(queriedFieldValue instanceof Map<String,Object>){
            system.debug((Map<String, Object>) queriedFieldValue);
            dumpFields(relatedField+queriedFieldName + '.', (Map<String, Object>) queriedFieldValue);
        } else{
            System.debug(relatedField + queriedFieldName + ' = ' + queriedFieldValue);
            fields.add(relatedField + '' + queriedFieldName);
        }
    }       
}   

}


Attribution to: Guy Clairbois

Possible Suggestion/Solution #2

I would say you create/deploy test class with the soql you want to test optimise and make your soql configurable in custom settings so you can try tweak it to try and test different versions without need to deploy again. Most importantly, make sure you run your test as the Community User, by using system.runAs().

Finally, open Developer Console with debug logs in the test class and checkpoints you can analyse performance of overall execution.


Attribution to: Tanmay K
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32523

My Block Status

My Block Content