Correct severe misconception about SetCurrentKey in the knowledge base

This commit is contained in:
Djordje Cenic 2026-08-22 12:04:00 +02:00
parent 841b4e7cab
commit 6e321d3c56
6 changed files with 74 additions and 40 deletions

View file

@ -0,0 +1,20 @@
codeunit 50231 "Perf Sample SetCurrentKey Bad"
{
// Misconception: SetCurrentKey does NOT tell SQL Server to use this index.
// The optimizer picks the index from the filters (WHERE clause) and statistics.
// The result order is never used here, so SetCurrentKey only adds an ORDER BY
// the query does not need — and can push the plan toward a sort.
procedure SumRemainingAmount(CustomerNo: Code[20]) Total: Decimal
var
CustLedgerEntry: Record "Cust. Ledger Entry";
begin
CustLedgerEntry.SetCurrentKey("Customer No.", Open, "Posting Date");
CustLedgerEntry.SetRange("Customer No.", CustomerNo);
CustLedgerEntry.SetRange(Open, true);
CustLedgerEntry.SetAutoCalcFields("Remaining Amount");
if CustLedgerEntry.FindSet() then
repeat
Total += CustLedgerEntry."Remaining Amount";
until CustLedgerEntry.Next() = 0;
end;
}