Bulk Load and Minimal Logging Dean Savović Bulk Load and Minimal Logging
Sponsors
Agenda Minimal Logging Defined Minimal Logging Cross Reference Enhancements Methods Delete Update
Minimal Logging Defined Only full page BU Locking Batchsize Enable/disable triggers and constraints Input sort order (clustered target)
Minimal Logging Prereqs Simple or Bulk-logged recovery model TABLOCK (not always) Target table not replicated ALLOW_PAGE_LOCKS „ON” Backup
Minimally Logged Operations Bulk import operations (BCP, BULK INSERT and INSERT..SELECT) SELECT INTO operations TRUNCATE Starting in Sql server 2008 , INSERT SELECT statement can also be handled with minimal logging. Partial updates to large value data types, using the .WRITE clause. CREATE INDEX, ALTER INDEX REBUILD DROP TABLE Partition Switch Merge
Bulk Methods Integration Services Data Destinations BCP BULK INSERT SQL Server Destination OLE DB Destination BCP BULK INSERT INSERT … SELECT SELECT … INTO …
Minimal Logging Heap IsEmpty TABLOCK Locks Logging Concurrent Yes BU-Tab Minimal No IX-Tab Full
Data – minimal index – full Minimal Logging Heap + Non-Clustered Index IsEmpty TABLOCK Locks Logging Concurrent Yes Sch-M Minimal No IX-Tab Full Data – minimal index – full
Minimal Logging Clustered Index IsEmpty TABLOCK Locks Logging Concurrent Yes Sch-M Minimal No IX-Tab X-Tab Full
Minimal Logging Clustered Index + Non-Clustered Index IsEmpty TABLOCK Locks Logging Concurrent Yes Sch-M Depends No IX-Tab
Minimal Logging Columnstore Index 102400 NO TABLOCK
Minimal Logging Memory Optimized Always Fully Logged
Bulk Load Enhancements Select * Into dbo.TargetTable from dbo.SourceTable (SQL Server 2005-) Insert into dbo.TargetTable with (TABLOCK) select * from dbo.SourceTable (SQL Server 2008+) Insert into CI Btree (SQL Server 2008+) Order by Enable TF-610 Bulk insert (SQL Server 2008+) Without TABLOCK
Insert Scenarios
Comparison bulk insert dbo.tTable from 'd:\temp\tHeap.csv’ with ( formatfile = ‘D:\temp\bcp.fmt’ , rows_per_batch = 10000000 )
Heap No Indexes
Heap and Indexes
Clustered
Clustered And Non-Clus Indexes
Memory Optimized
Columnstore (CCI)
Comparison
Deleting a Large Amount of Rows in a Partition or Table in Minimally Logging Mode
Create a Copy of Main Table
Bulk load the rows you want to keep into the temporary partition INSERT INTO Sales_Temp WITH (TABLOCK) SELECT * FROM Sales WHERE OrderDate >=‘20010101’ AND OrderDate <‘20020101’ AND<Keep Criteria>
Switch out and truncate the old data
Rebuild indexes and use SWITCH ALTER TABLE Sales_Temp TO Sales SWITCH PARTITION 1
Drop the temporary table
Updating a Large Part of the Data in a Partition or a Table in Minimally Logged Mode
Write a change log to a temporary table
If the table is partitioned, switch out the partition you want to change ALTER TABLE Sales SWITCH PARTITION 1 TO Sales_Old
Merge the change log with the original table and bulk load to temporary table SELECT o.PrimaryKey ,COALESCE(d.Col1, o.Col1) , COALESCE(d.Col2, o.Col3) ... etc... FROM Sales_Old o LEFT JOIN Sales_Delta d
Delete the change log and original data DROP TABLE Sales_Delta DROP TABLE Sales_Old
SWITCH in the result of the merge ALTER TABLE Sales_New SWITCH TO Sales PARTITION 1 DROP TABLE Sales_New
EXEC [sys]. [sp_tableoption] @TableNamePattern = N'dbo EXEC [sys].[sp_tableoption] @TableNamePattern = N'dbo.BulkLoadTest’, @OptionName = 'table lock on bulk load’, @OptionValue = 'ON'
Evaluations Event Evaluation Session Evaluation http://www.sqlsaturday.com/764/EventEval.aspx Session Evaluation http://www.sqlsaturday.com/764/Sessions/SessionEvaluation.aspx
Sponsors
Questions?