-1

初心者の PHP/Mysql ユーザーがここにいます...誰かがこのビットのコードをより高速に実行するのを手伝ってくれることを願っています。サッカー チームの今シーズンのオフェンスとディフェンスの平均値を簡単に計算しようとしています... ms Excel では、SUMIF を使用すると 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

あなたが何をしているのか完全にはわかりませんが、これはすべてを1つのクエリにまとめていると思います:

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

ただ、グループ化についてはよくわかりません。元のクエリはグループ化されていませんが、結果を出力すると、日付、季節、および最初のクエリから離れた場所のみが出力されるため、それはあなたが考えている方法のようです.

(クエリの重複に加えて) 非常に時間がかかる理由は、2 次計算のように見えるためです。最初のクエリによって返されたすべての行をループしており、それらの行のそれぞれについて、季節に先行するすべての行を合計しています。それはたくさんの繰り返しです。

季節と日付で並べ替えられたすべての行を返すクエリを実行し、PHP コードで累計を計算する方がよい場合があります。ユーザー定義変数を使用して、SQL で直接実行することもできます。私は疲れすぎて、そのバージョンのクエリを書こうとはしません。

于 2012-10-27T03:57:16.763 に答える