Query, Scan, indexes and pagination
Choose the read operation
GetItem reads a known full primary key. Query selects items with a specified partition key and can narrow the sort key using a condition. Scan examines a table or index without requiring a partition key. For USER#12, use Query with SK beginning CART# to retrieve just its cart lines. Scan is useful for bounded administrative jobs, not a default for every customer request. A filter acts after data is read, so it does not save the read capacity of discarded items.
Read every page
A Query or Scan reads up to 1 MB per service page before filtering (or stops earlier at a supplied Limit). Limit is the number of items evaluated, not a promise of that many matches. Pass a returned LastEvaluatedKey as ExclusiveStartKey on the next request, keeping the other query parameters consistent. Continue until the key is absent or empty; an empty Items array alone is not a stopping condition. The example is a synthetic low-level response summary, not an actual AWS transcript. In a real application bound page counts and time, and persist the continuation key if pausing.
Page 1: Items=[], LastEvaluatedKey={PK: USER#12, SK: CART#A01}Page 2: Items=[CART#B02], LastEvaluatedKey absentCombined result: [CART#B02]Index a second access pattern
To find all carts containing a particular SKU, consider a global secondary index (GSI) with product as its partition key and customer/line as its sort key. A GSI can use different keys and projects selected attributes; only items with the required index keys appear. Its reads are eventually consistent, so recent writes may not appear immediately. A local secondary index (LSI) keeps the table partition key and uses another sort key; it is defined at table creation and has an item-collection size limit. Both add storage and write overhead. A GSI cannot fetch non-projected attributes directly from the base table.
Consistency is a requirement
If a customer must see a confirmed update immediately, a strongly consistent read on the base table or LSI is an option within its supported scope. Eventual reads may be stale; strong reads cost more units. A strongly consistent Scan does not make the entire multi-item scan a snapshot. Do not retry with stronger consistency on a GSI, which does not support it. Practice: choose the operation for a known cart line, all carts for a customer, and a cross-customer SKU search.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Query, Scan, indexes and pagination — knowledge check
Apply DynamoDB concepts to fictional shopping workloads.
Which read retrieves one known PK and SK?
A filtered Query returns no items but a continuation key. What next?
What is a useful index for finding carts by SKU across customers?
Does a post-read filter reduce read capacity consumed by discarded items?
Which index supports strong reads?