Files
ScadaBridge/docs/plans/sql/ReviewRemediationIndexes.sql
T
Joseph Doherty 1627513fdb 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.
2026-10-22 09:42:02 -04:00

72 lines
2.3 KiB
Transact-SQL

-- 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