Reputation: 51
I have referred multiple posts on Query Vs Scan in DynamoDB. Almost all the posts mentioned Scan is slower since it reads entire table or index. I wanted to know use cases where Scan is better than Query. Can you share if there are any?
Upvotes: 1
Views: 427
Reputation: 14819
Ashan's answer is correct. To add some extra description and a couple of examples:
Find an item based on non-index attributes (Should be infrequent since the scan is expensive).
There are a few reasons you might not index an attribute.
Low Cardinality
Imagine a table where each item represents a person. You might want an operation that returns all female people. A boolean gender attribute would make a poor partition key due to low cardinality, so you would almost certainly never partition this attribute. In this case a scan is appropriate. A good rule of thumb here is that if an operation returns a significant proportion of items in a table, a scan is probably fine.
Rarely Used
Let's say the people in the table are employees and once a month you order pizza for everyone. Your table contains everyone's favourite pizza so before you order, you want to know which employees will have the margherita. You only do this once a month. Creating an index just for this purpose would be expensive as you have to provision the index. It's probably better in this case just to do a scan.
Upvotes: 2
Reputation: 19728
There are several use-cases where Scan is preferred over Query.
Upvotes: 2