I am running into a problem where my database calls are slowing up the page load significantly. I am populating multiple charts from elections data, and my table contains around 1 million rows, and I have to query this data multiple times in each methods inside the getCharts()
method.
I'am using this to pass the return data to JavaScript.
These charts gets re-populated when you click on a data point. So if you click on a point i.e ('democrat) it will reload the page and call these methods again.
What I am asking is wether it is possible to do something like this in native PHP. The server is running PHP 5.2 on linode.
foreach(function in getChartsMethod){
Start a child thread to process the function.
}
join the threads.
reload the page.
public function getCharts(){
$this->get_cast_chart();
$this->get_party_chart();
$this->get_gender_chart();
$this->get_age_chart();
$this->get_race_chart();
$this->get_ballot_chart();
$this->get_county_chart();
$this->get_precinct_chart();
$this->get_congressional_district_chart();
$this->get_senate_district_chart();
$this->get_house_district_chart();
return view('elections.index');
}
Sample method
public function get_party_chart(){
$query = DB::table($this->tableName)
->select('party', DB::raw('count(*) as numVotes'))
->groupBy('party')
->orderBy('numVotes', 'ASC');
if ($this->filterParameters != null){
$query = $query->where(key($this->filterParameters), $this->operator, current($this->filterParameters));
}
$chartData = $query->get();
$chartData = $this->prepare_data_for_chart($chartData, 'party', 'numVotes');
JavaScript::put(['partyChart' => $chartData]);
}