perf(store-and-forward): rank sweep lanes over a covering narrow index, join payloads for the page only (SF-3, WP6 task 6.3)
The fair-paging sweep computed ROW_NUMBER() over every column of every due
row, so each sweep sorted the whole due backlog with payloads into temp
storage. The ranking now projects only id + lane keys over the new covering
idx_sf_messages_lane (status, category, target, created_at,
last_attempt_at_ms, retry_interval_ms, id), takes the page of ids, and joins
back by primary key. The index is created in place by StoreAndForwardSchema
(idempotent, local only — not part of CDC capture). idx_sf_messages_category
is kept (still used by parked-count filters).
EXPLAIN QUERY PLAN + timing, sqlite3 3.54, 200,000 due rows x ~1 KB payload,
20 targets x 3 categories, LIMIT 500:
before: SEARCH idx_sf_messages_status_created (status=?) + USE TEMP B-TREE
FOR ORDER BY (window over full rows) + outer temp b-tree; 1.29 s
after: SEARCH USING COVERING INDEX idx_sf_messages_lane (no window sort),
outer temp b-tree over (id, rank, created_at) only, PK join of 500
rows; 0.15 s
This commit is contained in:
@@ -111,6 +111,19 @@ public static class StoreAndForwardSchema
|
||||
"CREATE INDEX IF NOT EXISTS idx_sf_messages_status_created ON sf_messages(status, created_at)";
|
||||
orderIndex.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// SF-3: covering index for the sweep's fair lane ranking. Leading (status, category,
|
||||
// target, created_at) matches ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY
|
||||
// created_at) so the window reads rows already in lane order with no sort, and the
|
||||
// trailing due-predicate columns + id make it covering, so the ranking never reads a
|
||||
// payload. Applied in place on every boot (idempotent); local only — indexes are not
|
||||
// part of LocalDb's CDC capture.
|
||||
using (var laneIndex = connection.CreateCommand())
|
||||
{
|
||||
laneIndex.CommandText =
|
||||
"CREATE INDEX IF NOT EXISTS idx_sf_messages_lane ON sf_messages(status, category, target, created_at, last_attempt_at_ms, retry_interval_ms, id)";
|
||||
laneIndex.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -211,16 +211,28 @@ public class StoreAndForwardStorage
|
||||
// hundreds of due rows contributes one row per "round" like every other
|
||||
// lane instead of owning the whole page. Within a lane rank order == created_at
|
||||
// order, so per-target FIFO is preserved for the service's GroupBy.
|
||||
//
|
||||
// SF-3: the ranking runs over a NARROW projection (id + lane keys) that
|
||||
// idx_sf_messages_lane covers in (category, target, created_at) order, so the window
|
||||
// needs no sort and never touches a payload; only the chosen page of ids is joined
|
||||
// back for the full rows. Ranking over every column used to sort the whole due
|
||||
// backlog — payloads included — on every sweep.
|
||||
cmd.CommandText = $@"
|
||||
SELECT {cols}
|
||||
FROM (
|
||||
SELECT {cols},
|
||||
ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at ASC) AS lane_rank
|
||||
FROM sf_messages
|
||||
WHERE {due}
|
||||
WITH page AS (
|
||||
SELECT id, lane_rank, created_at
|
||||
FROM (
|
||||
SELECT id, created_at,
|
||||
ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at ASC) AS lane_rank
|
||||
FROM sf_messages
|
||||
WHERE {due}
|
||||
)
|
||||
ORDER BY lane_rank ASC, created_at ASC
|
||||
LIMIT @limit
|
||||
)
|
||||
ORDER BY lane_rank ASC, created_at ASC
|
||||
LIMIT @limit";
|
||||
SELECT {SweepColumnsM}
|
||||
FROM page p
|
||||
JOIN sf_messages m ON m.id = p.id
|
||||
ORDER BY p.lane_rank ASC, p.created_at ASC";
|
||||
cmd.Parameters.AddWithValue("@limit", limit);
|
||||
}
|
||||
else
|
||||
@@ -238,6 +250,15 @@ public class StoreAndForwardStorage
|
||||
return await ReadMessagesAsync(cmd);
|
||||
}
|
||||
|
||||
/// <summary>The sweep's column list qualified with the <c>m</c> alias (SF-3 join-back).</summary>
|
||||
private static readonly string SweepColumnsM =
|
||||
string.Join(", ", new[]
|
||||
{
|
||||
"id", "category", "target", "payload_json", "retry_count", "max_retries",
|
||||
"retry_interval_ms", "created_at", "last_attempt_at", "status", "last_error", "origin_instance",
|
||||
"execution_id", "source_script", "parent_execution_id",
|
||||
}.Select(c => "m." + c));
|
||||
|
||||
/// <summary>
|
||||
/// Updates a message after a delivery attempt.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user