Search⌘ K
AI Features

DynamoDB: Secondary Indexes and Querying

Explore how to efficiently retrieve data from Amazon DynamoDB using query and scan operations. Learn to optimize queries with filters and projections and understand how to design and maintain Local and Global Secondary Indexes for scalable access patterns.

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.

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 ...