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
Sean TanSean Tan 

Cross Object Field vs Join Performance question

Hey,

 

This is more of a nitty gritty type question but a question that has come up nonetheless.

 

If I have a formula field that is doing a cross object join, will this perform faster than a join in a raw SOQL statement when retrieving the information (not filtering)? For example:

 

Contact has a lookup to Account

Formula field on Contact called Account_Name__c

 

If I query something like this:

 

Contact[] contactList =  [ SELECT Account_Name__c FROM Contact ];

 

 

vs

 

Contact[] contactList =  [ SELECT Account.Name FROM Contact ];

Would the first query be faster than the second?  As far as I know, formula fields are always evaluated at point in time that the query is run and therefore should perform roughly the same. I am however being told otherwise and that a formula field will be more performant. Am I missing something here?

 

VaasuVaasu
Second one will be fast when compared to first one.
Sean TanSean Tan

Normally I'd think they'd be roughly the same (since it's not a filter it is essentially doing the join for us). However, some benchmarks have been done and are showing that the formula field approach is significantly faster than the join approach.

 

Any other insight on why this might be would be appreciated.