php - How to increase the performance of this function -


how increase performance of following function in phalcon framework. there thousands of records in table. tried different ways, stuck point. how can increase efficiency , reduce execution time. following 2 methods:

public function currentmonthaction()     {         $payload = $this->request->getjsonrawbody();          $this->setdb();         $ticketsmodel = new tickets();         $fromcitycondition = "";         if( isset($payload->city->id) )         {                $fromcitycondition = "and fromcity='{$payload->city->id}'";         }          try{             $date = new \datetime($payload->date);             $year = $date->format('y');             $month = $date->format('m');             $month = '08';             $daysinmonth = cal_days_in_month(cal_gregorian, $month, $year);             /* result cities passenger */             $result = array();              // cities permutations              $tmpcitiesdata = array();             $rawresultset = tickets::find (                 array(                     'columns' => 'fromcity,tocity',                     'conditions' => "departure between '{$year}-{$month}-01' , '{$year}-{$month}-$daysinmonth' , tickettype in (1) ". $fromcitycondition,                     'group' => 'fromcity,tocity'                 ));             foreach ($rawresultset $rawresult) {                 $tmpcitiesdata[$rawresult->fromcity.'-'.$rawresult->tocity]['fromcity'] = $rawresult->fromcity;                 $tmpcitiesdata[$rawresult->fromcity.'-'.$rawresult->tocity]['tocity'] = $rawresult->tocity;             }              //var_dump($rawresultset);             // tickets sold based on cities combinations              $total = 0;              foreach ($tmpcitiesdata $tmpcities) {                 $rawresultset = tickets::find (                 array(                     'columns' => 'customer',                     'conditions' => "departure between '{$year}-{$month}-01' , '{$year}-{$month}-$daysinmonth' , tickettype in (1) , fromcity=". $tmpcities['fromcity']." , tocity=".$tmpcities['tocity'],                     'group' => 'customer'                 ));                  $totalsoldraw = count($rawresultset);                  // ticket cancellations                  $rawresultset = tickets::find (                 array(                     'conditions' => "departure between '{$year}-{$month}-01' , '{$year}-{$month}-$daysinmonth' , tickettype in (3) , fromcity=". $tmpcities['fromcity']." , tocity=".$tmpcities['tocity']                 ));                  //make sure cancellations tickets cancellations not booking cancellations                  foreach($rawresultset $rawresult)                 {                     $resultnumber = tickets::find("departure='$rawresult->departure' , seatno={$rawresult->seatno} , id < {$rawresult->id} , tickettype = 1" );                     if( count($resultnumber) > 0 ){                         $totalsoldraw = $totalsoldraw-1;                     }                 }                  $total += $totalsoldraw;                  array_push($result, array('total' => $totalsoldraw, 'fromcity' => cities::findfirstbyid($tmpcities['fromcity'])->name, 'tocity' => cities::findfirstbyid($tmpcities['tocity'])->name));             }              //sort result based on counts             arsort($result);              //cut result max 6 cities             $result = array_slice($result, 0, 6);              $this->response->setcontenttype('application/json')                 ->setjsoncontent(                     array( 'totaltickets' => $total, "allcities" => $result )                 );             $this->response->send();             return;         }         catch(\pdoexception $e)         {             $this->response->setstatuscode('422','invalid payload');             $this->response->setcontenttype('application/json')                 ->setjsoncontent(array(                     'flash' => array(                         'class' => 'danger',                         'message' => $e->getmessage()                     )                 ));             $this->response->send();             return;         }      } 

use count instead of count(result of find). sure in // ticket cancellations don't need group ? can select tickets customers in 1,3 tickettype , filter resultset.

also can't first $rawresulset can't be:

$rawresultset = tickets::find (                 array(                     'columns' => 'customer,fromcity,tocity,tickettype ',                     'conditions' => "departure between '{$year}-{$month}-01' , '{$year}-{$month}-$daysinmonth' , tickettype in (1,3)".$fromcitycondition                     'group' => 'customer,fromcity,tocity'                 )); 

?

$ticketcanccelations = $rawresultset->filter(function($row){     if($row->tickettype == 3) {         return $row;     }  });  $resultnumber = tickets::count("departure='$rawresult->departure' , seatno={$rawresult->seatno} , id < {$rawresult->id} , tickettype = 1" ); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -