PHP Mysql查询疑难解答

时间:2012-10-27 01:35:05

标签: php mysql

新手PHP / Mysql用户在这里...希望有人可以帮助我让这段代码运行得更快。尝试对赛季迄今为止的足球队的进攻和防守平均值进行简单的计算......在ms中,这需要不到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

1 个答案:

答案 0 :(得分: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中执行此操作。我现在太累了,试图写出那个版本的查询。