Salesforce Performance Queried Fields

Does it matter how many fields you return from a query? Compare the SOQL queries below:

List<Opportunity> opps = [
        SELECT
                AccountId, Amount, CampaignId, CloseDate, ContactId, ContractId,
                CreatedById, CreatedDate, Description, ExpectedRevenue, Fiscal,
                FiscalQuarter, FiscalYear, ForecastCategory, ForecastCategoryName,
                HasOpenActivity, HasOpportunityLineItem, HasOverdueTask, Id,
                IsClosed, IsDeleted, IsPrivate, IsWon, LastActivityDate, LastModifiedById,
                LastModifiedDate, LastReferencedDate, LastViewedDate, LeadSource, Name,
                NextStep, OrderNumber__c, OwnerId, Pricebook2Id, Probability, StageName,
                SystemModstamp, TotalOpportunityQuantity, Type
        FROM Opportunity
        LIMIT 30
];
                    
List<Opportunity> opps = [
        SELECT ExpectedRevenue, Id, Name
        FROM Opportunity
        LIMIT 30
];
                    

The first query takes more than 3x the time it takes for the latter and costs almost 4x the cpu time.

Not too surprisingly, it is preferable to only query the fields you need.

At times I've seen the former combined with using Schema info to dynamically generate the SOQL, which has an additional cost as well.

Note: there was no measurable in performance difference between using the standard query vs calling Database.query and passing a string.

 

 

© 2001 – 2023 Object Factory Inc
marker marker marker marker marker