Bulk Inserts

Microsoft Dynamics NAV automatically buffers inserts in order to send them to Microsoft SQL Server at one time.

By using bulk inserts, the number of server calls is reduced, thereby improving performance.

Bulk inserts also improve scalability by delaying the actual insert until the last possible moment in the transaction. This reduces the amount of time that tables are locked; especially tables that contain SIFT indexes.

Application developers who want to write high performance code that utilizes this feature should understand the following bulk insert constraints.

Bulk Insert Constraints

If you want to write code that uses the bulk insert functionality, you must be aware of the following constraints.

Records are sent to SQL Server when the following occurs:

  • You call COMMIT to commit the transaction.

  • You call MODIFY or DELETE on the table.

  • You call any FIND or CALC methods on the table.

Records are not buffered if any of the following conditions are met:

  • The application is using the return value from an INSERT call; for example, "IF (GLEntry.INSERT) THEN".

  • The table that you are going to insert the records into contains any of the following:

    • BLOB fields

    • Fields with the AutoIncrement property set to Yes

The following code example cannot use buffered inserts because it contains a FIND call on the GL/Entry table within the loop.

IF (JnlLine.FIND('-')) THEN BEGIN
  GLEntry.LOCKTABLE;
  REPEAT
    IF (GLEntry.FINDLAST) THEN
      GLEntry.NEXT := GLEntry."Entry No." + 1
    ELSE
      GLEntry.NEXT := 1;
    // The FIND call will flush the buffered records.
    GLEntry."Entry No." := GLEntry.NEXT ;
    GLEntry.INSERT;
  UNTIL (JnlLine.FIND('>') = 0)
END;
COMMIT;

If you rewrite the code, as shown in the following example, you can use buffered inserts.

IF (JnlLine.FIND('-')) THEN BEGIN
  GLEntry.LOCKTABLE;
  IF (GLEntry.FINDLAST) THEN
    GLEntry.Next := GLEntry."Entry No." + 1
  ELSE
    GLEntry.Next := 1;
  REPEAT
    GLEntry."Entry No.":= GLEntry.Next;
    GLEntry.Next := GLEntry."Entry No." + 1;
    GLEntry.INSERT;
  UNTIL (JnlLine.FIND('>') = 0)
END;
COMMIT;
// The inserts are performed here.

See Also

Concepts

Data Access Redesign
Table Keys and Performance
C/AL Database Functions and Performance on SQL Server
Query Objects and Performance