Newer Version Available

This content describes an older version of this product. View Latest

Get Feedback on Query Performance

Use the Query resource along with the explain parameter to get feedback on how Salesforce executes your query, report, or list view. Salesforce analyzes each query to find the optimal approach to obtain the query results. Depending on the query and query filters, Salesforce uses an index or internal optimization. Use the explain parameter to return details on how Salesforce optimizes your query, without actually running the query. Based on the response, you can decide whether to fine-tune the performance of your query by making changes like adding filters to make the query more selective.

Using explain with the REST API query resource is a beta feature. There is no support associated with this beta feature. For more information, contact Salesforce.

Note

The response contains one or more query execution plans that, sorted from most optimal to least optimal. The most optimal plan is the plan that’s used when the query, report, or list view is executed.

See the explain parameter in Query for more details on the fields returned when using explain. See Working with Very Large SOQL Queries in the Apex Developer Guide for more information on making your queries more selective.

Example

Example usage for getting performance feedback on a query that uses Merchandise__c
1/services/data/v49.0/query/?explain=
2SELECT+Name+FROM+Merchandise__c+WHERE+CreatedDate+=+TODAY+AND+Price__c+>+10.0
Example response body for executing a performance feedback query
1{
2  "plans" : [ {
3    "cardinality" : 1,
4    "fields" : [ "CreatedDate" ],
5    "leadingOperationType" : "Index",
6    "notes" :  [ {
7      "description" : "Not considering filter for optimization because unindexed",
8      "fields" : [ "IsDeleted" ],
9      "tableEnumOrId" : "Merchandise__c"
10    } ],
11    "relativeCost" : 0.0,
12    "sobjectCardinality" : 3,
13    "sobjectType" : "Merchandise__c"
14  }, {
15    "cardinality" : 1,
16    "fields" : [ ],
17    "leadingOperationType" : "TableScan",
18    "notes" :  [ {
19      "description" : "Not considering filter for optimization because unindexed",
20      "fields" : [ "IsDeleted" ],
21      "tableEnumOrId" : "Merchandise__c"
22    } ],
23    "relativeCost" : 0.65,
24    "sobjectCardinality" : 3,
25    "sobjectType" : "Merchandise__c"
26  } ]
27}

This response indicates that Salesforce found two possible execution plans for this query. The first plan uses the CreatedDate index field to improve the performance of this query. The second plan scans all records without using an index. The first plan is used if the query is executed. Both plans note that a secondary optimization used when filtering out records marked as deleted was not used because the IsDeleted field is not indexed.

Example

Example usage for getting performance feedback on a report
1/services/data/v49.0/query/?explain=00OD0000001hCzMMCU
Example response body for getting performance feedback on a report
1{
2  "plans" : [ {
3    "cardinality" : 1,
4    "fields" : [ ],
5    "leadingOperationType" : "TableScan",
6    "notes" :  [ {
7      "description" : "Not considering filter for optimization because unindexed",
8      "fields" : [ "IsDeleted" ],
9      "tableEnumOrId" : "Merchandise__c"
10    } ],
11    "relativeCost" : 0.65,
12    "sobjectCardinality" : 3,
13    "sobjectType" : "Merchandise__c"
14  } ]
15}

This response indicates that Salesforce found one possible execution plan for the query behind this report. The plan scans all records without using an index. It can’t apply a secondary optimization when filtering out records marked as deleted, because the IsDeleted field is not indexed.