bcquality/microsoft/knowledge/performance/avoid-commit-inside-loops.good.al
Jesper Schulz-Wedde e374a8a4b8 Bound commit checkpoints by key range
Use a capped ordered query to discover each checkpoint watermark before locking and processing only that key range.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 85be3fc4-5253-47b8-ba1b-6b8fd188fcea
2026-07-14 11:48:01 +02:00

63 lines
1.7 KiB
AL

query 50127 "Perf Customer Chunk"
{
QueryType = Normal;
OrderBy = ascending(CustomerNo);
elements
{
dataitem(Customer; Customer)
{
column(CustomerNo; "No.") { }
}
}
}
codeunit 50128 "Perf Sample CommitInLoop Good"
{
procedure NormalizeCustomerNames()
var
LastCustomerNo: Code[20];
begin
// The outer loop owns checkpoints; the per-row loop contains no Commit.
while NormalizeNextChunk(LastCustomerNo) do
Commit();
end;
local procedure NormalizeNextChunk(var LastCustomerNo: Code[20]): Boolean
var
Customer: Record Customer;
CustomerChunk: Query "Perf Customer Chunk";
FirstCustomerNo: Code[20];
LastChunkCustomerNo: Code[20];
begin
CustomerChunk.TopNumberOfRows(500);
if LastCustomerNo <> '' then
CustomerChunk.SetFilter(CustomerNo, '>%1', LastCustomerNo);
CustomerChunk.Open();
if not CustomerChunk.Read() then begin
CustomerChunk.Close();
exit(false);
end;
FirstCustomerNo := CustomerChunk.CustomerNo;
repeat
LastChunkCustomerNo := CustomerChunk.CustomerNo;
until not CustomerChunk.Read();
CustomerChunk.Close();
Customer.SetCurrentKey("No.");
Customer.SetRange("No.", FirstCustomerNo, LastChunkCustomerNo);
if not Customer.FindSet(true) then begin
LastCustomerNo := LastChunkCustomerNo;
exit(true);
end;
repeat
Customer.Name := UpperCase(Customer.Name);
Customer.Modify();
until Customer.Next() = 0;
LastCustomerNo := LastChunkCustomerNo;
exit(true);
end;
}