Laravel提高SQL速度

时间:2018-10-12 17:10:32

标签: php sql laravel eloquent

我正在尝试提高在 Laravel 5.7 中的查询速度,并且我将通话时间缩短到约2.5秒。我正在尝试找出更多使它更快的方法,如果能得到帮助,我将不胜感激。

谢谢

我的数据的结构:enter image description here

功能(控制器):

public function getUserDataTmp(Request $request) {

        $input = file_get_contents("php://input");
        $request = json_decode($input);

        if ($this->authTokenAccess($request) == true) {

            $bottomWords = bottom_exterior_word::select('word','sentence','sequence','id','group_id')->where('user_id','=', $request->id)->get();

            $emergencyWords = left_exterior_word::select('word','sentence','sequence','id')->where('user_id','=', $request->id)->get();

            foreach($bottomWords as $tmp => $key) {
                $group_id = $key->group_id;
                $bottomWords->user_id = $request->id;
                $bottomWords[$tmp]->words = $key->getMainWords($group_id, $request->id);
            }

            foreach($emergencyWords as $key => $word) {
                $emergencyWords[$key]->image = imageModel::select('base64','id')->where('emergency_id','=', $word->id)->first();

            }

            $data = [
                'data' => [
                    'return' => 'success',
                    'code' => 'VEDC001',
                    'response' => 'Successfully Gathered Words',
                    'main_categories' => $bottomWords,
                    'emergency_words' => $emergencyWords
                ]
            ];

            return(json_encode($data));

        }

    }

getMainWords函数(bottom_exterior_word模型):

    public function getMainWords($group_id, $id)
  {
      // return("TEST");
      $words = \App\main_word::select('id','group_id','sentence','sequence','word')->where('group_id','=', $group_id)->where('user_id','=', $id)->get();
      foreach ($words as $key => $word) {
          $words[$key]->image = Image::select('base64','id')->where('word_id','=', $word->id)->first();
      }
      return $words;
  }

2 个答案:

答案 0 :(得分:1)

从重构开始,这样您就不会在foreach循环内进行查询

foreach($bottomWords as $tmp => $key) {
            $group_id = $key->group_id;
            $bottomWords->user_id = $request->id;
            $bottomWords[$tmp]->words = $key->getMainWords($group_id, $request->id);
        }

我将更改getMainWords函数以接受一组组ID并使用whereIn子句:

  

whereIn方法验证是否包含给定列的值   在给定的数组中:

     

$ users = DB :: table('users')                       -> whereIn('id',[1、2、3])                       -> get();

对此循环进行相同的处理。

        foreach($emergencyWords as $key => $word) {
            $emergencyWords[$key]->image = imageModel::select('base64','id')->where('emergency_id','=', $word->id)->first();

        }

通常,将查询数量最小化将缩短响应时间。

答案 1 :(得分:1)

旧帖子,但只想更新。自从我第一次发布此文章以来,我已经了解了很多有关Laravel的知识,并且对它有了更多的经验。

这是我的新功能和解决方案:

控制器:

public function data(Request $request)
{
    return response()->success(
        [
            'emergencywords' => EmergencyWord::with('image')->whereUserId($request->user()->id)->get(),
            'categorywords' => CategoryWord::with(['image','words.image'])->whereUserId($request->user()->id)->get(),
        ]
    );
}

类别词关系:

public function image()
{
    return $this->hasOne('App\Image','id','image_id');
}

public function words()
{
    return $this->hasMany('App\MainWord','category_words_id','sequence');
}

紧急单词关系:

public function image()
{
    return $this->hasOne('App\Image','id','image_id');
}

主要单词关系:

public function image()
{
    return $this->hasOne('App\Image','id','image_id');
}