Skip to content

Count is wrong if the data set is huge. #60

@jspreddy

Description

@jspreddy

I have around 300,000 records in my table.
I want to get the count of records in my table. Let's stick to no filters for now.
I tried three methods:

Method 1:

Transaction.find().count().exec()
    .then((result) => {
      console.log(result);
    });

// 2683

The result in this query is just a number.
Expectation: I expect to get the correct count which is 300000.
Actual:: I get a partial count as an integer value: 2683

Method 2:

Transaction.find().count().raw().exec()
    .then((result) => {
      console.log(result);
    });

// 2683

Expectation: I would expect this to return the raw object along with the ScannedCount, Count, LastEvaluatedKey, ...
Actual:: I get an integer value: 2683

Method 3:

I see the only way of doing this is by fetching the raw result set as shown in the Pagination example and iterating through and fetching whole of the data and summing up the Count attribute.
like so:

let count = 0;
Transaction.find().raw().exec()
    .then((result) => {
      count += result.Count;
      return Transaction.find().startFrom(result.LastEvaluatedKey).raw().exec();
    }).then( result =>{
       // so on...
    });

Doing repeatedly this untill there is no LastEvaluatedKey and summing up the counts will give me accurate count.

BUUUUUTT This is not optimal, as it is fetching the entirety of the data set when I only need a count.

Maybe fix either Method 1 or Method 2?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions