DynamoDB interview question · Most asked
Query or Scan: what is the difference, and why avoid Scan?
Summary
Query reads one item collection (one partition key value), optionally narrowed by a sort key condition, so it reads only the relevant items. Scan reads every item in the table or index and then optionally filters, consuming capacity for everything it reads.
Longer Answer
On a large table a Scan is slow, expensive and can consume capacity needed by live traffic. Legitimate uses: one-off migrations and backfills (use parallel scan with Segment/TotalSegments and rate limiting), small tables, or exports (prefer the managed export to S3, which consumes no table capacity). If a feature needs a Scan in its request path, the data model is missing an index or key design.
Key Points
- Query = one partition, targeted.
- Scan = whole table.
- Scan only for backfills/exports/small tables.
- Export to S3 for analytics.
More most-asked DynamoDB questions
- What are DynamoDB Streams, and how are they used?
- How do conditional writes work, and how do you implement optimistic locking in DynamoDB?
- What is DynamoDB, and what workloads is it a good fit for?
- What is a hot partition, and how do you avoid one?
- How do you approach data modelling in DynamoDB, and how is it different from relational design?
- Explain partition keys, sort keys and the two kinds of primary key.
- Global secondary index or local secondary index: what is the difference?
- DynamoDB or a relational database such as Aurora PostgreSQL: how do you decide?