perf(audit-log,site-event-log): channel+age purge index with varchar binding; instance+timestamp event index (AUD-5, SEL-1, WP6 task 6.5)

AUD-5: PurgeChannelOlderThanAsync binds @channel as varchar(32) and the model
gains IX_AuditLog_Channel_Occurred (Category, OccurredAtUtc). The migration +
partition-aligned SQL script land in the WP's final migration commit
(AuditLogChannelOccurredIndex).
  sqlcmd -I, SQL_Latin1_General_CP1_CI_AS, scratch DB with 1M AuditLog rows
  (6 channels, clustered (OccurredAtUtc, EventId), ScriptTrace override 30 d),
  steady-state per-channel tick (nothing left to purge):
  before (nvarchar, old indexes): 5,858 logical reads, 124 ms
  after  (varchar + new index):   6 logical reads, 0 ms
  new index but nvarchar param:   4,747 logical reads, 114 ms (binding matters)

SEL-1: site_events idx_events_instance -> idx_events_instance_timestamp
(instance_id, timestamp), dropped/rebuilt in place by SiteEventLogSchema.Apply
like #123; the storage-cap size model now charges 4 x timestamp per row.
  sqlite3, 2.7M rows / 20 instances, instance-filtered 501-row page:
  before: SEARCH idx_events_instance + USE TEMP B-TREE FOR ORDER BY; 396 ms
  after:  SEARCH idx_events_instance_timestamp (temp b-tree for id tiebreak only); 0.7 ms
This commit is contained in:
Joseph Doherty
2026-12-03 10:00:42 -05:00
parent 9f03726fc7
commit 04e3969071
10 changed files with 64 additions and 16 deletions
@@ -235,6 +235,14 @@ public class AuditLogEntityTypeConfiguration : IEntityTypeConfiguration<AuditLog
.IsDescending(false, false, true)
.HasDatabaseName("IX_AuditLog_Channel_Status_Occurred");
// AUD-5: the per-channel retention purge (DELETE TOP (@batch) ... WHERE Category =
// @channel AND OccurredAtUtc < @threshold) had no index with the two predicates
// adjacent — Status sits between them in IX_AuditLog_Channel_Status_Occurred — so each
// batch scanned most of the table. Created via raw SQL ON ps_AuditLog_Month so it stays
// partition-aligned for the partition-switch purge (AuditLogChannelOccurredIndex).
builder.HasIndex(e => new { e.Channel, e.OccurredAtUtc })
.HasDatabaseName("IX_AuditLog_Channel_Occurred");
builder.HasIndex(e => new { e.Target, e.OccurredAtUtc })
.IsDescending(false, true)
.HasFilter("[Target] IS NOT NULL")
@@ -769,10 +769,10 @@ VALUES
pBatch.Value = (int)Math.Min(batchSize, maxRows - totalDeleted);
cmd.Parameters.Add(pBatch);
var pChannel = cmd.CreateParameter();
pChannel.ParameterName = "@channel";
pChannel.Value = channel;
cmd.Parameters.Add(pChannel);
// AUD-5: bind as varchar(32) to match the Category column. An untyped string
// parameter goes out as nvarchar, which under a SQL collation converts the
// column side and rules out a seek on IX_AuditLog_Channel_Occurred.
AddParameter(cmd, "@channel", SqlDbType.VarChar, size: 32, channel);
var pThreshold = cmd.CreateParameter();
pThreshold.ParameterName = "@threshold";
@@ -90,9 +90,10 @@ public class EventLogPurgeService : BackgroundService
private const double TablePageFillFactor = 0.78;
/// <summary>
/// Number of secondary indexes on <c>site_events</c>: timestamp, instance_id,
/// Number of secondary indexes on <c>site_events</c>: timestamp, (instance_id, timestamp),
/// (event_type, timestamp) and (severity, timestamp). The sampled <c>indexed_bytes</c>
/// charges every covered column once per index, hence <c>3 * octet_length(timestamp)</c>.
/// charges every covered column once per index, hence <c>4 * octet_length(timestamp)</c>
/// (SEL-1 added timestamp to the instance index).
/// </summary>
private const int IndexedColumnCount = 4;
@@ -734,7 +735,7 @@ public class EventLogPurgeService : BackgroundService
+ octet_length(severity) + COALESCE(octet_length(instance_id), 0)
+ octet_length(source) + octet_length(message)
+ COALESCE(octet_length(details), 0) AS payload_bytes,
(3 * octet_length(timestamp)) + octet_length(event_type)
(4 * octet_length(timestamp)) + octet_length(event_type)
+ octet_length(severity) + COALESCE(octet_length(instance_id), 0)
AS indexed_bytes
FROM site_events LIMIT {SizeSampleRows}
@@ -746,7 +747,7 @@ public class EventLogPurgeService : BackgroundService
+ octet_length(severity) + COALESCE(octet_length(instance_id), 0)
+ octet_length(source) + octet_length(message)
+ COALESCE(octet_length(details), 0) AS payload_bytes,
(3 * octet_length(timestamp)) + octet_length(event_type)
(4 * octet_length(timestamp)) + octet_length(event_type)
+ octet_length(severity) + COALESCE(octet_length(instance_id), 0)
AS indexed_bytes
FROM site_events ORDER BY rowid DESC LIMIT {SizeSampleRows}
@@ -67,7 +67,8 @@ public static class SiteEventLogSchema
details TEXT
);
CREATE INDEX IF NOT EXISTS idx_events_timestamp ON site_events(timestamp);
CREATE INDEX IF NOT EXISTS idx_events_instance ON site_events(instance_id);
DROP INDEX IF EXISTS idx_events_instance;
CREATE INDEX IF NOT EXISTS idx_events_instance_timestamp ON site_events(instance_id, timestamp);
DROP INDEX IF EXISTS idx_events_type;
DROP INDEX IF EXISTS idx_events_severity;
CREATE INDEX IF NOT EXISTS idx_events_type_timestamp ON site_events(event_type, timestamp);
@@ -85,6 +86,11 @@ public static class SiteEventLogSchema
// part of LocalDb's replicated-table digest, so a pair may upgrade one node at a
// time as far as this change is concerned.
//
// SEL-1 (2026-12-03): the instance_id index got the same treatment — the Instances
// page drill-in filters by instance and orders by (timestamp, id), which the old
// single-column idx_events_instance could only answer by fetching and sorting every
// row of that instance. Dropped and rebuilt in place on first boot, like #123.
//
// The query service also supports keyword search via leading-wildcard
// LIKE on message/source. A leading-wildcard LIKE cannot use a B-tree
// index, so that path intentionally full-scans; severity/event_type/