11

We needs to use Schema and DescribeSObjectResult classes to get all SObject in the user's organization. But we noticed performance problem after upgraded to API v44. The following code could reproduce the issue: Visualforce Page Code:

<apex:page controller="TestSchemaTimeController">
    <script type="text/javascript">
        function perform() {
            var args = [];
            var start = new Date().valueOf();
            args.push("{!$RemoteAction.TestSchemaTimeController.QuerySchema}");
            args.push(function(result, event) { var output = document.getElementById("output"); output.value += "Count:" + result + " Time:" + (new Date().valueOf() - start) + "ms\r\n"; });
            args.push({ buffer: false, escape: false, timeout: 120000 }); 
            window.Visualforce.remoting.Manager.invokeAction.apply(window.Visualforce.remoting.Manager, args);
        }
        function clearOutput() {
            var output = document.getElementById("output");
            output.value = "";
        }
    </script>
    <h1>Test Schema Time</h1>
    <div>
        <button onclick="perform()">Click</button>
        <button onclick="clearOutput()">Clear</button>
    </div>
    <div>
        <TextArea id="output" style="width:800px;height:600px"></TextArea>
    </div>
</apex:page>

Apex Code

public class TestSchemaTimeController {
    @RemoteAction
    public static Integer QuerySchema() {
        Map<String, Schema.SObjectType> typeMap = Schema.getGlobalDescribe();
        for (Schema.SObjectType sot : typeMap.values()) {
            System.debug(sot);
            Schema.DescribeSObjectResult dsr = sot.getDescribe();
            //String name = dsr.getName();
            //Boolean acc = dsr.isAccessible();
        }
        return typeMap.values().size();
    }
}

By the demo code above, it cost about 250~300ms via API version 43, but cost about 1000~1600ms via API version 44. You could modify the API version of the Apex Class to verify the result. API v45 preview in sandbox also have the performance issue. We have no idea why this Apex method is slow down after API version 44. And we have to keep on using API version 43 now to make sure our customer not face the performane issue. Anyone else meet the same problem?

5
9

I beefed up your code a bit to get more information, and here's data from five sample runs in each version:

API 44.0

{"avgTime":1,"globalTime":51,"maxObject":"User","maxTime":54,"minObject":"ContractHistory","minTime":0,"objectCount":831,"totalTime":1162}
{"avgTime":3,"globalTime":49,"maxObject":"RecordType","maxTime":575,"minObject":"ContractHistory","minTime":0,"objectCount":831,"totalTime":2806}
{"avgTime":1,"globalTime":58,"maxObject":"User","maxTime":59,"minObject":"ContractHistory","minTime":0,"objectCount":831,"totalTime":1621}
{"avgTime":3,"globalTime":198,"maxObject":"RecordType","maxTime":580,"minObject":"ContractHistory","minTime":0,"objectCount":831,"totalTime":3120}
{"avgTime":2,"globalTime":57,"maxObject":"User","maxTime":60,"minObject":"ContractHistory","minTime":0,"objectCount":831,"totalTime":2054}

API 43.0

{"avgTime":0,"globalTime":30,"maxObject":"OrgDeleteRequest","maxTime":2,"minObject":"Contract","minTime":0,"objectCount":820,"totalTime":45}
{"avgTime":0,"globalTime":45,"maxObject":"OrgDeleteRequest","maxTime":3,"minObject":"ContractHistory","minTime":0,"objectCount":820,"totalTime":51}
{"avgTime":0,"globalTime":46,"maxObject":"OrgDeleteRequest","maxTime":3,"minObject":"Contract","minTime":0,"objectCount":820,"totalTime":50}
{"avgTime":0,"globalTime":27,"maxObject":"OrgDeleteRequest","maxTime":3,"minObject":"ContractHistory","minTime":0,"objectCount":820,"totalTime":44}
{"avgTime":0,"globalTime":52,"maxObject":"OrgDeleteRequest","maxTime":2,"minObject":"Contract","minTime":0,"objectCount":820,"totalTime":57}
  • avgTime: average time per object describe
  • minTime: minimum time per object describe
  • maxTime: maximum time per object describe
  • globalTime: time for global describe
  • totalTime: total time spent describing the objects
  • minObject: the fastest object to describe (ignoring sub-ms differences)
  • maxObject: the slowest object to describe (ignoring sub-ms differences)
  • objectCount: number of objects described

