Prerequisites for Minimal Logging in Bulk Import
SQL Server 2008 Books Online (November 2009)
Prerequisites for Minimal Logging in Bulk Import

For a database under the full recovery model, all row-insert operations that are performed by bulk import are fully logged in the transaction log. Large data imports can cause the transaction log to fill rapidly if the full recovery model is used. In contrast, under the simple recovery model or bulk-logged recovery model, minimal logging of bulk-import operations reduces the possibility that a bulk-import operation will fill the log space. Minimal logging is also more efficient than full logging.

ms190422.note(en-us,SQL.100).gifNote:
The bulk-logged recovery model is designed to temporarily replace the full recovery model during large bulk operations. For information on switching between the full recovery model and bulk-logged recovery model, see Considerations for Switching from the Full or Bulk-Logged Recovery Model.

Table Requirements for Minimally Logging Bulk-Import Operations

Minimal logging requires that the target table meets the following conditions:

  • The table is not being replicated.
  • Table locking is specified (using TABLOCK).
    For more information, see Controlling Locking Behavior for Bulk Import.
    ms190422.note(en-us,SQL.100).gifNote:
    Although data insertions are not logged in the transaction log during a minimally logged bulk-import operation, the Database Engine still logs extent allocations each time a new extent is allocated to the table.

Whether minimal logging can occur for a table also depends on whether the table is indexed and, if so, whether the table is empty:

  • If the table has no indexes, data pages are minimally logged.
  • If the table has no clustered index but has one or more nonclustered indexes, data pages are always minimally logged. How index pages are logged, however, depends on whether the table is empty:
    • If the table is empty, index pages are minimally logged.
    • If table is non-empty, index pages are fully logged.
      ms190422.note(en-us,SQL.100).gifNote:
      If you start with an empty table and bulk import the data in multiple batches, both index and data pages are minimally logged for the first batch, but beginning with the second batch, only data pages are minimally logged.

  • If the table has a clustered index and is empty, both data and index pages are minimally logged. In contrast, if a table has a clustered index and is non-empty, data pages and index pages are both fully logged regardless of the recovery model.
    ms190422.note(en-us,SQL.100).gifNote:
    If you start with an empty table and bulk import the data in batches, both index and data pages are minimally logged for the first batch, but from the second batch onwards, only data pages are bulk logged.

For more information, including a summary of table locking and logging behavior during bulk import, see Optimizing Bulk Import Performance.

Best Practice   When you use bulk-logged recovery to import a large set of table rows, consider distributing bulk imports among multiple batches. Each batch equates to one transaction. Thus, when a batch completes, its log becomes available for backup. The next log backup will reclaim the log space that is used to bulk import that batch of rows.

See Also

Concepts

Choosing the Recovery Model for a Database

Other Resources

bcp Utility
BULK INSERT (Transact-SQL)
OPENROWSET (Transact-SQL)
BACKUP (Transact-SQL)
ALTER DATABASE (Transact-SQL)
SuspendIndexing Property
UseBulkCopyOption Property
Table Hints (Transact-SQL)
INSERT (Transact-SQL)

Help and Information

Getting SQL Server 2008 Assistance
Community Content

Trace Flag 610
Added by:SteveOLAP
No mention of TF 610! As tersely and nebulously stated by the SQLCAT team, "Whether an INSERT… SELECT operation into clustered indexes is minimally logged depends on the state of trace flag 610." Itzik Ben-Gan provides a more precise description of conditions in SQL Mag: "B-tree AND nonempty AND TF-610 AND new key-range" is one important scenario under which minimal logging can be obtained. Yet MSFT's Data Loading Performance Guide points out that this may not be true when the b-tree is not a clustered index (see 'Heap + Index' under "Summarizing Minimal Logging Conditions".) This whole topic deserves to be summarized far better than it has been by MSFT.
© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View