Newer Version Available

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

Query

This API resource executes the specified SOQL query.

When a SOQL query is executed, up to 2,000 records can be returned at a time in a synchronous request. If the number of results exceeds this limit, the response contains only the first set of results and a query identifier, contained in the response’snextRecordsUrl field. The identifier can be used in another request to retrieve the next batch of records.

URI
/services/data/vXX.X/query/?q=SOQL query
For retrieving query performance feedback without executing the query:
/services/data/vXX.X/query/?explain=SOQL query
For retrieving query performance feedback on a report or list view:
/services/data/vXX.X/query/?explain=report or list view ID
For retrieving more query results if the initial results are o large:
/services/data/vXX.X/query/query identifier
Formats
JSON, XML
HTTP Method
GET
Authentication
Authorization: Bearer token
Parameters
Parameter Description
q A SOQL query. To create a valid URI, replace spaces with a plus sign + in the query string. For example: SELECT+Name+FROM+MyObject. If the SOQL query string is invalid, a MALFORMED_QUERY response is returned.
explain

A SOQL query to get performance feedback. Use explain instead of q to get a response that details how Salesforce processes your query. Use this feedback to further optimize your queries. To get feedback on how Salesforce processes a report or list view, use a report or list view ID in place of the query string.

The explain parameter is available in API version 30.0 and later.

Using explain with the Query resource is a beta feature. There’s no support for this beta feature. For more information, contact Salesforce.

Note

If the SOQL query string is invalid, a MALFORMED_QUERY response is returned. If the report or list view ID is invalid, an INVALID_ID response is returned.

Response body
For a query using the q parameter, the response contains an array of query result records. For a query using the explain parameter, the response contains one or more query plans that can be used to execute the query, report, or list view. The plans are sorted from most optimal to least optimal. Each plan has the following information:
Name Type Description
cardinality number The estimated number of records the query would return, based on index fields, if any.
fields string[] If the leading operation type is Index, these values are the index fields used for the query. Otherwise, the value is null.
leadingOperationType string The primary operation type that can use to optimize the query. This type can be one of these values:
  • Index—The query uses an index on the query object.
  • Other—The query uses optimizations internal to Salesforce.
  • Sharing—The query uses an index based on the user’s sharing rules. If there are sharing rules that limit which records are visible to the current user, those rules can optimize the query.
  • TableScan—The query scans all records for the query object, and doesn’t use an index.
notes feedback note[] An array of one or more feedback notes. Each note contains:
  • description— A detailed description of a part of the optimization. This description can include information on optimizations that can’t be used and why they can’t used.
  • fields— An array of one or more fields used for the optimization.
  • tableEnumOrId— The table name for the fields used for the optimization.
This response field is available in API version 33.0 and later.
relativeCost number The cost of this query compared to the SOQL selective query threshold. A value greater than 1.0 means the query isn’t selective. For more information on selective queries, see More Efficient SOQL Queries in the Apex Code Developer's Guide.
sobjectCardinality number The approximate count of all records in your organization for the query object.
sobjectType string The name of the query object, such as Merchandise__c.

Resources for Executing SOQL Queries