Business central blog
Subscribe to blog and get news about new posts.

Significant improvement of Number Series in BC24

In one of my series of posts, I examined Number Series and Number Sequences from a performance perspective. I am pleased to report that the situation has significantly improved starting from Business Central 24! I will focus on the cloud version.
Performance of Number Series and Number Sequence
Performance of Number Series and Number Sequence in Cloud. Detailed Analysis.

NumberSequence.Range

Let's start with the new method NumberSequence.Range(). Essentially, this method updates the last number of the current NumberSequence to the specified number. This is a simple and quick solution if we need to reserve a certain number of numbers (sic!). It is important to note that the reservation occurs immediately after calling the Range() method and is not rolled back by errors.

local procedure TestNumberSequenceRange()
var
  ListOfText: List of [Text];
  StartDateTime: DateTime;
  StartNumber: Integer;
  i: Integer;
begin
  StartDateTime := CurrentDateTime();
  StartNumber := NumberSequence.Range(NumberSequenceLbl, NumberOfIterations, true);
  for i := StartNumber to StartNumber + NumberOfIterations do
    ListOfText.Add(Format(i));
  Message('Number sequence range: %1 iterations took %2.', NumberOfIterations, CurrentDateTime() - StartDateTime);
end;

New number series interfaces

In the latest version of Business Central 24, the NoSeriesManagement codeunit is marked as obsolete. Developers should now use two new codeunits: "No. Series" and "No. Series - Batch". As the names suggest, the first is better suited for obtaining a single number, while the second is intended for obtaining multiple numbers.
Let's check how the "No. Series" performs when generating 100,000 numbers. I want to point out that this is not the correct usage, and it's better to use "No. Series - Batch" for this purpose. However, it's still interesting to see if there is a difference. As you will see later, the difference for BC23 vs. BC24 is within the margin of error, and it seems that nothing has changed. But in reality, this is not the case.
Let's return to the more appropriate use of the new interfaces, specifically "No. Series - Batch" for sequential number generation.
It's a huge difference! So, what's the deal? Well, all the work in this implementation is transferred to the temporary table No Series Line, which makes it much faster. But how do the data end up in the database? For this, the `SaveState()` method was created, which essentially processes the temporary table and saves it to the physical instance of the variable.

local procedure TestNoSeriesNoGaps()
var
  NoSeriesBatch: Codeunit "No. Series - Batch";
  ListOfText: List of [Text];
  StartDateTime: DateTime;
  i: Integer;
begin
  StartDateTime := CurrentDateTime();
  for i := 1 to NumberOfIterations do
    ListOfText.Add(NoSeriesBatch.GetNextNo(NoSeriesCodeNoGapsLbl));
  NoSeriesBatch.SaveState();
  Message('Number series with no gaps: %1 iterations took %2.', NumberOfIterations, CurrentDateTime() - StartDateTime);
end;
This also means that a single "No Series" essentially cannot generate gaps.
Now, I suggest comparing the results from Cloud BC23 and Cloud BC24. I used the production type of DB, but I didn't notice any difference with sandbox.

Summary

Business Central is constantly evolving, and we are witnessing just one example of this development. The improvements in number series are indeed impressive. There's still a lot of work to be done, but the path is the right one. I remind that everyone can contribute to open source.

Source Code

June 3, 2024