Practical coding tips to Improve performance
Practical Coding Performance Tips
Improving performance in Dynamics 365 NAV/Business Central (AL or C/AL language where applicable) requires a combination of smart coding practices, efficient data access, and leveraging platform features. Here are practical AL coding tips and tricks specifically aimed at improving performance:
Limit the Scope of SETFILTER and SETRANGE
-
Only filter the fields you need to reduce record scanning.
Use instead:
Use SetAutoCalcFields Wisely
Avoid automatic calculation of flow fields (like Amount, Balance) unless you need them.
Note: Use only the fields you actually need to avoid unnecessary SQL hits
Use Temporary Tables When Possible
Avoid hitting the database multiple times by working with temporary in-memory copies.
Avoid Unnecessary Loops
Minimize looping through large data sets. Use aggregation or count queries.
Use MODIFYALL, DELETEALL, INSERTALL for Bulk Operations
These are more efficient than updating/deleting records one at a time in a loop.
Slower:
Faster:
Use MARK and MARKEDONLY for Bulk Filters
Good for tagging data before processing.
Rec.MARKEDONLY(TRUE);
Avoid Using SLEEP or Long UI Locks
Avoid delays and unnecessary SLEEP calls, which tie up the UI and server threads.
Use TESTFIELD Efficiently
Don’t overuse TESTFIELD in loops—it adds unnecessary checks.
Set Keys Appropriately
Define and use proper primary and secondary keys. Index fields based on common filters and sorts.
Minimize Use of FlowFields in Loops
FlowFields trigger lookups and calculations. Avoid them in tight loops—use CALCFIELDS once.
Use FINDSET(TRUE) When Modifying Records
FINDSET(TRUE) gives a lockable, modifiable set of records with fewer reads.
Avoid Find('-'), Find('+'), and Find('=') Unless Necessary
These can cause full table scans if not filtered well.
Better:
Avoid Calling Codeunit.Run Unnecessarily
If you're calling a Codeunit repeatedly, make it single-instance if possible, or cache results.
Set DataPerCompany = false if Applicable
For shared setup tables, avoid duplicating across companies.
Debugging & Profiling Tools
-
SQL Profiler: Track long-running SQL queries.
-
Application Insights (BC version): Can be configured to detect slow operations
Comments
Post a Comment