I have a problem with BULK INSERT/BCP process that includes sort operator although I have done everything to avoid that. Firstly, I have exported the data to file with BCP (native format) using queryout and with "ORDER BY Id ASC, Created ASC" clause. This gave me a file with sorted data. Secondly, I used the following T-SQL to import the data:
BULK INSERT dbo.LSZ_Table
FROM
'T:\LSZ\data_file_native.dat'
WITH
(
FORMATFILE='T:\LSZ\format_n.xml'
,KEEPNULLS
,KEEPIDENTITY
,TABLOCK
,ORDER (Id ASC, Created ASC)
,ROWS_PER_BATCH = 57380362
)
ROWS_PER_BATCH = 57380362 - I know this because I previously exported the data. Column Id is of type int with Identity defined, not null. Column Created is of type datetime, not null.
Digression: Some may ask why such a sort order "Id ASC, Created ASC", it's because the table that will be populated with data is a staging table that will be switched in as one of the partitions (partitioning key is Created) and since unique indexes on partitioned tables have to have partitioning column as part of the key my "clustered, unique, primary key" index has key definition on (Id ASC, Created ASC).
The staging table with "clustered, unique, primary key" index on (Id ASC, Created ASC) is empty before import, I want to take advantage of minimal logging and importing into clustered index at the same time.
The format file contains such information regarding key columns:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="NativeFixed" LENGTH="4"/>
...
<FIELD ID="31" xsi:type="NativeFixed" LENGTH="8"/>
...
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="Id" xsi:type="SQLINT"/>
...
<COLUMN SOURCE="31" NAME="Created" xsi:type="SQLDATETIME"/>
...
</ROW>
</BCPFORMAT>
Now what am I missing in my procedure or what am I doing wrong here that forces SQL Server to insert Sort Operator into the execution plan ?? I think I don't have to explain why I want that Sort operator out of the plan :) Execution Plan Link
Versions: Microsoft SQL Server 2012 - 11.0.5548.0 (X64) Developer Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1)