Mysql 使用laravel从数据库获取详细信息

Mysql 使用laravel从数据库获取详细信息,mysql,laravel-5,eloquent,Mysql,Laravel 5,Eloquent,我试图从数据库中获取详细信息,并在下面编写了Laravel代码,以便使用join查询从两个表中获取详细信息。但是在获取数据时花费的时间太多了 你们能帮帮我吗?因此,获取API所需的时间最短 public function getWebsiteVisitorsDetails($businessId, $startTime, $endTime, $filterType){ $chatBotClicked = DB::table('chatbot_website_visitor_

我试图从数据库中获取详细信息,并在下面编写了Laravel代码,以便使用join查询从两个表中获取详细信息。但是在获取数据时花费的时间太多了

你们能帮帮我吗?因此,获取API所需的时间最短

   public function getWebsiteVisitorsDetails($businessId, $startTime, $endTime, $filterType){
        $chatBotClicked = DB::table('chatbot_website_visitor_detail')
            ->join('website_visitors_task', 'website_visitors_task.website_session_id', '=', 'chatbot_website_visitor_detail.website_session_id')
            ->where('website_visitors_task.session_task_perform' , 3)
            ->where('chatbot_website_visitor_detail.business_id', $businessId);
        if($filterType != ApiConstant::ALL_TIME){
            $chatBotClicked = $chatBotClicked->whereBetween('website_visitors_task.created_at', [$startTime, $endTime]);
        }

        $feedbackClicked = DB::table('chatbot_website_visitor_detail')
            ->join('website_visitors_task', 'website_visitors_task.website_session_id', '=', 'chatbot_website_visitor_detail.website_session_id')
            ->where('website_visitors_task.session_task_perform' , 2)
            ->where('chatbot_website_visitor_detail.business_id', $businessId);
        if($filterType != ApiConstant::ALL_TIME){
            $feedbackClicked = $feedbackClicked->whereBetween('website_visitors_task.created_at', [$startTime, $endTime]);
        }

        $firstTimeVisitor = DB::table('chatbot_website_visitor_detail')
            ->select('chatbot_website_visitor_detail.id')
            ->where('chatbot_website_visitor_detail.business_id', $businessId)
            ->where('chatbot_website_visitor_detail.from_popup_widget', 1);
        if($filterType != ApiConstant::ALL_TIME){
            $firstTimeVisitor = $firstTimeVisitor->whereBetween('chatbot_website_visitor_detail.created_at', [$startTime, $endTime]);
        }

        $returnData['chatbot_clicked'] = $chatBotClicked->count();
        $returnData['feedback_clicked'] = $feedbackClicked->count();
        $returnData['first_time_user'] = $firstTimeVisitor->count();
    }

使用缓存?您是否在表中设置了索引/外键?“太多时间”是多少?不,我没有设置索引,但我使用了外键。获取80000计数需要40秒以上的时间。第一步是添加索引,否则任何修复/更改都是毫无意义的。另一个技巧是用来跟踪慢速质询。