SOQL with parent field reference

1. Always use Parent Id instead of Parent.Id.

Use AccountId instead of Account.Id in the Query.

2. Use inner join query instead of using Parent object field reference. Make sure the parent object field is indexed for selective.

List < Contact > listContacts = [ SELECT Id FROM Contact WHERE Account.Name = ‘Test’ ];//Takes more time to execute
List < Contact > listContacts1 = [ SELECT Id FROM Contact WHERE AccountId IN ( SELECT Id FROM Account WHERE Name = ‘Test’) ];//Takes less time to execute

Leave a Reply