Php 由于在循环中运行查询,如果您构造一个连接的查询而不是在循环中运行额外的查询,则会更好。您的查询速度很慢,因为您是在循环中运行查询。将您的查询放在循环外以加快速度。您有两个#code…,因此我们是盲目的。如上所述,如果你能将它移动到mysql中,它就会尖叫起

Php 由于在循环中运行查询,如果您构造一个连接的查询而不是在循环中运行额外的查询,则会更好。您的查询速度很慢,因为您是在循环中运行查询。将您的查询放在循环外以加快速度。您有两个#code…,因此我们是盲目的。如上所述,如果你能将它移动到mysql中,它就会尖叫起,php,mysql,laravel-4,Php,Mysql,Laravel 4,由于在循环中运行查询,如果您构造一个连接的查询而不是在循环中运行额外的查询,则会更好。您的查询速度很慢,因为您是在循环中运行查询。将您的查询放在循环外以加快速度。您有两个#code…,因此我们是盲目的。如上所述,如果你能将它移动到mysql中,它就会尖叫起来comparison@HarshitShrivastava更新了我的答案仍然执行时间超过了600秒我想,你能帮我写一个关于外来id的连接查询实现索引吗,用户id。查看是否减少了执行时间我得到了一些偏移错误,可能有些数据在数据库中没有正确索引编


由于在循环中运行查询,如果您构造一个连接的查询而不是在循环中运行额外的查询,则会更好。您的查询速度很慢,因为您是在循环中运行查询。将您的查询放在循环外以加快速度。您有两个
#code…
,因此我们是盲目的。如上所述,如果你能将它移动到mysql中,它就会尖叫起来comparison@HarshitShrivastava更新了我的答案仍然执行时间超过了600秒我想,你能帮我写一个关于外来id的连接查询实现索引吗,用户id。查看是否减少了执行时间我得到了一些偏移错误,可能有些数据在数据库中没有正确索引编辑了我的响应,添加了检查数据是否存在。执行时间从10.20分钟减少到4.48分钟,尽管如此,我还是会接受您的回答,但如果您认为有什么方法可以提高性能,请让我知道,我认为您可以从数据库中仅选择您实际使用的问题和答案。编辑答案。
function UpdateAnswer()
{
ini_set('max_execution_time', 800);
        $users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));

foreach($users as $user)
       {
         $answers= DB::select(DB::raw("select * from answers where  user_id='$user->id' "));

         foreach($answers as $answer)
                {

            $questionDetNew = '';
            $questionDetOld = DB::select(DB::raw("select * from questions where  id='$answer->question_id' "));
             if(is_array($questionDetOld))
                echo "questionDetOld true";

            foreach ($questionDetOld as $question) {
                # code...
                echo "<br/>";
                echo $question->id;
                echo $question->question_text;
                echo $user->event; echo"<br/>";


                $questionDetNew= DB::select(DB::raw("select * from questions where question_text='$question->question_text' and event='$user->event' and question_group='$question->question_group' "));

            }

            if(is_array($questionDetNew)){
            foreach ($questionDetNew as $questionx) {
                # code..
                if($questionx->id!=NULL){
                            echo "Updated<br/>";
            //DB::select(DB::raw("update answers set question_id='$questionx->id' where id='$answer->id' "));
        }
        else {
                echo "Its already existing ";
        }

            }
        }
     }
   }

}
ini_set('max_execution_time', 800);
        $users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));
        // $users = User::where('role','!=','admin_dd')->orWhere('role','!=','sales_dd')->orderby('name')->get();
        $answers= DB::select(DB::raw("select * from answers "));
        $questionDetOld = DB::select(DB::raw("select * from questions"));

        foreach($users as $user)
        {
            foreach ($answers as $answer) {

                if($answer->user_id==$user->id)
                {
                    foreach($questionDetOld as $question)
                    {
                        if($question->id==$answer->question_id  )
                        {

                                    echo "<br/>";
                                    echo $question->id;
                                    echo $question->question_text;
                                    echo $user->event; echo"<br/>";
                                    //$questionDetNew= DB::select(DB::raw("select * from questions where question_text='$question->question_text' and event='$user->event' and question_group='$question->question_group' "));

                                    foreach ($questionDetOld as $questionx) {
                                        if($questionx->question_text==$question->question_text && $questionx->event==$user->event && $questionx->question_group==$question->question_group)
                                        {
                                          echo "Updated<br/>";
                                        }
                                    }
                        }
                    }
                }
            }
        }
$INuserdata[] = array();
$INanswerdata[] = array();
$users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));
foreach($users as $user)
     $INuserdata[] = $user->id;

$answers= DB::select(DB::raw("select * from answers where  user_id IN (".implode(',',$INuserdata).")"));
foreach($answers as $answer)
    $INanswerdata[] = $answer->question_id;

$questionDetOld = DB::select(DB::raw("select * from questions where  id IN (".implode(',',$INanswerdata).")"));
    ini_set('max_execution_time', 800);

    $userIds = array();
    $users= DB::select(DB::raw("select * from users where role!='admin_dd' and role!='sales_dd' "));
    forach ($users as $user) {
        $userIds[] = $user->id;
    }

    $questionIds = array();
    $answers_tmp= DB::select(DB::raw("select * from answers where user_id in (".implode(',',$userIds).')'));
    foreach ($answers_tmp as $answer) {
        $answers[$answer->user_id][] = $answer;
        $questionIds[] = $answer->question_id;
    }

    $questionDetOld_tmp = DB::select(DB::raw("select * from questions where id in (".implode(',',$questionIds).')'));
    foreach ($questionDetOld_tmp as $question) {
        $questionDetOld[$question->id][] = $question;
    }

    foreach($users as $user)
    {
        if (isset($answers[$user->id]))
        {
            foreach ($answers[$user->id] as $answer)
            {
                if (isset($questionDetOld[$answer->question_id]))
                {
                    foreach($questionDetOld[$answer->question_id] as $question)
                    {
                        echo "<br/>";
                        echo $question->id;
                        echo $question->question_text;
                        echo $user->event; echo"<br/>";
                        //$questionDetNew= DB::select(DB::raw("select * from questions where question_text='$question->question_text' and event='$user->event' and question_group='$question->question_group' "));

                        foreach ($questionDetOld_tmp as $questionx)
                        {
                            if($questionx->question_text==$question->question_text && $questionx->event==$user->event && $questionx->question_group==$question->question_group)
                            {
                                echo "Updated<br/>";
                            }
                        }
                    }
                }
            }
        }
    }