Home > Mobile >  DynamoDB client doesn't fulfil the limit
DynamoDB client doesn't fulfil the limit

Time:12-09

Client lib : "@aws-sdk/client-dynamodb": "3.188.0"

I've a DynamoDB pagination implementation.

My user count is 98 & page size is 20. Therefore I'm expecting 5 pages & each having 20,20,20,20 & 18 users in the result.

But actually I'm getting more than 5 pages and each page having variable number of users like 10, 12, 11 ..etc.

How can I get users with proper page limit like 20, 20, 20, 20 & 18?

public async pagedList(usersPerPage: number, lastEvaluatedKey?: string): Promise<PagedUser> {

      const params = {
         TableName: tableName,
         Limit: usersPerPage,
         FilterExpression: '#type = :type',
         ExpressionAttributeValues: {
            ':type': { S: type },
         },
         ExpressionAttributeNames: {
            '#type': 'type',
         },
      } as ScanCommandInput;

      if (lastEvaluatedKey) {
         params.ExclusiveStartKey = { 'oid': { S: lastEvaluatedKey } };
      }

      const command = new ScanCommand(params);
      const data = await client.send(command);

      const users: User[] = [];
      if (data.Items !== undefined) {
         data.Items.forEach((item) => {
            if (item !== undefined) {
               users.push(this.makeUser(item));
            }
         });
      }

      let lastKey;
      if (data.LastEvaluatedKey !== undefined) {
         lastKey = data.LastEvaluatedKey.oid.S?.valueOf();
      }
      return {
         users: users,
         lastEvaluatedKey: lastKey
      };
   }

CodePudding user response:

The scan command documentation https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Scan.html#Scan.Pagination

Provides few reasons why your result may contain less results:

  • The result must fit in 1 MB
  • If a filter is applied, the data is filtered "after scan". You have a filter in your query.

From the docs

A filter expression is applied after a Scan finishes but before the results are returned. Therefore, a Scan consumes the same amount of read capacity, regardless of whether a filter expression is present.

...

Now suppose that you add a filter expression to the Scan. In this case, DynamoDB applies the filter expression to the six items that were returned, discarding those that do not match. The final Scan result contains six items or fewer, depending on the number of items that were filtered.

In the next section it is explained how you can verify that it is possibly your case:

Counting the items in the results

In addition to the items that match your criteria, the Scan response contains the following elements:

ScannedCount — The number of items evaluated, before any ScanFilter is applied. A high ScannedCount value with few, or no, Count results indicates an inefficient Scan operation. If you did not use a filter in the request, ScannedCount is the same as Count.

  • Related