Querying DynamoDB without Primary Key
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
DynamoDB, a fully managed NoSQL database provided by AWS, is designed to offer seamless scalability and high performance for applications. One of the core features of DynamoDB is its use of primary keys, which ensures efficient query operations. However, what if your application requires querying without a primary key or supplementing queries with additional attributes? In this article, we delve into various strategies to query DynamoDB without relying solely on primary keys.
Understanding DynamoDB's Table Structure
DynamoDB tables store data using a combination of primary keys and attributes:
- Primary Key: This is a field or set of fields that uniquely identify an item. It can be either a partition key or a combination of a partition key and a sort key.
- Attributes: These are additional fields that describe an item but are not part of the primary key.
Challenges of Querying Without Primary Key
Querying using a primary key is highly efficient because DynamoDB is optimized for this access pattern. When querying without specifying a primary key, you may encounter several challenges:
- Inefficient Data Access: Without a primary key, scans might be necessary, leading to full table scans which are costly in terms of read capacity units (RCUs) and slower response times.
- Limited Query Options: Querying without a primary key might restrict you to using filters and scans, which can be less efficient for large datasets.
Strategies for Querying Without a Primary Key
1. Utilizing Secondary Indexes
Secondary indexes allow you to query data on attributes that aren't part of the primary key. There are two types of secondary indexes:
- Global Secondary Index (GSI): Allows queries on any attribute. It maintains a full copy of the indexed attributes, which allows for efficient querying.
- Local Secondary Index (LSI): An index that you can create for attributes within the same partition key but allows a different sort key.
Example:
2. Using Filter Expressions
Filter expressions can be applied after querying with a scan operation. They're useful for narrowing down results based on non-key attributes:
3. Taking Advantage of Scan Operations
While less efficient due to the full table scan, you can use the scan operation for flexibility. Filters can help reduce the data size after scanning:
4. Implementing Composite Attributes
Combining multiple attributes into a single attribute can help create alternate keys:
Best Practices
To optimize querying without a primary key, consider the following best practices:
- Use Indexes Judiciously: Balance performance gain from GSIs and LSIs with the additional storage and RCUs they require.
- Avoid Full Table Scans: Limit scans using filter expressions and projection expressions wherever possible.
- Monitor Usage Patterns: Regularly review and optimize your indexes and queries based on usage patterns.
Summary
DynamoDB provides flexibility for querying beyond primary keys through options like secondary indexes, filter expressions, and scan operations. Each method comes with trade-offs in terms of efficiency and cost. Assessing the application needs and usage patterns is crucial for choosing the right approach. Below is a table summarizing querying strategies without a primary key:
| Query Strategy | Use Case | Pros | Cons |
| Global Secondary Index (GSI) | Query on non-key attributes | Fast access Supports multiple attributes | Increases storage and RCUs Complex design |
| Local Secondary Index (LSI) | Query within a partition | Efficient sorting Minimal overhead | Limited to partition Design complexity |
| Scan with Filters | Large, dynamic queries | Flexibility Simple to implement | High cost Slow response |
| Composite Attributes | Custom key alternatives | Creative query solutions No extra index | Complexity in design Potential data redundancy |
By leveraging these techniques and best practices, you can optimize your use of DynamoDB for complex query patterns without relying solely on primary keys.
Related reading
- Querying DynamoDB without PrimaryKey with Lambda
- Querying for greatest value of Range key on AWS DynamoDb
- Quick way to get AWS Account number from the AWS CLI tools?
- R and data.table on AWS
- Questions for reading data from JDBC source in DataStream Flink
- Questions while I'm making distributed key-value store
- RabbitMQ on EC2 Consuming Tons of CPU
- Rails based EC2 AMI

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.