function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael ColuzziMichael Coluzzi 

Executing a Query on a Large Amount of Accounts/Opportunities/Contacts

Hello Everyone,

We are currently running into an issue where the queries in one of our visualforce pages is taking a very long time to execute. Currently the query is taking around 120 seconds sometimes more to return thier results. Adding a "where condition" has descreased the time but I am looking to see if anyone has any possible suggestions to improve it further. I have posted both queries(with and without WHERE condition). Let me know if you need any further information.

Thank you!

Without Where Condition:

SELECT id, Name, type, owner.Name, website FROM Account ORDER BY LastModifiedDate DESC LIMIT 50

SELECT id, FirstName, Name, LastName, Email, Account.Name, Department, Title, Phone FROM Contact ORDER BY LastModifiedDate DESC LIMIT 50

SELECT id, Account.Name, AccountId, Name, Type, Amount, StageName, Probability, owner.Name, CloseDate FROM Opportunity ORDER BY LastModifiedDate DESC LIMIT 50

Select Username, Title, TimeZoneSidKey, ProfileId, Phone, Name, LastName, Id, FirstName, Email, Division, Department From User WHERE IsActive = true ORDER BY LastModifiedDate DESC LIMIT 50

With Where Condition:

SELECT id, Name, type, owner.Name, website FROM Account where lastvieweddate = LAST_N_DAYS:30 ORDER BY LastViewedDate DESC NULLS LAST LIMIT 50


SELECT id, FirstName, Name, LastName, Email, Account.Name, Department, Title, Phone FROM Contact where lastvieweddate = LAST_N_DAYS:30 ORDER BY LastViewedDate DESC NULLS LAST LIMIT 50

SELECT id, Account.Name, AccountId, Name, Type, Amount, StageName, Probability, owner.Name, CloseDate FROM Opportunity where lastvieweddate = LAST_N_DAYS:30 ORDER BY LastViewedDate DESC NULLS LAST LIMIT 50

Select Username, Title, TimeZoneSidKey, ProfileId, Phone, Name, LastName, Id, FirstName, Email, Division, Department From User WHERE IsActive = true AND lastvieweddate = LAST_N_DAYS:30 ORDER BY LastViewedDate DESC NULLS LAST LIMIT 50