skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    GsiPk GsiSk data
    1 2023-03-15T19:00:59.000Z data
    1 2023-03-13T15:00:59.000Z data
    1 2023-03-11T12:00:59.000Z data
    const lastweek = '2023-03-10T12:00:59.000Z'
    dynamodb.query({
        TableName: 'devices',
        IndexName: 'LastAccess-index',
        KeyConditionExpression: 'GsiPk = :v AND GsiSk > :d',
        ExpressionAttributeValues: {
            ":d": { "S": `"${lastweek}"` }, 
            ":v": { "S": "1" }, 
        },
    })
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search