How can i increase the performance of this function : how can i increase the performance of following function in `phalcon` framework.there are thousands of records in the table. i have trying to make different ways but i am stuck the point. how can i increase the efficiency and reduce the execution time. kindly guide me. thanks following are two methods. kindly review both and give me best tips and techniques.
    
    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 for all cities passenger */
    			$result = array();
    
    			// get all cities permutations
    
    			$tmpcitiesdata = array();
    			$rawresultset = Tickets::find (
    				array(
    					'columns' => 'fromcity,tocity',
    					'conditions' =>	"departure between '{$year}-{$month}-01' and '{$year}-{$month}-$daysInMonth' and tickettype in (1) ". $fromcitycondition,
    					'group' => 'fromcity,tocity'
    				));
    			foreach ($rawresultset as $rawresult) {
    				$tmpcitiesdata[$rawresult->fromcity.'-'.$rawresult->tocity]['fromcity'] = $rawresult->fromcity;
    				$tmpcitiesdata[$rawresult->fromcity.'-'.$rawresult->tocity]['tocity'] = $rawresult->tocity;
    			}
    
    			//var_dump($rawresultset);
    			// get tickets sold based on cities combinations
    
    			$total = 0;
    
    			foreach ($tmpcitiesdata as $tmpcities) {
    				$rawresultset = Tickets::find (
    				array(
    					'columns' => 'customer',
    					'conditions' =>	"departure between '{$year}-{$month}-01' and '{$year}-{$month}-$daysInMonth' and tickettype in (1) and fromcity=". $tmpcities['fromcity']." and tocity=".$tmpcities['tocity'],
    					'group' => 'customer'
    				));
    			
    				$totalsoldRaw = count($rawresultset);
    
    				// get ticket cancellations 
    				$rawresultset = Tickets::find (
    				array(
    					'conditions' =>	"departure between '{$year}-{$month}-01' and '{$year}-{$month}-$daysInMonth' and tickettype in (3) and fromcity=". $tmpcities['fromcity']." and tocity=".$tmpcities['tocity']
    				));
    
    				//make sure cancellations are tickets cancellations not booking cancellations
    
    				foreach($rawresultset as $rawresult)
    				{
    					$resultNumber = Tickets::find("departure='$rawresult->departure' and seatno={$rawresult->seatno} and id < {$rawresult->id} and 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 to 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;
    		}
    
    	}

    
   