Reputation: 403
I have a table with 3 attributes
ARTICLE | CLAPS | STAMP
----- | ----- | ----
RUST | 1 | 2 OCT 2020 19:20
C++ | 3 | 10 OCT 2020 12:30
I want to do these 3 queries on the table :
What is the most efficient way to query 2 and 3 without using a scan operation?
Upvotes: 0
Views: 407
Reputation: 403
After going through @Charles answer. I am taking the following approach.
TABLE
POST | TYPE | CLAPS | STAMP
----- | ---- | ----- | ----
RUST | A | 1 | 1594185483964
C++ | A | 3 | 1594185984082
Here TYPE A
denotes an article.
I have made 2 GSIs.
I think this will increase flexibility while allowing me to do valid queries.
{
TableName: 'WB360-POSTS',
IndexName: 'TYPE-CLAPS-POSTS',
KeyConditionExpression: '#T = :T',
ScanIndexForward: false,
Limit: 10,
ExpressionAttributeNames: {
'#T': 'TYPE'
},
ExpressionAttributeValues: {
':T': 'A'
},
ProjectionExpression: 'POST'
}
{
TableName: 'WB360-POSTS',
IndexName: 'TYPE-STAMP-POSTS',
KeyConditionExpression: '#T = :T',
ScanIndexForward: false,
Limit: 10,
ExpressionAttributeNames: {
'#T': 'TYPE'
},
ExpressionAttributeValues: {
':T': 'A'
},
ProjectionExpression: 'POST'
}
Upvotes: 0
Reputation: 23793
What is the most efficient way to query 2 and 3 without using a scan operation?
Create Global Secondary Indexes (GSI)
Hash key would be either the string "CLAPS" or the string "STAMP", sort key would be the corresponding CLAPS value or the STAMP value.
When you Query(), specify '"ScanIndexForward=false"and
"Limit=1"` to get the most highest (most recent) value.
Note you need to store the time stamp in a sortable format, "2020-10-10-12:30" not "10 OCT 2020 12:30"
Upvotes: 2