新手 PHP/Mysql 用户在这里...希望有人可以帮助我使这段代码运行得更快。尝试对足球队本赛季至今的进攻和防守平均值进行简单计算......在 ms excel 中,使用 sumifs 只需不到 10 秒。使用下面使用 php/mysql 发布的代码位需要 3 个小时!我知道我可以做一些更好的事情......只是不知道是什么!谢谢。
mysql_query("UPDATE nfl_new SET Season = if(MONTH(date)<5, YEAR(date)-1,YEAR(date))");
$sql= "SELECT * FROM nfl_new";
if (!$results = mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$num = mysql_numrows($results);
$i = 0;
while ($i < $num) {
//get a row from mysql one at a time
$row = mysql_fetch_row($results, $i);
//calculate aw teams's offense
$awscoreaw = mysql_query("SELECT SUM(aw_score) AS awscoreaw FROM nfl_new WHERE away='$row[away]' AND date<'$row[date]' AND Season='$row[Season]'");
$awgames = mysql_query("SELECT SUM(if((away = '$row[away]' OR home = '$row[away]') AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgames FROM nfl_new");
$awgamesaw = mysql_query("SELECT SUM(if(away = '$row[away]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgamesaw FROM nfl_new");
$awgameshm = mysql_query("SELECT SUM(if(home = '$row[away]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgameshm FROM nfl_new");
$awscorehm = mysql_query("SELECT SUM(hm_score) AS awscorehm FROM nfl_new WHERE home='$row[away]' AND date<'$row[date]' AND Season='$row[Season]'");
$awscoreaw = mysql_fetch_assoc($awscoreaw);
$awscorehm = mysql_fetch_assoc($awscorehm);
$awgames = mysql_fetch_assoc($awgames);
$awgamesaw = mysql_fetch_assoc($awgamesaw);
$awgameshm = mysql_fetch_assoc($awgameshm);
$aw_off = ($awscoreaw['awscoreaw'] + $awscorehm['awscorehm']+$awgamesaw['awgamesaw']*1.5-$awgameshm['awgameshm']*1.5)/$awgames['awgames'];
//calculate aw teams's defense
$awscoreaw = mysql_query("SELECT SUM(hm_score) AS awscoreaw FROM nfl_new WHERE away='$row[away]' AND date<'$row[date]' AND Season='$row[Season]'");
$awgames = mysql_query("SELECT SUM(if((away = '$row[away]' OR home = '$row[away]') AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgames FROM nfl_new");
$awgamesaw = mysql_query("SELECT SUM(if(away = '$row[away]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgamesaw FROM nfl_new");
$awgameshm = mysql_query("SELECT SUM(if(home = '$row[away]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgameshm FROM nfl_new");
$awscorehm = mysql_query("SELECT SUM(aw_score) AS awscorehm FROM nfl_new WHERE home='$row[away]' AND date<'$row[date]' AND Season='$row[Season]'");
$awscoreaw = mysql_fetch_assoc($awscoreaw);
$awscorehm = mysql_fetch_assoc($awscorehm);
$awgames = mysql_fetch_assoc($awgames);
$awgamesaw = mysql_fetch_assoc($awgamesaw);
$awgameshm = mysql_fetch_assoc($awgameshm);
$aw_def = ($awscoreaw['awscoreaw'] + $awscorehm['awscorehm']-$awgamesaw['awgamesaw']*1.5+$awgameshm['awgameshm']*1.5)/$awgames['awgames'];
//calculate hm teams's offense
$hmscoreaw = mysql_query("SELECT SUM(aw_score) AS awscoreaw FROM nfl_new WHERE away='$row[home]' AND date<'$row[date]' AND Season='$row[Season]'");
$hmgames = mysql_query("SELECT SUM(if((away = '$row[home]' OR home = '$row[home]') AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgames FROM nfl_new");
$hmgamesaw = mysql_query("SELECT SUM(if(away = '$row[home]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgamesaw FROM nfl_new");
$hmgameshm = mysql_query("SELECT SUM(if(home = '$row[home]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgameshm FROM nfl_new");
$hmscorehm = mysql_query("SELECT SUM(hm_score) AS awscorehm FROM nfl_new WHERE home='$row[home]' AND date<'$row[date]' AND Season='$row[Season]'");
$hmscoreaw = mysql_fetch_assoc($hmscoreaw);
$hmscorehm = mysql_fetch_assoc($hmscorehm);
$hmgames = mysql_fetch_assoc($hmgames);
$hmgamesaw = mysql_fetch_assoc($hmgamesaw);
$hmgameshm = mysql_fetch_assoc($hmgameshm);
$hm_off = ($hmscoreaw['awscoreaw'] + $hmscorehm['awscorehm']+$hmgamesaw['awgamesaw']*1.5-$hmgameshm['awgameshm']*1.5)/$hmgames['awgames'];
//calculate hm teams's defense
$hmscoreaw = mysql_query("SELECT SUM(hm_score) AS awscoreaw FROM nfl_new WHERE away='$row[home]' AND date<'$row[date]' AND Season='$row[Season]'");
$hmgames = mysql_query("SELECT SUM(if((away = '$row[home]' OR home = '$row[home]') AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgames FROM nfl_new");
$hmgamesaw = mysql_query("SELECT SUM(if(away = '$row[home]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgamesaw FROM nfl_new");
$hmgameshm = mysql_query("SELECT SUM(if(home = '$row[home]' AND date<'$row[date]' AND Season='$row[Season]', 1, 0)) AS awgameshm FROM nfl_new");
$hmscorehm = mysql_query("SELECT SUM(aw_score) AS awscorehm FROM nfl_new WHERE home='$row[home]' AND date<'$row[date]' AND Season='$row[Season]'");
$hmscoreaw = mysql_fetch_assoc($hmscoreaw);
$hmscorehm = mysql_fetch_assoc($hmscorehm);
$hmgames = mysql_fetch_assoc($hmgames);
$hmgamesaw = mysql_fetch_assoc($hmgamesaw);
$hmgameshm = mysql_fetch_assoc($hmgameshm);
$hm_def = ($hmscoreaw['awscoreaw'] + $hmscorehm['awscorehm']-$hmgamesaw['awgamesaw']*1.5+$hmgameshm['awgameshm']*1.5)/$hmgames['awgames'];
mysql_query("UPDATE nfl_new SET aw_off='$aw_off', aw_def='$aw_def', hm_off='$hm_off', hm_def='$hm_def' WHERE away='$row[away]' AND date='$row[date]' AND Season='$row[Season]'");
echo $row['date'].' '.$row['away'].' '.$aw_def.'<br />';
ob_flush();
flush();
$i++;
}
这是 SHOW CREATE TABLE 的输出
nfl_new CREATE TABLE `nfl_new` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`away` varchar(40) NOT NULL,
`aw_line` varchar(6) NOT NULL,
`home` varchar(40) NOT NULL,
`hm_line` varchar(6) NOT NULL,
`total` varchar(6) NOT NULL,
`aw_score` int(3) NOT NULL,
`hm_score` int(3) NOT NULL,
`aw_firstdown` int(3) NOT NULL,
`hm_firstdown` int(3) NOT NULL,
`aw_rush_att` int(3) NOT NULL,
`hm_rush_att` int(3) NOT NULL,
`aw_rush_yds` varchar(3) NOT NULL,
`hm_rush_yds` varchar(3) NOT NULL,
`aw_pass_yds` varchar(3) NOT NULL,
`hm_pass_yds` varchar(3) NOT NULL,
`aw_ret_yds` varchar(3) NOT NULL,
`hm_ret_yds` varchar(3) NOT NULL,
`aw_pass_comp` int(3) NOT NULL,
`hm_pass_comp` int(3) NOT NULL,
`aw_pass_att` int(3) NOT NULL,
`hm_pass_att` int(3) NOT NULL,
`aw_int` int(2) NOT NULL,
`hm_int` int(2) NOT NULL,
`aw_sacks` int(2) NOT NULL,
`hm_sacks` int(2) NOT NULL,
`aw_sack_yds` int(3) NOT NULL,
`hm_sack_yds` int(3) NOT NULL,
`aw_fum` int(2) NOT NULL,
`hm_fum` int(2) NOT NULL,
`aw_fum_lost` int(2) NOT NULL,
`hm_fum_lost` int(2) NOT NULL,
`aw_pen` int(3) NOT NULL,
`hm_pen` int(3) NOT NULL,
`aw_pen_yds` int(4) NOT NULL,
`hm_pen_yds` int(4) NOT NULL,
`aw_fg_att` int(2) NOT NULL,
`hm_fg_att` int(2) NOT NULL,
`aw_fg` int(2) NOT NULL,
`hm_fg` int(2) NOT NULL,
`aw_top` varchar(8) NOT NULL,
`hm_top` varchar(8) NOT NULL,
`Season` year(4) NOT NULL,
`aw_off` double NOT NULL,
`aw_def` double NOT NULL,
`hm_off` double NOT NULL,
`hm_def` double NOT NULL,
`aw_mov` double NOT NULL,
`hm_mov` double NOT NULL,
`aw_sos` double NOT NULL,
`hm_sos` double NOT NULL,
`aw_sim` double NOT NULL,
`hm_sim` double NOT NULL,
`aw_ypp_off` double NOT NULL,
`aw_ypp_def` double NOT NULL,
`hm_ypp_off` double NOT NULL,
`hm_ypp_def` double NOT NULL,
`aw_ypp_mov` double NOT NULL,
`hm_ypp_mov` double NOT NULL,
`aw_ypp_sos` double NOT NULL,
`hm_ypp_sos` double NOT NULL,
`aw_ypp_sim` double NOT NULL,
`hm_ypp_sim` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6733 DEFAULT CHARSET=latin1