When creating the Dynamodb table, select Primary Keys and Local Secondary Indexes (LSI) so that the Query operation returns the items you need.
The query operations support only the same evaluation of the primary key operator, but conditional (=, <, <=,>,> =, Between, Begin) for the sort key.
Scanning operations are usually slower and more expensive, since the operation must go through each element in your table in order to get the requested elements.
Example:
Table: CustomerId, AccountType, Country, LastPurchase Primary Key: CustomerId + AccountType
In this example, you can use the Query operation to get:
- CustomerId with conditional filter for AccountType
The scan operation should be used to return:
- All clients with a specific account type
- Elements based on conditional filters by country, i.e. all customers from the USA
- Items based on LastPurchase conditional filters, i.e. all customers who made a purchase last month
To avoid scanning operations for frequently used operations, create a local secondary index (LSI) or global secondary index (GSI).
Example:
Table: CustomerId, AccountType, Country, LastPurchase Primary Key: CustomerId + AccountType GSI: AccountType + CustomerId LSI: CustomerId + LastPurchase
In this example, the Query operation may allow you to get:
- CustomerId with conditional filter for AccountType
- [GSI] Conditional filter by CustomerIds for a specific AccountType
- [LSI] CustomerId with conditional filter on LastPurchase
Kinman
source share