bcquality/microsoft/knowledge/performance/calcsums-instead-of-calcfields-in-loop.md
Jesper Schulz-Wedde a9f3c50863 Regenerate microsoft/knowledge from upstream BCApps instructions
The previous LLM-generated knowledge files contained factual
hallucinations. The most visible was the claim that `FindFirst` /
`FindLast` "forces a full-table scan" on an unfiltered record - it does
not; those APIs return a single row via the current key.

Other inaccuracies the audit found and fixed:

* `FindSet(true)` was described as "taking a LockTable". The correct
  upstream phrasing is that `FindSet(true)` sets
  `ReadIsolation::UpdLock` on the read. UpdLock and LockTable are
  related but distinct mechanisms.
* The list of production-scale tables had been invented beyond the
  upstream source (e.g. "Detailed Cust. Ledg. Entry") without a
  citation. The regenerated list matches the ten tables upstream lists
  with their P95 row counts.
* `SetLoadFields` guidance had been augmented with an extra mechanism
  claim ("the database resolves the filter using the index without
  hydrating the value") not present in upstream.

Approach: full regeneration of `microsoft/knowledge/` from the six
upstream BCApps Code Review instruction files, with Microsoft Learn /
the AL language reference as a secondary source. Every claim in every
regenerated file is anchored to a verbatim upstream quote (or a Learn
URL); the audit trail lives in artifacts/trace-<domain>.json on the
session workspace.

The PR #11 transaction/error-handling cluster is preserved verbatim:

* performance/understand-implicit-transaction-boundary.md
* performance/codeunit-run-as-atomic-sub-operation.{md,good.al,bad.al}
* performance/codeunit-run-requires-prior-commit-inside-transaction.{md,good.al,bad.al}
* performance/use-tryfunction-for-error-catching-not-rollback.{md,good.al,bad.al}
* performance/avoid-commit-inside-loops.{md,good.al,bad.al}
* security/commitbehavior-attribute-scopes-explicit-commits.{md,good.al,bad.al}
* testing/transactionmodel-attribute-governs-test-transactions.{md,good.al,bad.al}

These articles already cite Microsoft Learn and were carefully
cross-referenced; the regeneration skips their topics rather than
duplicating them.

File counts after regeneration:

  performance   35 .md  (5 preserved + 30 new)
  privacy       17 .md
  security      18 .md  (1 preserved + 17 new)
  style         33 .md
  testing        1 .md  (preserved)
  ui            19 .md
  upgrade       18 .md

Total 141 atomic knowledge files, each strictly one rule. All pass
.github/scripts/validate_frontmatter.py with 0 errors and 0 warnings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-21 09:53:09 +02:00

1.7 KiB

bc-version domain keywords technologies countries application-area
all
performance
calcfields
calcsums
loop
flowfield
n-plus-one
aggregation
al
w1
all

Use CalcSums to aggregate, not CalcFields inside a loop

Description

CalcFields materializes FlowField values for one record. Each call against a persistent table is "a separate SQL query"; running it inside a repeat ... until Next() = 0 over a large table issues one query per row on top of the iteration itself. CalcSums answers the same aggregation question — "give me the sum of this FlowField over the filtered set" — as a single SQL statement. Per the upstream guidance, CalcFields inside loops on large persistent tables is "a performance problem"; the aggregation form is CalcSums().

Best Practice

When the procedure totals a FlowField (or several) across a filtered set, set the filters, then call CalcSums("Field 1", "Field 2", ...). The platform issues one query; the result is read off the record's FlowField slot. Single CalcFields outside loops is fine, and CalcFields on the current row in a page's OnAfterGetRecord or in OnValidate is the standard pattern — those are per-action, not per-row over a large set.

See sample: calcsums-instead-of-calcfields-in-loop.good.al.

Anti Pattern

if CustLedgerEntry.FindSet() then repeat CustLedgerEntry.CalcFields("Remaining Amount"); Total += CustLedgerEntry."Remaining Amount"; until CustLedgerEntry.Next() = 0; — exactly the upstream-flagged shape. The iteration is the cheap part; the per-row CalcFields is what scales linearly with table size.

See sample: calcsums-instead-of-calcfields-in-loop.bad.al.