Amazon DynamoDB Query for Items whose key contains a substring
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction to Amazon DynamoDB Query
Amazon DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS) that offers fast and predictable performance with seamless scalability. DynamoDB is particularly effective for applications that require consistent latency and massive throughput. In this guide, we'll focus on querying items whose key contains a specific substring using DynamoDB.
Understanding DynamoDB Query Operations
DynamoDB supports two primary operations to access data:
- Scan: Examines every item in the table and returns all data attributes by default. This operation can be resource-intensive and is not usually recommended for large datasets when efficiency is a priority.
- Query: More efficient than scanning because it can retrieve items based on a primary key or global secondary index. The query operation allows you to filter results, but this filtering happens after the query processes all its data.
For DynamoDB tables with a large number of items, the `Query` operation is usually preferred due to its performance advantages.
Using Query with Key Substring
DynamoDB's query operation on string data types is limited to using either the partition key or sort key with full match conditions or sort key with specific conditions such as begins with. Unfortunately, DynamoDB does not support full-fledged `contains` operations on keys directly during a query operation. To handle substring match scenarios, an additional approach dealing with attribute values rather than keys can be taken. Here we'll consider best practices and strategies associated with this approach.
Approaches to Solve Key Substring Queries
- Design Adjustment: Often, problems arise from requiring features that differ from DynamoDB's typical access patterns. Re-evaluate your data model to see if you can modify design, possibly through denormalization or indexing attributes differently, to facilitate expected queries.
- Use of Filter Expressions: While direct substring queries aren’t possible with the keys themselves, you can query against attributes (non-key values) using filter expressions.
Related reading
- Amazon EC2 - Swap root instance store device with EBS device
- Amazon EC2 as web server?
- Amazon EC2 EBS backup AMI vs Snapshot
- Amazon EC2 ELB alarm - which instance is unhealthy?
- Amazon RDS - Online only when needed?
- Amazon RDS Aurora vs RDS MySQL vs MySQL on EC2?
- Amazon EC2 Free tier - how many instances can I run
- Amazon EC2 how to convert an existing PV AMI to HVM

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.