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]);
}
Multithreading is possible in PHP which has been discussed on Stackoverflow before: How can one use multi threading in PHP applications
However I don't know how much multithreading will help you here. How much data are we talking, what kind of accuracy are you looking for in the charts? How many charts are being shown? A lot of the solution will come from context.
If it were me, I would have a background schedule pre-processing the "huge" amounts of data into something more useful/usable on the frontend. Again useful/usable entirely depend on the context. When there is an actual page request, you just need to pass down the processed data, versus actual "live" data.
Again, again, this will entirely depend on the context of your application. Perhaps you need by-the-minute data, maybe you don't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With