Repository navigation
Conversation
… GSI query Adds an assigned-to-index GSI on the devices table and converts getDevicesAssignedToUser() from a full-table Scan with FilterExpression to a Query against the new index. This is the highest-value scan (runs per user on listing and on every device (re)assignment). Closes review finding M13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
M13 — Convert full-table Scans to GSI queries
Problem
Several Lambda handlers perform DynamoDB
Scanoperations withFilterExpression, which read the entire table before filtering. As tables grow these become slow and costly (RCU-heavy), and they run on hot paths.Fix (this PR)
Converts the highest-value scan —
getDevicesAssignedToUser()inlambda/api-users/index.ts— from a full-tableScanto aQueryagainst a newassigned-to-indexGSI on the devices table.This scan is the cheapest high-value conversion because it runs:
Changes:
lib/storage-construct.ts— addassigned-to-indexGSI (assigned_toHASH,last_seenRANGE,ProjectionType.ALL).lambda/api-users/index.ts—getDevicesAssignedToUser()now usesQueryCommandwithKeyConditionExpressionon the GSI instead ofScanCommand+FilterExpression.Verification
npm run build(tsc) — clean.npm test(vitest) — 451 passed.storage-construct.test.tsasserts all three GSIs (fleet-index,status-index,assigned-to-index).Deferred scans (documented, not addressed here)
Each of these requires either a new access-key design or a table-level rearchitecture and is deferred to a follow-up to keep this PR minimal and low-risk:
lambda/api-activity(x6) — time-range/aggregation scans over activity data; best addressed by a dedicated time-series access pattern (partition-by-day + sort key) rather than a single GSI. Larger design change.lambda/api-devices/index.ts:129— list-all-devices scan; a table-wide listing has no single high-cardinality key to index on. Candidate for pagination + a sparse status/fleet GSI already present; needs product decision on default filter.lambda/api-alerts/index.ts:148— alert listing scan; needs an alert-status/time GSI design.lambda/api-users/index.ts:147— a second scan on a different path; folded conceptually with the assigned-to pattern but the remaining call site needs its own key model.lambda/api-commands/index.ts:416— command history scan; needs a device+time GSI.lambda/analytics/chat-query.ts:488— analytics scan on a path that already tolerates higher latency; lowest priority.Follow-up tickets should design the appropriate GSIs / time-series keys for each before conversion.
Closes review finding M13