...

/

DynamoDB: Secondary Indexes and Querying

DynamoDB: Secondary Indexes and Querying

Explore how to efficiently retrieve and manage data in DynamoDB through queries, scans, filters, and effective indexing strategies.

Before building performant applications with DynamoDB, we need to understand how data is accessed and retrieved efficiently. This lesson focused on the fundamental operations and indexing strategies that shape how we interact with data at scale.

Press + to interact

Query vs. scan

DynamoDB provides two primary operations to retrieve multiple items:

  • Query operation: A query is used when we know the partition key and want to retrieve all (or a subset of) items that share that key. Queries can be efficient, as DynamoDB uses the partition key to locate the appropriate data partitions and then applies optional filters to narrow the results further.

  • Scan operation: A scan reads every item in the table, regardless of key. This makes scans significantly more resource-intensive, especially as the table size grows. Scans are often used during data migrations, audits, or administrative tasks, not during routine application queries.

Note: When possible, we should always prefer a query over a scan, especially in latency-sensitive applications. Queries use partition key targeting, while scans consume more read capacity and take longer.

The illustration below depicts the impact of a scan on the provisioned throughput of a DynamoDB table. Notice that as the size of requests becomes nonuniform, throttling (shown by the rectangle bar) becomes more frequent.

Press + to interact
Impact of scan on the provisioned throughput of a DynamoDB table
Impact of scan on the provisioned throughput of a DynamoDB table

Here are some of the strategies to minimize the impact of a scan operation:

  • Reduce page size using the Limit parameter: Setting a smaller page size allows scans to consume fewer read capacity units per request. This spreads out the load and ...