Optimize AL performance with SetLoadFields, nevertheless beware of JIT load
A nice little feature in AL when thinking about performance is the SetLoadFields Function. It sets the fields to be initially loaded when the record is retrieved from the database, meaning that, for example, if you do define an Item.SetLoadFields(Description) only Description Field will be retrieved from the database along with the fields that are always selected for loading: Primary key, SystemId, and data audit fields SystemCreatedAt, SystemCreatedBy, SystemModifiedAt, SystemModifiedBy, fields that are filtered upon, and all other fields will be unloaded.
Also, a good method to use is SetBaseLoadFields(), which will omit to load of fields that are coming from table extensions, so you have only fields that are from the base table to work without worrying about how many extensions are installed and lowering the performance of your own code for which extensions are usually irrelevant.
And now comes the “Be careful” part if you want to use “Description 2”. For example, in your later process, you will receive a nice little warning/error (depending on how you set up your CodeCop alert) that using a “Description 2” field will cause a JIT load.
And this is one thing you should be careful about since every unloaded field will cause a JIT load (Just-in-time loading). In such a case, the platform does an implicit Get on the record and loads the missing field. Get calls can often be served by the server’s data cache or resolved as a clustered index operation on the database. Nevertheless, they are one more call you should avoid for the best performance.
Also, the JIT load may fail for multiple reasons, like:
- The record has been modified between the original load and the subsequent JIT load.
- The record has been deleted between the original load and the subsequent JIT load.
- The record has been renamed between the original load and the subsequent JIT load.
One more thing you should care about is passing a partially loaded record as a function parameter. Since CodeCop does not look for the JIT load outside of the scope of the function, be very careful not to cause a JIT load when passing Partial Record as a parameter since this will cause a JIT load and lower the performance.
Example:
Conclusion
In summary, the SetLoadFields and SetBaseLoadFields functions in AL provide efficient ways to manage field loading and optimize performance. Selectively loading only necessary fields and excluding table extension fields enhances overall efficiency. However, caution is required to avoid JIT loads, as they may introduce potential risks and impact performance. Developers should be mindful when passing partially loaded records as parameters to maintain optimal AL development performance.