Query in Dynamo DB without hashkey or scan
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Amazon DynamoDB, querying involves retrieving data from the database by filtering by specific attributes. Generally, to execute an efficient query, you need to use the primary key or a secondary index. However, the focus of this discussion will be methods to query the database without using the hash key or performing a full table scan.
Technical Background
DynamoDB utilizes two primary key types:
- Partition Key: A single attribute that uniquely identifies items in a table. Its value determines the partition for data distribution.
- Composite Key (Partition Key + Sort Key): Consists of two attributes that uniquely identify an item.
The Scan operation allows you to search through all items in a table, which can be inefficient and costly for large datasets. It's generally not recommended when you want to query without the hash key, as it can significantly degrade performance.
Querying Without Hash Key
Direct querying without using a partition key requires strategic planning during the design phase. Here are several methods to achieve this:
Utilize Secondary Indexes
Secondary indexes allow you to query data in new ways. Two types of secondary indexes are supported:
- Global Secondary Index (GSI): Allows querying on non-partition key attributes, and can have both a different partition and sort key.
- Local Secondary Index (LSI): Limited to the same partition key as the table but allows a different sort key.
Example Python code using a GSI:
Filter Expressions
Filter expressions are used to refine the results of queries or scans. Though they don’t reduce the read operation size for querying, they can help in refining the search results.
BatchGetItem as an Alternative
When you need to retrieve multiple items, and you cannot afford to use a partition key, you can still exploit BatchGetItem function. It's useful to fetch items by specifying keys and limiting the number of read requests.
Example:
Limitations and Best Practices
- Increased Cost: Secondary indexes can increase costs because they count as additional read and write units.
- Limitations on LSI: LSIs are constrained by the partition key, which might not be ideal for every use case requiring varied access patterns.
- Index Consistency: Updates to an item will be automatically applied to all associated indexes, which can potentially lead to latency if there are large volumes of updates.
- Regular Index Assessment: Regularly review and understand your query patterns and indexes to avoid unnecessary index usage.
Summary Table
| Strategy | Pros | Cons |
| Global Secondary Index | Flexible queries on attributes outside primary key attributes | Additional cost, maintenance challenge |
| Local Secondary Index | Efficient queries with different sort key without needing a new partition key | Limited by partition key, cost impact |
| BatchGetItem | Retrieve multiple items without query/scan limitations | Requires known keys, cost constraints |
| Filter Expressions | Narrowing down result sets | Does not reduce read operation size |
Approaching queries without the hash key necessitates a strategic formulation during the design phase to maintain efficiency, minimize costs, and deliver required performance. 想象下,使这些操作专业知识和效率都最大化,不会轻易阻碍执行复杂的查询。
Related reading
- Querying a Global Secondary Index in dynamodb Local
- Querying DynamoDB by date
- Querying DynamoDB without Primary Key
- Querying DynamoDB without PrimaryKey with Lambda
- Query just runs, doesn''t execute
- Query on non-key attribute
- Querying for greatest value of Range key on AWS DynamoDb
- Queue data structure supporting fast k-th largest element finding

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.