Skip to content

Commit 410a6dd

Browse files
committed
perf(mssql): enable OPTIMIZE_FOR_SEQUENTIAL_KEY on primary keys for Streams and Messages
- Added WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON) to clustered primary keys on __schema__.Streams (PK_Streams) and __schema__.Messages (PK_Events) - Improves insert throughput and reduces latch contention on identity-based keys - No changes to schema shape, constraints, or indexes beyond PK optimization This improves concurrent insert performance under high write workloads by allowing SQL Server to better handle last-page insert contention on sequential identity keys. See docs on OPTIMIZE_FOR_SEQUENTIAL_KEY: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-index-option-transact-sql?view=sql-server-ver17#optimize_for_sequential_key---on--off-
1 parent dc7d583 commit 410a6dd

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/SqlServer/src/Eventuous.SqlServer/Scripts/1_Schema.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ IF OBJECT_ID('__schema__.Streams', 'U') IS NULL
1010
StreamId INT IDENTITY (1,1) NOT NULL,
1111
StreamName NVARCHAR(850) NOT NULL,
1212
[Version] INT DEFAULT (-1) NOT NULL,
13-
CONSTRAINT PK_Streams PRIMARY KEY CLUSTERED (StreamId),
13+
CONSTRAINT PK_Streams PRIMARY KEY CLUSTERED (StreamId) WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON),
1414
CONSTRAINT UQ_StreamName UNIQUE NONCLUSTERED (StreamName),
1515
CONSTRAINT CK_VersionGteNegativeOne CHECK ([Version] >= -1)
1616
);
@@ -28,7 +28,7 @@ IF OBJECT_ID('__schema__.Messages', 'U') IS NULL
2828
JsonData NVARCHAR(MAX) NOT NULL,
2929
JsonMetadata NVARCHAR(MAX) NOT NULL,
3030
Created DATETIME2(7) NOT NULL,
31-
CONSTRAINT PK_Events PRIMARY KEY CLUSTERED (GlobalPosition),
31+
CONSTRAINT PK_Events PRIMARY KEY CLUSTERED (GlobalPosition) WITH (OPTIMIZE_FOR_SEQUENTIAL_KEY = ON),
3232
CONSTRAINT FK_MessageStreamId FOREIGN KEY (StreamId) REFERENCES __schema__.Streams (StreamId),
3333
CONSTRAINT UQ_StreamIdAndStreamPosition UNIQUE NONCLUSTERED (StreamId, StreamPosition),
3434
CONSTRAINT UQ_StreamIdAndMessageId UNIQUE NONCLUSTERED (StreamId, MessageId),

0 commit comments

Comments
 (0)