문제

I am working with an app which uses phpActiveRecord and mySQL to pull in data from a sensor network and plot it onto a number of flot.js graphs on the client.

There are several timeframes the user can chose between to affect the range of data the graphs display. 2hrs, 24hrs, 3 days and 1 week.

The sensors post to the database every 60 seconds, so when plotting the graphs, the query pulls in all rows between now and DATE_SUB(CUR_DATE - INTERVAL ? DAY) where ? is either 1, 3 or 7 etc.

However this results in a massive number of rows being returned (60,000 + for the full week!) and is causing huge delays and server errors.

I know I can just massively increase the max memory available for queries in the php.ini file, but this is hardly a good solution, and doesn't solve the issue of speed.

My question is, is there a way I can easily select only every second or third row from the required date range depending on the length of the interval the user wishes to view?

In C or Java I would do something like a modulo select to return alternate rows but I cannot think of a way to do this in the current framework.

Any ideas would be appreciated. Thanks.

도움이 되었습니까?

해결책

<?
$row = 1;
WHILE QUERY {
    if ($row % 2 == 0) {
        echo "Yourstuff";
    } else {
        //Nothing
    }
    $row++;
}
?>

This should help you to think about a solution..maybe not the perfect one for you, but i hope it helps... For every third use $row%3 and so on...

다른 팁

Try:

$i = 0;
foreach($row as $data){
  if($i == 1){
    //Do whatever with your 2n record
    $i++;
  }
  if($i > 1){$i = 0;} //Reset the counter
}

Returning every second row also not ideal solution. You may use cache.

Easiest way is caching your table to another table. Configure automatic cron job periodically.

datas
datas_cache ( Only Results ) 

If you want more professional solution you need to cache your result into files can use JSON

In the end I did end up managing to select the rows as specified above.

However this was not an acceptable solution to the problem I was having so I have decided to try and generate my graphs server side in order to try and avoid the issue altogether. It seems logical that generating charts with so many data points be done on the server and just push an image down the wire to an html5 canvas or some such. Going to try pChart for this. Thanks to all responders in any case.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top