Search code examples
amazon-dynamodb

Querying the number of rows using index - Dynamo DB


We would like to retrieve count every now and then but using index instead of primary key. The below query works but because it says ScannedCount - Does it mean it is scanning the entire table ? Since we are having 300 million+ rows , it would be a very costly operation incase if it scans the entire table

import {DynamoDBClient} from "@aws-sdk/client-dynamodb";
import {DynamoDBDocument} from "@aws-sdk/lib-dynamodb";

async function query() {
  const dynamodDBConnection = new DynamoDBClient({region: "us-east-1"});
  const documentClient = DynamoDBDocument.from(dynamodDBConnection);
  const output = await documentClient.query(
        {
            TableName: "table7",
            IndexName: "parentid-index",
            KeyConditionExpression: 'parentid = :parentid',
            ExpressionAttributeValues: {
                ':parentid': "5ceb26579086c94159bf145d8933acb2"},
        }
    );

    console.log(output.ScannedCount)
}

(async function() {
  await query();
})();

Solution

  • No, ScannedCount is the number of item your request evaluated. As you're using a Query you only evaluate the items which have the parent-id that you pass in. As you don't use a Filter expression, ScannedCount and Count will be the same.

    • ScannedCount — The number of items that matched the key condition expression before a filter expression (if present) was applied.

    • Count — The number of items that remain after a filter expression (if present) was applied.

    If you just wish to obtain the count, you can also pass the Select parameter with a value of COUNT which means it only returns the count, not the data. The cost is the same but you may save time on the wire and deserializing.