feat(configdb): ReviewRemediationIndexes migration — AuditLog (Kind, Actor) + AuditLogEntries composite indexes

AuditLog indexes are raw SQL ON ps_AuditLog_Month so they stay partition-aligned for the switch purge.
This commit is contained in:
Joseph Doherty
2026-10-22 09:41:51 -04:00
parent 88273693f9
commit 1627513fdb
7 changed files with 2359 additions and 0 deletions
@@ -0,0 +1,71 @@
-- ReviewRemediationIndexes (2026-10-22). Run with QUOTED_IDENTIFIER ON (sqlcmd -I, or keep
-- this header): AuditLog has persisted computed columns / filtered indexes and SQL Server
-- raises error 1934 otherwise.
-- AuditLog is monthly-partitioned; the two AuditLog indexes are created ON
-- ps_AuditLog_Month(OccurredAtUtc) so they stay partition-aligned and the partition-switch
-- purge keeps working. Building them scans the whole AuditLog: on a large production table
-- run in a quiet window, and on Enterprise edition add WITH (ONLINE = ON) to the two
-- AuditLog CREATE INDEX statements (and to the AuditLogEntries ones if that table is large).
SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
GO
BEGIN TRANSACTION;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20261022133907_ReviewRemediationIndexes'
)
BEGIN
CREATE INDEX [IX_AuditLogEntries_EntityType_Timestamp] ON [AuditLogEntries] ([EntityType], [Timestamp] DESC);
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20261022133907_ReviewRemediationIndexes'
)
BEGIN
CREATE INDEX [IX_AuditLogEntries_Timestamp_Id] ON [AuditLogEntries] ([Timestamp] DESC, [Id] DESC);
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20261022133907_ReviewRemediationIndexes'
)
BEGIN
CREATE INDEX [IX_AuditLogEntries_User_Timestamp] ON [AuditLogEntries] ([User], [Timestamp] DESC);
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20261022133907_ReviewRemediationIndexes'
)
BEGIN
CREATE NONCLUSTERED INDEX IX_AuditLog_Kind_Occurred
ON dbo.AuditLog (Kind, OccurredAtUtc DESC)
ON ps_AuditLog_Month(OccurredAtUtc);
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20261022133907_ReviewRemediationIndexes'
)
BEGIN
CREATE NONCLUSTERED INDEX IX_AuditLog_Actor_Occurred
ON dbo.AuditLog (Actor, OccurredAtUtc DESC)
ON ps_AuditLog_Month(OccurredAtUtc);
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20261022133907_ReviewRemediationIndexes'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20261022133907_ReviewRemediationIndexes', N'10.0.7');
END;
COMMIT;
GO