-1

新手 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
4

1 回答 1

0

我不完全确定您在做什么,但我认为这将所有内容组合到一个查询中:

SELECT n2.date, n2.season, n2.away,
       SUM(n2.away IN (n1.away, n1.home)) AS awgames,
       SUM(n2.home IN (n1.away, n1.home)) AS hmgames,
       SUM(n2.away = n1.away) AS awgamesaw,
       SUM(n2.away = n1.home) AS awgameshm,
       SUM(n2.home = n1.away) AS hmgamesaw,
       SUM(n2.home = n1.home) AS hmgameshm,
       SUM(IF(n2.away = n1.away, aw_score)) AS awgamesaw_awscore,
       SUM(IF(n2.away = n1.away, hm_score)) AS awgamesaw_hmscore,
       SUM(IF(n2.away = n1.home, aw_score)) AS awgameshm_awscore,
       SUM(IF(n2.away = n1.home, hm_score)) AS awgameshm_hmscore,
       SUM(IF(n2.home = n1.away, aw_score)) AS hmgamesaw_awscore,
       SUM(IF(n2.home = n1.away, hm_score)) AS hmgamesaw_hmscore,
       SUM(IF(n2.home = n1.home, aw_score)) AS hmgameshm_awscore,
       SUM(IF(n2.home = n1.home, hm_score)) AS hmgameshm_hmscore,
FROM nfl_new n1 JOIN nfl_new n2
ON n1.date < n2.date AND n1.season = n2.season AND (n2.home IN (n1.home, n1.away) OR n2.away IN (n1.home, n1.away))
GROUP BY n2.date, n2.season, n2.away

不过,我不确定分组情况。您的原始查询没有分组,但是当它打印结果时,它只打印日期、季节和远离第一个查询的时间,所以这似乎是您的想法。

它花费这么长时间(除了重复的查询)的原因是它似乎是一个二次计算。您正在遍历第一个查询返回的所有行,并且对于这些行中的每一行,您都在对本季中它之前的所有行求和。这是很多重复。

最好执行一个查询,返回按季节和日期排序的所有行,并在 PHP 代码中计算运行总计。也可以使用用户定义的变量直接在 SQL 中执行此操作。我现在太累了,无法尝试编写该版本的查询。

于 2012-10-27T03:57:16.763 回答