I am building a dashboard for my website where users can see the number of records in a database table by All Time, Last 12 Months, Last 6 Months and Last 3 Months. It occurs to me that this could bog down the server given the fact there could be thousands of records and multiple users requesting these figures at the same time.
Is there an optimal way to do this using Doctrine 2? Right now, the function I have to achieve this looks like this:
/**
* Search group instruction sessions based on the number of months selected from the dashboard gauges.
*
* @param int $number_months (how many months back to search)
* @param User $user_obj (optional- the user being searched)
* @return Response
*/
private function __recordsByNumberOfMonths($number_months, $user_obj = NULL){
$from = date('Y-m-d', strtotime('-'.$number_months.' months'));
//get surveys by a specific user
if($user_obj != NULL){
$query = $this->entityManager->createQuery('SELECT i FROM GroupInstruction i WHERE i.librarian = :librarian AND i.instruction_date > :from ORDER BY i.instruction_date DESC');
$query->setParameter('from', $from);
$query->setParameter('librarian', $user_obj);
}
//get all surveys by all users
else {
$query = $this->entityManager->createQuery('SELECT i FROM GroupInstruction i WHERE i.instruction_date > :from ORDER BY i.instruction_date DESC');
$query->setParameter('from', $from);
}
$results = $query->getResult();
$total_results = count($results);
return $total_results;
}
Any tips on optimization (or if I'm doing this completely backwards) would be appreciated!