aws-lambdaamazon-dynamodbserverlessdynamoose

Slow on fetching records from DynamoDB table


I have a DynamoDB table with 151 records, table size is 666 kilobytes and average item size is 4,410.83 bytes.

This is my table schema:

uid - partition key
version - sort key
status - published
archived - boolean
... // other attributes

When I'm doing scan operation from AWS Lambda.

module.exports.searchPois = async (event) => {
  const klass = await getKlass.byEnv(process.env.STAGE, 'custom_pois')

  const status = (event.queryStringParameters && event.queryStringParameters.status) || 'published'

  const customPois = await klass.scan()
    .filter('archived').eq(false)
    .and()
    .filter('status').eq(status)
    .exec()

  return customPois
}

This request takes up 7+ seconds to fetch. I'm thinking to add a GSI so I can perform a query operation. But before I add, is it really like this slow when using scan and If I add GSI, will it fetch faster like 1-3 seconds?


Solution

  • Using Scan should be avoided of at all possible. For your use-case a GSI would be much more efficient and a sparse index would be even better: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-general-sparse-indexes.html

    In saying that, for the small number of items you have it should not take 7seconds. This is likely caused by making infrequent requests to DynamoDB as DynamoDB relies on caching metadata to improve latency for requests, if your requests are infrequent then the metadata will no exist in cache increasing response times.

    I suggest to ensure you re-use your connections, create your client outside of the Lambda event handler and ensure you keep active traffic on the table.