Blog
AdvancedReferenceSQL Server Performance Internals

Bulk loading and minimal logging

How to move data in at speed - BULK INSERT, bcp, and OPENROWSET - the exact conditions that unlock minimal logging (recovery model, TABLOCK, index state), and why a full-logged bulk load is a transaction-log time bomb.

Applies to
SQL Server2016-2025 (13.x-17.x)
matureUpdated Reviewed
advancedAssumes production experience with the basics.

What you will be able to do

  • State the three conditions minimal logging always requires, and the recovery model it depends on.
  • Navigate the index rules - when a clustered index or a non-empty table turns minimal logging back into full logging.
  • Explain why a bulk load under FULL recovery can fill the log, and how bulk-logged recovery exists to answer that.
  • Batch a large load so a failure does not force a rollback of the whole file.

Row-by-row INSERT is the wrong tool for a bulk load - it pays the full per-row log and lock cost for millions of rows. SQL Server’s bulk path (BULK INSERT, bcp, OPENROWSET(BULK)) moves data in extents rather than rows, and under the right conditions it barely logs at all. Getting those conditions right is the difference between a load that finishes and one that fills the transaction log and rolls back.

The analogy is a goods-in bay at a warehouse. Carrying stock through the front door box by box, signing each item into the ledger, is row-by-row insert; reversing a lorry into the bay and tipping whole pallets onto the floor is the bulk path, where the ledger just records the pallets that landed rather than itemising every box. The bay doors stay locked to everyone else while the load is in progress - the TABLOCK - and the summary paperwork is only permitted when the paperwork rules allow it and the destination shelves are empty. Shelving into an aisle that already holds stock forces the itemised log again, which is why the cheapest load lands on empty shelves.

The three conditions minimal logging always needs

Minimal logging is available only when all of these hold:

  1. Recovery model is SIMPLE or BULK_LOGGED - under FULL recovery every bulk row-insert is fully logged. This is the gate most loads trip on.
  2. TABLOCK is specified - a table-level bulk-update lock on the load.
  3. The table is not replicated and not memory-optimized - replication forces full logging even under BULK_LOGGED, because the log reader must reconstruct the rows.

When minimal logging applies, the engine logs extent allocations instead of every row - a small fraction of the log volume. It is also faster, not just smaller, because logging less is doing less.

The index rules - the part that surprises

Whether data and index pages are minimally logged depends on the table’s index state:

Table shape Data pages Index pages
No indexes (heap) Minimal n/a
Heap + nonclustered indexes, empty Minimal Minimal (first batch; later batches log data only)
Heap + nonclustered indexes, non-empty Minimal Fully logged
Clustered index, empty Minimal Minimal
Clustered index, non-empty Fully logged Fully logged
Any index with IGNORE_DUP_KEY = ON Fully logged Fully logged

The pattern that matters operationally: the cheapest bulk load is into an empty table with a clustered index, or into a heap and index afterward. Loading into a populated clustered table is fully logged regardless of recovery model - the table’s existing data is the reason. Loading in batches into an initially-empty clustered table keeps the first batch minimal and the later batches minimal on the data pages only.

A bulk load that stays fast and does not fill the log

  1. Recovery + target

    SIMPLE or BULK_LOGGED; empty clustered table or heap

    FULL recovery makes every row fully logged - check before you start

  2. TABLOCK load

    BULK INSERT ... WITH (TABLOCK, BATCHSIZE = n)

    BATCHSIZE splits the load so a failure rolls back a batch, not the file

  3. Index afterward (heap case)

    Rows in, then CREATE INDEX in one pass

    Cheaper than maintaining the index through the load - one sort, not a million inserts

  4. Verify + switch back

    Row count, then FULL recovery if you moved to BULK_LOGGED

    A log backup after returning to FULL restores the chain for point-in-time recovery

The order of operations decides whether the load is minimally logged and whether a failure costs the whole file.

Why this is the right mental model

A fully-logged bulk load is not merely slow - it is a transaction-log hazard. Every row lands in the log, the log file grows until the disk is full or the operation aborts, and then the entire load rolls back (paying the log cost again in undo). The bulk-logged recovery model exists precisely for this: it temporarily replaces FULL during a large load so the load itself is minimally logged, while keeping the database in a recoverable state around it - with the documented caveat that a minimally-logged operation is only recoverable to the point of the last log backup, not point-in-time.

