Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

How to improve performance in x++(Loop, Insert)

(0) ShareShare
ReportReport
Posted on by
I am in the process of modifying a class to improve performance.
I am calling a child method for insert in a parent method that is looping, is there room for performance improvement here?
I would like to know in detail how to modify it. Thank you in advance.
 
※This is a rough image.
private void【parent method】
{
    while...
    {
    ・
    ・
        this.【child method】
    ・
    ・
    }
}
 
==============================
private void【child method】(TableB, strC)
{
    TableA.XXX = TableB.XXX;
    TableA.YYY = TableB.YYY;
    ・
    ・
    TableA.ZZZ = strC;
 
    TableA.insert();
}
==============================
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 13,249 Super User 2025 Season 1 on at
    How to improve performance in x++(Loop, Insert)
    Hi,
     
    I suggest to use RecordInsertList
     
    e.g.
     
    RecordInsertList                        myTableList;
    MyTable                                 myTable;
    
    myTableList = new RecordInsertList(tableNum(<<YourTable>>));
    
    while(<<your condition>>) // Loops
    {
       myTable.clear();
       myTable.test = 'Test'
       myTableList.add(myTable);
    }
    
    myTableList.insertDatabase();
    Best Regards,
    Mohamed Amine MAHMOUDI
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,965 Super User 2025 Season 1 on at
    How to improve performance in x++(Loop, Insert)
    Hi,
     
    There are two options for making this concept faster. In case you insert records in a loop, the database is called every insert. There can be some latency or table locks, depending on the situation. You can consider using a RecordInsertList or the command insert_recordset for one database call and better performance. 
     
    Depending on how strC is calculated, you can use both or only the RecordInsertList.
     
    You can read more here: 
     
    In one of my blogs, I also talked about insert_recordset preventing nested loops
  • Suggested answer
    Judy Profile Picture
    Judy Microsoft Employee on at
    How to improve performance in x++(Loop, Insert)
    Hi, instead of calling the child method for each iteration of the loop, you can modify the child method to accept a collection of 'TableB' objects e.g.: List<TableB> List = new List<TableB>(); then perform a batch insert operation.
    This can reduce the number of database calls and improve performance.
    Hope it can give you some hints.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans