Scenario
Let's say I have two custom obejcts – Student
and Class
. The Class
object is connected with object Student through lookup relationship. The Class
object also has a formula field (Student_UniqueCode__c
):
Student__r.uniqueCode__c.
Now while doing SOQL, which one I should use, the formula field or the relationship to get better performance?
Query 1
Select Student_UniqueCode__c from Student__c
or
Select Student__r.uniqueCode__c from Student__c
Query 2
Select <..> from Student__c where Student_UniqueCode__c like ...
or
Select <..> from Student__c where Student__r.uniqueCode__c like ...
Best Answer
You should use the
Query Plan
tool to figure out performance questions for your own org. When I analyzed a nearly identical structure in my own org, there was no difference in performance.It may also be worth reading this article by Steve Bobrowski: Force.com Formula Fields, Indexes, and Performance Gotchas. It goes into detail about what your decision making process should be and the performance impacts.