Question:
How efficient is Lightning Data Service (LDS) in Salesforce when it comes to working with multiple records? Is it suitable for bulk loading or updating, or is it better to use Apex for handling large volumes of records?
Answer:
Lightning Data Service (LDS) in Salesforce is designed to simplify working with records on the client side. It handles standard operations like creating, reading, updating, and deleting records while automatically respecting field-level security, sharing rules, and CRUD permissions. LDS also provides a client-side cache, which allows multiple components on the same page to share the same record data without making additional server calls. This makes LDS extremely efficient for single-record operations, such as Quick Actions, record forms, or components that reference the same record multiple times.
However, when it comes to working with multiple records, LDS becomes significantly less efficient. From testing, it is evident that loading more than about six records triggers separate load operations for each record. Although the system does not crash, the total loading time increases dramatically. For example, fetching around 800 records using LDS took over 20 seconds, which works out to roughly 40 records per second. This is in stark contrast to a bulk-loading scenario using Apex, such as a PagingSortingDemo approach, which can load 52,770 records with four fields in just three seconds, or about 17,500 records per second—a total performance increase of approximately 43,900%.
The core reason for this inefficiency lies in how LDS handles multiple records. Each record fetched via LDS typically triggers its own transaction and data fetch, even if the records are displayed together in a list or table. This is fundamentally different from an Apex approach, where a single SOQL query can fetch hundreds or thousands of records in a single transaction. Furthermore, LDS operations like createRecord or updateRecord also operate one record at a time, which makes bulk updates slow and less efficient.
Here is an example demonstrating the inefficiency of using LDS for multiple records:
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import CONTACT_FIELDS from '@salesforce/schema/Contact.Name';
export default class MultiContactLoader extends LightningElement {
contactIds = ['003xx000004TmiAAAS', '003xx000004TmiBAAS', '003xx000004TmiCAAS'];
contacts = [];
connectedCallback() {
this.contactIds.forEach(id => {
getRecord({ recordId: id, fields: [CONTACT_FIELDS] })
.then(record => {
this.contacts.push(record);
})
.catch(error => {
console.error('Error loading contact:', error);
});
});
}
}
In the example above, getRecord is called individually for each contact ID. While this works for a few records, if you scale it to hundreds or thousands, the system will create multiple separate requests, leading to slow loading times. The solution in such cases is to fetch records in bulk using an Apex controller and a single SOQL query:
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(List<Id> contactIds) {
return [SELECT Id, Name FROM Contact WHERE Id IN :contactIds];
}
}
Using Apex, all contacts are retrieved in one efficient transaction, and the data can be sent to the Lightning component in bulk, drastically reducing the loading time.
The takeaway is that Lightning Data Service is best suited for single-record operations, such as Quick Actions or updating related records. For scenarios requiring bulk loading or updates, using Apex is the preferred method. It is expected that Salesforce might introduce a bulk-friendly option for LDS in the future, but until then, developers should rely on Apex for high-volume operations to maintain optimal performance.
Job-Oriented Salesforce Training with 100% Money Back Assurance
Our Salesforce Course is designed to provide a thorough understanding of the Salesforce platform, equipping you with the essential skills to thrive in the CRM industry. The curriculum includes vital modules such as Salesforce Admin, Developer, and AI, combining foundational knowledge with hands-on practice. By engaging in real-world projects and assignments, you’ll develop the expertise to address complex business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical skills and industry insights necessary for success in the Salesforce ecosystem.
Along with technical knowledge, our Top 10 Salesforce training institutes in Hyderabad offers personalized mentorship, exam preparation, and interview coaching to enhance your career prospects. You’ll have access to comprehensive study materials, live project experience, and dedicated support throughout your learning journey. Upon completing the course, you’ll be well-prepared for certification exams and equipped with the practical skills employers value. Begin your Salesforce career with us and unlock countless career opportunities. Sign up for a Free Demo today!






Leave a Reply
You must be logged in to post a comment.