Optimizing AL Performance: SetLoadFields, ReadIsolation, and Avoiding Table Locks
Applying a few disciplined patterns in your AL code can cut execution times from minutes to milliseconds and eliminate SQL lock contention in Business Central SaaS.
1. Use SetLoadFields (Partial Records)
By default, Record.FindSet() fetches every column plus SQL JOINs for every installed tableextension. Load only what you need:
procedure CalculateActiveItemsValue(): Decimal
var
Item: Record Item;
TotalValue: Decimal;
begin
Item.SetLoadFields("Unit Cost");
Item.SetRange(Blocked, false);
if Item.FindSet() then
repeat
TotalValue += Item."Unit Cost";
until Item.Next() = 0;
exit(TotalValue);
end;
2. Control Locking Explicitly with ReadIsolation
With Tri-state Locking, BC upgrades reads to UpdLock after the first write in a transaction. Set ReadIsolation explicitly on read-only lookups:
procedure GetTotalStockForItem(ItemNo: Code[20]): Decimal
var
ItemLedgEntry: Record "Item Ledger Entry";
begin
ItemLedgEntry.ReadIsolation := IsolationLevel::ReadUncommitted;
ItemLedgEntry.SetLoadFields(Quantity);
ItemLedgEntry.SetRange("Item No.", ItemNo);
ItemLedgEntry.CalcSums(Quantity);
exit(ItemLedgEntry.Quantity);
end;
3. Measure with Performance Profiler & Telemetry
Open Performance Profiler (Alt + Q), click Start, reproduce the slow operation, and inspect the Call Tree alongside Application Insights events RT0005 (Long Running SQL) and RT0012 (Lock Timeouts). Never execute external HttpClient calls inside open SQL write transactions!