I have a dynamodb table, that is formatted as such:
UDID | Key | LastAccess |
---|---|---|
primary | data | secondaryIndex |
I know that I can write a query such as:
dynamodb.query({TableName: 'devices', IndexName: 'LastAccess-index',
KeyConditionExpression: 'lastAccess=:d',
ExpressionAttributeValues:{":d":{"S":`"${lastweek}"`}},
"ScanForwardIndex:false,
"Limit":1})
This does work, however, what I need is for my KeyConditionExpression
to be lastAccess >= :d
. When I try it I get a "Query key condition not supported" error. What are my options without using a scan that would be highly inefficient.
2
Answers
Such a query would work so much more naturally in an SQL database!
If you have a need to make very frequent queries in a NoSQL DB for "Accesses Last Week" and "Accesses Last Month", it would make sense to run an overnight reindex lambda to update one or more separate attributes to ‘this_week’, ‘last_week’, etc. Otherwise, it looks like you are stuck with a scan.
A simple solution to your issue, assuming you do not write more that 1000WCU per second, is to have a static value as your GSI PK. Let’s use the value
1
as an example.As mentioned, be aware of the scalability limitations using this strategy, if you need to, you can shard the GSI PK to handle as much throughput as you need, but you will also have to read for every shard. Have a look at this if you need to.