Avoid Get / FindFirst inside a loop on a large inner table
A Get or FindFirst against another persistent table inside a loop can produce an N+1 access pattern: one outer query followed by repeated inner lookups. Server and primary-key caches can satisfy some Get calls, so a source-level Get is not proof of one SQL round-trip.
- microsoftlayer
- performancedomain
- 2examples
- 3pull requests
Rule on GitHub · microsoft layer · performance · BC all · tier official · derived
Description
A Get or FindFirst against another persistent table inside a loop can produce an N+1 access pattern: one outer query followed by repeated inner lookups. Server and primary-key caches can satisfy some Get calls, so a source-level Get is not proof of one SQL round-trip. The concern is an unbounded loop whose lookup keys are not known to repeat or remain cached.
Best Practice
Use a query object to join the outer and inner tables when the relationship and filters can be expressed as one query. If complete lookup keys repeat and the result remains valid, a scoped cache can reduce lookups to one per distinct key; do not assume primary-key Get calls each hit SQL (see transaction caching). SetLoadFields can reduce the columns transferred by unavoidable inner reads, but it does not eliminate the N+1 shape and must not be presented as doing so.
See sample: avoid-get-inside-loop-on-large-table.good.al.
Anti Pattern
Iterating production BOM lines and calling Item.Get(BOMLine."No.") for each line when the same result can be produced by a query joining Production BOM Line to Item. Partial loading alone is only a payload mitigation for this pattern.
See sample: avoid-get-inside-loop-on-large-table.bad.al.
Examples
Changed by
- #198 Add BC performance knowledge from OptimAL learnings, 2026-09-29
- #174 Make BCQuality documentation easier for partners to get started, 2026-09-09
- #94 Correct performance knowledge guidance, 2026-07-14
From microsoft/BCQuality at a4593a1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).