What to do about it

  • Into a populated table you cannot empty - accept the full logging, use BATCHSIZE, and watch log space, or stage into a heap and INSERT ... SELECT in batches.
  • The index-versus-load trade - for a large load into an empty heap, dropping nonclustered indexes first and rebuilding them after is almost always faster than maintaining them through the load; a single index build is one sort, maintenance is a per-row traversal.
  • BATCHSIZE is your rollback boundary - set it so a failure rolls back one bounded batch rather than the whole file, and so each batch is a commit boundary that checkpoints can keep up with.

Guided practice

A beginner exercise, worked in a scratch database. The scenario: prove that the minimal-logging conditions are real by watching log space across two loads - one that qualifies and one that does not. DBCC SQLPERF(LOGSPACE) shows the log space your database is using. Work each step and check yourself before moving on.

  1. Set the stage. Create a scratch database, set RECOVERY SIMPLE, and create an empty heap dbo.stg with a couple of columns. Note the database’s log space used from DBCC SQLPERF(LOGSPACE). Check: recovery model is SIMPLE and the target is an empty heap - two of the conditions already met. If the database is under FULL, every row will be fully logged and the comparison below collapses; the recovery model is the gate most loads trip on.
  2. Load with the conditions met. Run INSERT INTO dbo.stg WITH (TABLOCK) SELECT ... pulling a few hundred thousand generated rows (cross-joining a numbers list is enough), then check log space again. Check: the log grew by a small amount - extent allocations were logged, not every row. If the growth tracked the data volume instead, find which condition failed; TABLOCK on the target is the usual omission.
  3. Load against a disqualifying shape. Create a non-empty clustered table dbo.stg_c, pre-load some rows, then run the same INSERT ... WITH (TABLOCK) SELECT into it and watch log space again. Check: log growth now tracks the row volume - a populated clustered table is fully logged regardless of recovery model. If it stayed small, the table was not both clustered and non-empty; revisit the index-state table - the table’s existing data is the reason.
  4. Say why the cheapest shape is what it is. In one sentence each: why is loading into an empty clustered table, or into a heap with the index built afterward, the cheapest bulk load? Check: an empty clustered table keeps data and index pages minimally logged; a heap takes rows minimally logged and then CREATE INDEX is one sort rather than a per-row traversal through the load. If you answered “because heaps are faster”, revisit the index rules - the shape determines the logging, and logging less is doing less.
  5. Bound the rollback. A real file load fails halfway. Which option keeps the failure from rolling back the whole file, and what does it also give the checkpoints? Check: BATCHSIZE - each batch is a commit boundary, so a failure rolls back one bounded batch rather than the whole file, and checkpoints can keep up between batches. If you answered TABLOCK, that unlocks the logging, not the rollback boundary; reread the load pipeline.

Check your understanding

Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.

  1. What are the three conditions minimal logging always requires? Recovery model SIMPLE or BULK_LOGGED, TABLOCK specified on the load, and a table that is neither replicated nor memory-optimized. All three must hold - the recovery model alone is the gate most loads trip on. Reread the conditions list if any was missing.
  2. What does a minimally-logged load write to the transaction log instead of every row? Extent allocations - a small fraction of the log volume of per-row logging. It is faster as well as smaller, because logging less is doing less. If you answered “nothing”, that is the common misreading - minimal is not zero.
  3. Why is a fully-logged bulk load worse than merely slow? Every row lands in the log, the log file grows until the disk fills or the operation aborts, and the whole load then rolls back - paying the log cost again in undo. It is a transaction-log hazard, not a slowdown; this is exactly why the BULK_LOGGED recovery model exists.
  4. Why does a populated clustered table defeat minimal logging even under BULK_LOGGED? The index rules make a non-empty clustered table fully logged on both data and index pages - the table’s existing data is the reason, and no recovery model overrides it. The cheapest shapes are an empty clustered table, or a heap indexed afterward. If you expected recovery model to decide everything, revisit the index-state table.
  5. Why does replication force full logging even when every other condition is met? The log reader must reconstruct the rows for subscribers, and minimal logging writes only extent allocations - there is nothing to reconstruct from. If you thought replication was just another reader, revisit the third condition; it is the reason TABLOCK and BULK_LOGGED cannot rescue a replicated table.

Where SQL Server sits

How this component relates to the rest of the stack. Items without a link are not covered here yet.

Deployed with

Commonly run alongside

  • Windows Failover Clustering

Alternatives

Solves a similar problem differently