Update large tables in bulk using batch mode

Batch mode updates a class table in bulk, in batches of 500 objects, instead of in one large update. Use it to load a large class from M-Files into SQL for the first time, or to send a large number of changes from SQL to M-Files. The procedure is spMFUpdateTableinBatches.

Why use it

  • Large first loads. Taking on the records of a large class from M-Files into SQL. A single update of a very large class can run out of memory; batches keep each transfer small.

  • Mass updates from SQL. Sending changes for many records from SQL to M-Files.

  • More than about 10,000 records. Nothing stops you using it for small volumes, but it is advisable for record sets above about 10,000.

  • Failures cost less. Records are updated batch by batch. If the procedure stops or fails, the batches already processed stay updated, and you can continue from where it stopped.

  • Progress is visible. With @WithStats = 1 each batch reports its time and count.

  • One class at a time. Each run updates one class table.

How it works

From M-Files to SQL (@UpdateMethod = 1): the procedure takes the objects of the class whose object ID is between @FromObjid and @ToObjid and that the table audit marks as needing an update. It updates them in batches of 500, in object ID order.

  • @WithTableAudit = 1 refreshes the table audit (spMFTableAudit) first, so the procedure knows which objects are new or changed. Use it for a first load, and whenever the audit may be out of date.

  • @WithTableAudit = 0 uses the result of the last table audit. If no audit has run for the class, nothing is processed.

Note

Empty class table. In the current release, batch mode loads nothing into a class table that has no rows yet, even with @WithTableAudit = 1. Load a few objects of the class first with spMFUpdateTable, then run batch mode:

EXEC dbo.spMFUpdateTable @MFTableName  = 'MFLarge_Volume',
                         @UpdateMethod = 1,
                         @ObjIDs       = N'101,102,103';  -- IDs of a few objects in this class

Then run batch mode with @WithTableAudit = 1. Until the table audit is refreshed, it knows only the few objects loaded this way.

From SQL to M-Files (@UpdateMethod = 0): the procedure takes all records of the class table with Process_ID = 1, marks them Process_ID = 99, and sends them to M-Files in batches of 500 in ID order. @FromObjid and @ToObjid are ignored. If the run stops, the records not yet sent keep Process_ID = 99. You don’t need to reset them: run the procedure again and it continues with them.

Parameters

@MFTableName

The class table, for example 'MFCustomer'.

@UpdateMethod

1 (default) from M-Files to SQL; 0 from SQL to M-Files.

@WithTableAudit

0 (default) or 1. See How it works. With @UpdateMethod = 0, 1 overwrites changes made in M-Files with the values in SQL instead of raising a synchronization error.

@FromObjid

First object ID of the range, default 1. Only for @UpdateMethod = 1.

@ToObjid

Last object ID of the range, inclusive, default 1000000. Only for @UpdateMethod = 1. It is an upper limit: the last batch ends at the highest object ID the run finds, so a value above it does no harm. Best practice: pass the highest object ID of the class plus 500 (see Find the highest object ID), and raise it when the class has object IDs above 1,000,000.

@WithStats

0 (default) or 1 to show the progress of each batch in the SSMS messages window.

@RetainDeletions

0 (default) or 1 to keep objects deleted in M-Files in the class table.

@ProcessBatch_ID

Output: the ID of the process batch in the logging tables.

@Debug

0 (default) or 1.

Note

Earlier releases used @maxObjid, @BatchesToRun and @MinObjid. They no longer exist. @FromObjid and @ToObjid replace @MinObjid and @maxObjid. The batch size is fixed at 500, so instead of @BatchesToRun, limit a run by narrowing the object ID range.

Find the highest object ID

The range must reach the highest object ID among the objects of the class. Refresh the class index, which records every object of the class in MFAuditHistory, then read the highest object ID from it:

EXEC dbo.spMFObjectTypeUpdateClassIndex @IsAllTables = 1; -- 0 = only classes with IncludeInApp

SELECT COUNT(*) AS Objects, MAX(ah.ObjID) AS HighestObjid
FROM dbo.MFAuditHistory AS ah
INNER JOIN dbo.MFClass AS mc
        ON mc.MFID = ah.Class
WHERE mc.TableName = 'MFLarge_Volume';

Note

Earlier versions of this page read the highest object ID from MFObjectTypeToClassObject. That table is no longer used, and a Release 5 database does not have it.

Update from M-Files to SQL

A first load of a large class, with the highest object ID 59,500:

EXEC dbo.spMFUpdateTableinBatches @MFTableName    = 'MFLarge_Volume',
                                  @UpdateMethod   = 1,      -- M-Files to SQL
                                  @WithTableAudit = 1,      -- refresh the table audit first
                                  @FromObjid      = 1,
                                  @ToObjid        = 60000,  -- highest object ID + 500
                                  @WithStats      = 1;

To continue after a stop, run the procedure again with the same @FromObjid and @ToObjid and @WithTableAudit = 0. Objects already loaded are skipped, so the run continues where it stopped. Setting @FromObjid to the “From objid ID” of the last batch shown, for example @FromObjid = 15000, also works: that batch is processed again.

image1

Each batch reports its processing time in seconds, the object ID it started from (“From objid ID”), and the number of records processed. A batch holds only objects of this class that the table audit marks as new or changed, so its object IDs need not be consecutive. Every batch holds 500 objects except the last, which holds the rest.

The screenshots on this page are from an earlier release; the wording of the messages may differ slightly.

Very large classes

For very large volumes (hundreds of thousands of objects or more), M-Files needs time for its background processing, such as indexing, to catch up. Instead of one run over the whole range, run the procedure in ranges, for example 100,000 object IDs at a time, and let M-Files catch up between runs.

Update from SQL to M-Files

Mark the records to update with Process_ID = 1, then run:

EXEC dbo.spMFUpdateTableinBatches @MFTableName  = 'MFLarge_Volume',
                                  @UpdateMethod = 0,   -- SQL to M-Files
                                  @WithStats    = 1;

image2

The run first shows the number of records to update, then the number remaining after each batch.