限制数据库查询量

时间:2015-06-21 11:24:50

标签: php mysql mysqli

在阅读我的问题之前,请记住以下是我正在做个人项目的工作,以学习/拓宽我的php / mysql技能,因此我仍然是一个新手,因为我确信在我的编码后面是明显的。然而,任何提示/帮助赞赏

我的应用

我的网站允许用户猜测运动比赛的结果是什么,比赛结束后上传结果并且获胜会员获得奖励“积分”

用于验证获奖者的Psuedo代码

  1. 上传活动/体育比赛的结果
  2. 将旧事件从activeEvents表移至expiredEvents
  3. 现在从activeEvents
  4. 中删除事件数据
  5. 检查谁在picks table中猜到了正确的匹配结果 在步骤1中上传数据
  6. 将猜对正确的成员插入winners
  7. 将旧的选择数据移至expiredPicks表格
  8. 删除从expiredPicks
  9. 移至activePicks表的数据

    我的问题

    对于上面描述的每个步骤,我对数据库进行了新的查询,这不仅导致响应时间慢,而且似乎效率低下,我正在寻找一些关于如何保持相同功能但改进我的代码的建议

    if(isset($_POST['resultBtn'])){
        //STEP 1:----GET GAME INFO
        foreach($_POST['winner'] as $id =>$winner){
            $winScore = $_POST['score'][$id];
            $teamsel[] = $winner ;
            $team1 = $_POST['team1'][$id];
            $team2 = $_POST['team2'][$id];
            $venue = $_POST['ven'][$id];
            $matchId = $_POST['gameId'][$id]; //match Id
            $score = $_POST['score'][$id]; //score
            $tour = $_POST['tournament'][$id];
            $round = $_POST['round'][$id];
            $event_date = $_POST['event_date'][$id];
            $gameNR = $_POST['gameNr'][$id];
            $sport=$_POST['sport'][$id];
    
            //STEP 2:----INSERT event info into expiredEvents
            $sql="INSERT INTO expiredEvents (event_id, sport_type, tournament, 
                            round, team1, team2, venue, event_date)
                            VALUES
                            ('$matchId', '$sport', '$tour', '$round', 
                            '$team1', '$team2','$venue','$event_date')";                                        
            mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);      
    
            //STEP 3:----DELETE event/match from events table
            $sql ="DELETE FROM events WHERE event_id ='$matchId'";
            $result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
    
            //Step4: ----check for winner(s) in picks table with given data
            $sql="select * from picks where event_id = '$matchId' and
                     abs(score-$winScore) = (select min(abs(score-$winScore)) from picks 
                     where pick = '$winner');";      
        $result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
    
        while($row = mysql_fetch_array($result)){
            $winingMember = $row['member_nr'];
            $event = $row['event_id'];
            $pick = $row['pick'];
            $score = $row['score'];
    
            echo'<br />';
            echo $winingMember;
            echo'<br />';
            echo $event;
            echo'<br />';
            echo $pick;
            echo'<br />';
            echo $score;
            echo'<br />';
            echo $event_date;
            echo'<br />';
    
            //step5 insert winners into winners table
            $sql="INSERT INTO winners
            (member_nr,event_id, pickedTeam,pickedScore,event_date)
            VALUES
            ('$winingMember','$event','$pick','$score','$event_date')";
            mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
    
            }//while
    
            //Step 6 move old pick data to expired picks table  
            $sql="INSERT INTO expiredPicks (select * from picks WHERE event_id = '$matchId'";
            $result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
    
            //step7: --Delete expired picks
            $sql="DELETE FROM picks where event_id='$matchId'";
            $result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
    
        }//for each
    }//isset
    

1 个答案:

答案 0 :(得分:2)

只是想一想,重新设计你的表并使用join来提高查询效率。

Table1 Sports Id - Event - Result - Date From - Date To
Table2 Users Id - Name
Table3 Picks Id - Pick - Event (FK to Sports.Id) - Name (FK to Users.Id)
Table4 Winners Id - Winner (FK to Users.ID) - Credit(?)

现在,对于表1,如果要检查哪个事件已完成,只需检查Result不为空的位置,当事件完成时,您可以使用结果更新表。

这只是初步想法,即使从现在开始我觉得它需要更多的工作,但这应该足以让你开始。