+ Start a Discussion
Vijay sidaraddiVijay sidaraddi 

I have a account object which contains 100000 records in it

Hi

I have a account object which contains 100000 records in it,if i want to write a SOQL query to fetch only 50000 records in it, how to achieve it..

As per governer limit we can retrieve max 50000 records for each SOQL query, is there any possibilities in trigger i can fetch  more than 50000 records, if yes how?

Thanks
Vijay S

Thanks
Vijay S
 
Best Answer chosen by Vijay sidaraddi
William TranWilliam Tran
Use the LIMIT AND OFFSET tag to recursive retrieve information:
 
The syntax for LIMIT is:


SELECT fieldList
FROM objectType
[WHERE conditionExpression]
  [LIMIT numberOfRows]

For example:


SELECT Name
FROM Account
WHERE Industry = 'Media' LIMIT 125
SELECT Name
FROM Merchandise__c
WHERE Price__c > 5.0
ORDER BY Name
LIMIT 100
OFFSET 10
For more info:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks

All Answers

James LoghryJames Loghry
What are you trying to do with the SOQL query exactly?  You can specify "Limit 50000" at the end of your SOQL query to limit your result set to 50,000 records, but not only will it be slow and non performant, but it will cut off half the results you might be expecting back.

Try filtering the SOQL using the WHERE clause to filter your results to the particular Accounts you are interested in.  Also, if you can, pick indexed fields like Id or External Id or Lookup / Master Detail fields which will help improve the performance of your SOQL query.

If the query is still too massive to do what you would like, consider moving the SOQL and any necessary apex into a Batchable Apex class, which will chunk the records into groupings of at most 2,000 records, making any queries and Apex more managable in terms of governor limits and general performance.
William TranWilliam Tran
Use the LIMIT AND OFFSET tag to recursive retrieve information:
 
The syntax for LIMIT is:


SELECT fieldList
FROM objectType
[WHERE conditionExpression]
  [LIMIT numberOfRows]

For example:


SELECT Name
FROM Account
WHERE Industry = 'Media' LIMIT 125
SELECT Name
FROM Merchandise__c
WHERE Price__c > 5.0
ORDER BY Name
LIMIT 100
OFFSET 10
For more info:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks
This was selected as the best answer