Why doesn’t TypeORM skip() and take() function work?

The Delivery table has got more than 4 million rows. I used MySQL database.

async adminDeliveriesViewCount (data) {
 try {
   const batchSize = 10;
   let batchIndex = 0, totalDeliveries = 0;
   while(true) {
     let total = await Delivery.createQueryBuilder().skip(batchIndex * batchSize).take(batchSize)
       .getCount();

     if(total == 0) break;
     totalDeliveries += total;
     ++ batchIndex;
   }

   return totalDeliveries;
 } catch (e) {
   new ErrorHandler(e, 'adminDeliveriesViewCount');
 }
}

This function is not working. The “total” value is always the total number of rows in the table.

I tried changing the “batchSize” and “batchIndex”, but it didn’t help.
Can anyone help me?

  • 1

    Why don’t you just use a SELECT COUNT(*) query instead of looping over batches?

    – 

The code you’ve provided seems to be attempting to count the total number of rows in a Delivery table in batches. you can take below code as the reference

async adminDeliveriesViewCount(data) {
  try {
    const batchSize = 10000; // Adjust the batch size according to your needs
    let totalDeliveries = 0;
    let batchIndex = 0;

    while (true) {
      let result = await Delivery.createQueryBuilder()
        .offset(batchIndex * batchSize)
        .limit(batchSize)
        .getMany();

      if (result.length === 0) {
        break;
      }

      totalDeliveries += result.length;
      batchIndex++;
    }

    return totalDeliveries;
  } catch (e) {
    new ErrorHandler(e, 'adminDeliveriesViewCount');
    // Handle the error here
  }
}

this code fetches rows in batches using offset and limit, incrementing the batchIndex in each iteration until there are no more results to retrieve. It accumulates the count of the returned rows to calculate the total count of deliveries in the table.

adjust the batchSize according to your system’s requirements and resource constraints

Leave a Comment