In version 43.0, the maxTime is only just about 3, and there are 11 fewer objects described. It appears that RecordType is broken in 44.0, occasionally stalling for up to 580ms!

It might be worth your time, if possible, to open a case or get a bug logged. Having consistently unreliable describe calls is problematic, because it means that any number of orgs could be randomly affected for no discernible reason.

Keep in mind that staying on v43 means you are missing some objects (at least 11). If you need the latest describe, then that might not be a viable option. I don't know what your use case is, but you might mitigate this a little bit by a multi-step process where you describe just a few objects each round trip and do so in a loop.

This will be a wiki entry for those that want to contribute. Benchmark code is provided below.


public class TestSchemaTimeController {
    public class Results {
        public Long globalTime;
        public Long minTime = 999999;
        public Long maxTime = 0;
        public Long avgTime;
        public Long totalTime;
        public String minObject;
        public String maxObject;
        public Long objectCount;
    }
    @RemoteAction
    public static Results QuerySchema() {
        Results r = new Results();
        Long t1 = DateTime.now().getTime();
        Map<String, Schema.SObjectType> typeMap = Schema.getGlobalDescribe();
        Long t2 = DateTime.now().getTime();
        Long avgCounter = 0;
        for (Schema.SObjectType sot : typeMap.values()) {
            Long t3 = DateTime.now().getTime();
            Schema.DescribeSObjectResult dsr = sot.getDescribe();
            Long t4 = DateTime.now().getTime();
            Long totalTime = t4-t3;
            avgCounter += totalTime;
            if(totalTime > r.maxTime) {
                r.maxObject = ''+sot;
                r.maxTime = totalTime;
            }
            if(totalTime < r.minTime) {
                r.minObject = ''+sot;
                r.minTime = totalTime;
            }
            //String name = dsr.getName();
            //Boolean acc = dsr.isAccessible();
        }
        r.globalTime = t2-t1;
        r.totalTime = avgCounter;
        r.avgTime=avgCounter/typeMap.size();
        r.objectCount=typeMap.size();
        return r;
    }
}

<apex:page controller="TestSchemaTimeController">
    <script type="text/javascript">
        function perform() {
            var args = [];
            var start = new Date().valueOf();
            args.push("{!$RemoteAction.TestSchemaTimeController.QuerySchema}");
            args.push(function(result, event) { var output = document.getElementById("output"); output.value += JSON.stringify(result)+"\r\n"; });
            args.push({ buffer: false, escape: false, timeout: 120000 }); 
            window.Visualforce.remoting.Manager.invokeAction.apply(window.Visualforce.remoting.Manager, args);
        }
        function clearOutput() {
            var output = document.getElementById("output");
            output.value = "";
        }
    </script>
    <h1>Test Schema Time</h1>
    <div>
        <button onclick="perform()">Click</button>
        <button onclick="clearOutput()">Clear</button>
    </div>
    <div>
        <TextArea id="output" style="width:800px;height:600px"></TextArea>
    </div>
</apex:page>
3
  • Thanks for your answer, but we have no chance to log a case directly, I was reject by SF support because of we don't have a developer support contract. :-( That is wy I moved here and trying to ask a question. It is very kind of your guys give me a response. – Leo Li Jan 29 '19 at 1:59
  • @LeoLi See the other answer; there is a Known Issue for this. For now, stick with v43, but do subscribe to the KI (click This Issue Affects Me) if you want to be notified when fixed. – sfdcfox Jan 29 '19 at 2:01
  • sure, we have already to this, and we will wait for this issue be fixed. And of course, I have voted for "This Issue Affects me" – Leo Li Jan 29 '19 at 7:48
6

This is now a Known Issue that you can follow for updates: https://success.salesforce.com/issues_view?id=a1p3A000001RXBZ

Also has been referenced in Partner Community (Partner login required): https://partners.salesforce.com/0D53A000047HAW3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.