如何处理像mysql中计算平均分数这样的困难过程?

时间:2013-08-06 20:14:05

标签: php mysql optimization

使用PHP和MySQL设计应用程序,帮助教师进行在线多项选择测试。每个考试有100个问题,每个问题有4个以上的选择。

当超过1000名用户参加考试时,当系统管理员进入“统计”以查看平均分数或其他统计数据时,经过大量思考后,系统不会显示任何内容:没有结果,没有统计数据

计算:100个问题* 4个选择* 1000个用户获得平均分数。

当我们在更强大的PC上运行Web服务器时,它会显示统计信息,但在共享服务器中它不显示任何内容。

表架构是InnoDB。

如何计算平均分数和/或将结果导出到CSV文件,或将它们添加到数组并对其进行排序?

这是在导出到csv文件之前计算最终得分的代码:

$result_list = array();
foreach($UserTestList as $userTestProp)
{
    $result_list[$userTestProp['utid']]=array();
    ....
    $result_list[$userTestProp['utid']]['utid'] = $userTestProp['utid'];
    $result_list[$userTestProp['utid']]['utlength'] = gmdate("H:i:s", strtotime($userTestProp['utlast_activity'])-strtotime($userTestProp['utstart_datetime']));
    $result_list[$userTestProp['utid']]['utuser_ip'] = $userTestProp['utuser_ip'];
    $result_list[$userTestProp['utid']]['utbrowser'] = $userTestProp['utbrowser'];
    ...
    $result_list[$userTestProp['utid']]['correct'] = $test->GetNumOfAnswers('correct',$userTestProp['utid']);
    $result_list[$userTestProp['utid']]['incorrect'] = $test->GetNumOfAnswers('incorrect',$userTestProp['utid']);
    $result_list[$userTestProp['utid']]['none'] = $test->GetNumOfAnswers('none',$userTestProp['utid']);
    $result_list[$userTestProp['utid']]['final_score'] = ($testInfo['tminus_mark']==1?round(($result_list[$userTestProp['utid']]['correct']-($result_list[$userTestProp['utid']]['incorrect']/3)),2):$result_list[$userTestProp['utid']]['correct']);
}

这里是GetNumOfAnswers方法:

function GetNumOfAnswers($type,$utid)
    {
        global $table_prefix;
        $total_correct_answers=$total_incorrect_answers=$total_none_answered=0;
        $QuestionList = $this->GetLessonQuestionListByUTID($utid,NULL);
        foreach ($QuestionList as $QuestionInfo)
        {
            switch ($this->IsCorrect($utid,$QuestionInfo['qid']))
            {
                case '1':
                    $total_correct_answers++;
                    break;
                case '0':
                    $total_incorrect_answers++;
                    break;

                default:
                    $total_none_answered++;
                    break;
            }
        }

        switch ($type) {
            case 'correct':
                return $total_correct_answers;
                break;
            case 'incorrect':
                return $total_incorrect_answers;
                break;
            case 'none':
                return $total_none_answered;
                break;
            case 'all':
                return count($QuestionList);
                break;
        }
    }

这里是IsCorrect方法:

function IsCorrect($utid,$qid)
    {
        global $table_prefix;
        $query = $this->link->prepare("SELECT `qcid` FROM `".$table_prefix."user_test_question` INNER JOIN `".$table_prefix."user_test_question_choice` 
            ON `".$table_prefix."user_test_question`.`utqid`=`".$table_prefix."user_test_question_choice`.`utqid` WHERE `utid`=? AND `qid`=?");
        $values = array($utid,$qid);
        $query->execute($values);
        $qcid_chosen = $query->fetchAll();
        if(empty($qcid_chosen))
            return 2;
        $query = $this->link->prepare("SELECT `qcid` FROM `".$table_prefix."questions` INNER JOIN `".$table_prefix."questions_choices` 
            ON `".$table_prefix."questions`.`qid`=`".$table_prefix."questions_choices`.`qid` WHERE `".$table_prefix."questions`.`qid`=? AND `qcis_answer`=1");
        $values = array($qid);
        $query->execute($values);
        $qcid_correct = $query->fetchAll();

        if ($qcid_chosen==$qcid_correct)
            return 1;
        else
            return 0;

        return $counts;
    }

1 个答案:

答案 0 :(得分:0)

您的结构/应用中肯定存在一些设计缺陷。 MySQL我非常有能力计算大数据集的平均值。

您的数据结构是否方便安排以获得最简单的阅读效果?

您的表是否在最常读的列上编入索引?

您的应用程序平均逻辑是否在MySQL中作为一个查询运行?还是用PHP运行?

如果它在PHP中运行,那么很可能你的共享托管环境有一个超出你的PHP memory_limit(因为你试图从MySQL中读取大量数据到PHP及其过程)。