Query results bypass the primary-key cache
The Business Central server caches primary-key Get calls within a transaction. Query objects do not use that primary-key cache: reopening a Query per lookup executes the query again; Read consumes rows from an open query, not a new query for each row.
- microsoftlayer
- performancedomain
- 2examples
- 3pull requests
Rule on GitHub · microsoft layer · performance · BC all · tier official · derived
Contributions welcome — open a PR to refine or extend this article.
Description
The Business Central server caches primary-key Get calls within a transaction. Query objects do not use that primary-key cache: reopening a Query per lookup executes the query again; Read consumes rows from an open query, not a new query for each row. avoid-get-inside-loop-on-large-table.md is right when an unbounded inner Get/FindFirst joins two large sets. It is wrong as a blanket rewrite of repeated Get on the same keys. Replacing a cached Get with a Query reopened per call can be slower.
Best Practice
Keep Record.Get for repeated lookups of the same primary keys in one transaction. Use a Query when the work is a true join or aggregation that the record API would express as nested scans. Do not flag a guarded Get on a repeating key as an N+1 solely because a Query could express the same columns.
See sample: query-results-bypass-primary-key-cache.good.al.
Anti Pattern
Rewriting a helper that Gets Customer by No. on every sales line into a Query opened inside that helper. Distinct line customers still need a lookup; repeating customers were already served from the PK cache. The Query pays SQL every time.
See sample: query-results-bypass-primary-key-cache.bad.al.
Examples
- Good: query-results-bypass-primary-key-cache.good.al
- Bad: query-results-bypass-primary-key-cache.bad.al
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
- #153 Promote knowledge for Microsoft-owned review skills, 2026-09-03
From microsoft/BCQuality at aaaf6c1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).