此脚本使用PHP和MySQL来计算在一分钟移动平均,以减少异常值对我的数据的影响(1分= 6 10第二行)。它可以正确计算所有内容,但一次不足150行。我希望一次能做到尽可能多的行数,可能在5-10,000之间,因为我的表格超过150,000,并且每天输入大约8000行。滚动平均效率,PHP的MySQL
有没有人有任何建议,我怎样才能使这个脚本更有效地运行?
谢谢!
<?php
//connect to database
mysql_connect("localhost","user","password");//database connection
mysql_select_db("database");
$result = mysql_query("SELECT Timestamp FROM table");
if (!$result) {
die('Could not query:' . mysql_error());
}
//get number of rows in table
$resultA = mysql_query("SELECT * FROM table");
$num_rows = mysql_num_rows($result);
echo "There are $num_rows rows.</br>";
//select column to be averaged
$resultB = mysql_query("SELECT PortRPMSignal FROM table");
if (!$resultB) {
die('Could not query:' . mysql_error());
}
//set start equal to the first row you want to calculate the averages from, likely the first null row
$start = 5;
//calculate 1 minute average, the average is correct
for($i = $start; $i<$num_rows; $i++){
$output = mysql_result($result,$i);
$test = mysql_result($resultB,$i)+mysql_result($resultB,$i-1)+mysql_result($resultB,$i-2)+mysql_result($resultB,$i-3)+mysql_result($resultB,$i-4)+mysql_result($resultB,$i-5);
$test2 = $test/6;
$round = round($test2,4);
$temp = mysql_query("SELECT Timestamp FROM table");
if(!$temp){
die('Could not query:' . mysql_error());
}
//gets timestamp at row $i, and inserts new average value into that row in RPMAve column
$time = mysql_result($result,$i);
mysql_query("UPDATE table SET PortMinuteAveRPM = $round WHERE Timestamp = '$time'");
}
你需要使用MySQL吗?它看起来不像你需要它。 – Esailija 2012-07-23 13:55:47
如[mysql_query()'](http://php.net/manual/en/function.mysql-query.php)函数的PHP手册所述:*不推荐使用此扩展名。相反,[MySQLi](http://www.php.net/manual/en/book.mysqli.php)或[PDO_MySQL](http://www.php.net/manual/en/ref.pdo-应该使用mysql.php)扩展名。另请参见[MySQL:选择API](http://www.php.net/manual/en/mysqlinfo.api.choosing.php)指南和[相关FAQ](http://www.php.net/manual /en/faq.databases.php#faq.databases.mysql.depcated)以获取更多信息。* – eggyal 2012-07-23 14:02:13
我开始使用MySQL自动执行分析,而不是使用Excel,有没有其他方法可以提示? – user1537931 2012-07-23 14:09:35