I'm on SQL Server 2022 (RTM-GDR) (KB5021522) - 16.0.1050.5 (X64) doing some bulk import work in java using a modified version of the SQLServerBulkCopy class, that eventually streams data to the database via INSERT BULK stream.
My table and bulk command:
CREATE TABLE dbo.dummy (
id INT NOT NULL
, anotherInt INT NULL
, ix INT NULL
, ix2 INT NULL
, ix3 INT NULL
, z NUMERIC(19,12) NOT NULL
, CONSTRAINT PK__dummy__51AEE9A1E72E320E PRIMARY KEY CLUSTERED(
id
, z) WITH (ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT BULK dummy ([id] INT , [z] NUMERIC(19, 12) ) with (TABLOCK, ORDER (id ASC,z ASC))
I have pre-sorted the data being bulked to exactly mirror the primary clustered key of the table, as well as attached ORDER to the INSERT BULK command. But for some reason, I still get a sort operator in the query plan:
Paste the plan: https://www.brentozar.com/pastetheplan/?id=HJ_bjA9ea
Why does it make the extra sort step and how do I avoid it? It seems that sometimes (depending on something I don't know), SQL Server can skip sorting again, but most of the times, it doesn't. Also, interestingly enough, if I bulk stream unsorted data, then it crashes and complains about data not being sorted correctly.
If I put the data into a file and BULK IMPORT it with ORDER specified, it seem to work better but I'd like to skip that extra work step.
