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
@@ -40,6 +40,19 @@ public class AuditLogEntryConfiguration : IEntityTypeConfiguration<AuditLogEntry
builder.HasIndex(a => a.EntityType);
builder.HasIndex(a => a.EntityId);
builder.HasIndex(a => a.Action);
// Review-remediation composite indexes (ReviewRemediationIndexes). The single-column
// Timestamp/User/EntityType indexes above are kept: (Timestamp DESC, Id DESC) only
// subsumes Timestamp conceptually, and dropping existing indexes is deliberately
// left to a later, measured change.
builder.HasIndex(a => new { a.EntityType, a.Timestamp })
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLogEntries_EntityType_Timestamp");
builder.HasIndex(a => new { a.User, a.Timestamp })
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLogEntries_User_Timestamp");
builder.HasIndex(a => new { a.Timestamp, a.Id })
.IsDescending(true, true)
.HasDatabaseName("IX_AuditLogEntries_Timestamp_Id");
builder.HasIndex(a => a.BundleImportId).HasDatabaseName("IX_AuditLogEntries_BundleImportId");
}
}
@@ -229,5 +229,17 @@ public class AuditLogEntityTypeConfiguration : IEntityTypeConfiguration<AuditLog
.IsDescending(false, true)
.HasFilter("[Target] IS NOT NULL")
.HasDatabaseName("IX_AuditLog_Target_Occurred");
// Review-remediation indexes (ReviewRemediationIndexes migration): the Audit
// Log page's Kind and Actor filters ordered newest-first. Kind is a persisted
// computed column (indexable). Created via raw SQL ON ps_AuditLog_Month so they
// stay partition-aligned for the partition-switch purge.
builder.HasIndex(e => new { e.Kind, e.OccurredAtUtc })
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLog_Kind_Occurred");
builder.HasIndex(e => new { e.Actor, e.OccurredAtUtc })
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLog_Actor_Occurred");
}
}
@@ -0,0 +1,72 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
/// <summary>Review-remediation composite indexes on AuditLog (partition-aligned, raw SQL) and AuditLogEntries.</summary>
public partial class ReviewRemediationIndexes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_AuditLogEntries_EntityType_Timestamp",
table: "AuditLogEntries",
columns: new[] { "EntityType", "Timestamp" },
descending: new[] { false, true });
migrationBuilder.CreateIndex(
name: "IX_AuditLogEntries_Timestamp_Id",
table: "AuditLogEntries",
columns: new[] { "Timestamp", "Id" },
descending: new bool[0]);
migrationBuilder.CreateIndex(
name: "IX_AuditLogEntries_User_Timestamp",
table: "AuditLogEntries",
columns: new[] { "User", "Timestamp" },
descending: new[] { false, true });
// AuditLog is monthly-partitioned (ps_AuditLog_Month) with a partition-switch
// purge, so these two indexes MUST be partition-aligned. EF's CreateIndex cannot
// express the ON ps_AuditLog_Month(OccurredAtUtc) clause -> raw SQL, exactly as
// IX_AuditLog_Node_Occurred (AddAuditLogSourceNode). Kind is a PERSISTED computed
// column, so it is indexable. On a large production AuditLog consider adding
// WITH (ONLINE = ON) (Enterprise edition) to the generated script.
migrationBuilder.Sql(@"
CREATE NONCLUSTERED INDEX IX_AuditLog_Kind_Occurred
ON dbo.AuditLog (Kind, OccurredAtUtc DESC)
ON ps_AuditLog_Month(OccurredAtUtc);");
migrationBuilder.Sql(@"
CREATE NONCLUSTERED INDEX IX_AuditLog_Actor_Occurred
ON dbo.AuditLog (Actor, OccurredAtUtc DESC)
ON ps_AuditLog_Month(OccurredAtUtc);");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_AuditLogEntries_EntityType_Timestamp",
table: "AuditLogEntries");
migrationBuilder.DropIndex(
name: "IX_AuditLogEntries_Timestamp_Id",
table: "AuditLogEntries");
migrationBuilder.DropIndex(
name: "IX_AuditLogEntries_User_Timestamp",
table: "AuditLogEntries");
migrationBuilder.Sql(@"
IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_Actor_Occurred' AND object_id = OBJECT_ID('dbo.AuditLog'))
DROP INDEX IX_AuditLog_Actor_Occurred ON dbo.AuditLog;");
migrationBuilder.Sql(@"
IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_Kind_Occurred' AND object_id = OBJECT_ID('dbo.AuditLog'))
DROP INDEX IX_AuditLog_Kind_Occurred ON dbo.AuditLog;");
}
}
}
@@ -98,6 +98,18 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
b.HasIndex("User");
b.HasIndex("EntityType", "Timestamp")
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLogEntries_EntityType_Timestamp");
b.HasIndex("Timestamp", "Id")
.IsDescending()
.HasDatabaseName("IX_AuditLogEntries_Timestamp_Id");
b.HasIndex("User", "Timestamp")
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLogEntries_User_Timestamp");
b.ToTable("AuditLogEntries");
});
@@ -1871,6 +1883,14 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
b.HasIndex("ParentExecutionId")
.HasDatabaseName("IX_AuditLog_ParentExecution");
b.HasIndex("Actor", "OccurredAtUtc")
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLog_Actor_Occurred");
b.HasIndex("Kind", "OccurredAtUtc")
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLog_Kind_Occurred");
b.HasIndex("SourceNode", "OccurredAtUtc")
.HasDatabaseName("IX_AuditLog_Node_Occurred");