mardi 25 juillet 2017

Laravel Chart JS Report Analytics - Slow foreach Query Loop

I am currently building an analytics system for our job site, we have a database of applications which are time-stamped.

I am attempting to get the amount of applications per day with a specific month, I am doing this using ajax and chart js, the code works however, the php is extremely slow and I just wondered if there was anyway to improve this. Below is the code I am using:

$days = cal_days_in_month(CAL_GREGORIAN,$qmonth,$qyear);
  foreach (range(1, $days) as $key => $number) {
      $count = App\report::whereYear('date_created', $qyear)->whereMonth('date_created', $qmonth)->whereDay('date_created', $number)->count();
      $cart[] = array('day' => $number, 'count' => $count);
  }
  $result = json_encode($cart);
  print_r($count);

I pass two url parameters to the php code $qmonth and $qyear, I am running a query between 28 and 31 times depending on how many days are in the month, since I need to count the amount of applications on a daily basis. Is there any that I can do this without querying 30 times in a loop, since I assume this is why the query is so slow?

Best regards, Gareth



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire