【问题标题】:Fetching report taking lot of time laravel获取报告花费大量时间 laravel
【发布时间】:2020-09-30 05:38:02
【问题描述】:

我有一个项目,其中包含有关他们的购车体验的问题和答案,并在从展厅购买车辆后产生投诉,所以他们打电话给客户并询问有关从展厅购车体验的一组问题,这是项目的大纲laravel 5.3 或 5.6 的老项目,现在的问题是

当他们进行 1 个月的报告时,其中包含客户详细信息和车辆详细信息以及问题及其对问题的回答的详细信息,但如果我需要 1 个月的报告,则我的意思是 很久,我试图清除缓存和东西,仍然出现问题 任何人都可以帮助我,数据包含大约2000多个数据

   public function excelquerep24(Request $request)
    {

        //getting all questions
        $questions = Question::where([['question_schedul', 2], ['status', 0]])->orderBy('order_no', 'asc')->get();

        //get answered customers;
        $answeredcalls =  Registration::
        join('ssi_tracks', 'ssi_tracks.ssi_track_id','=','registrations.registration_id')
        ->select('registrations.customerid', 'registrations.customername', 'registrations.phone', 'registrations.chassis', 'registrations.model', 'registrations.invoicedate', 'registrations.delivery', 'registrations.dealership', 'registrations.zone', 'registrations.branchcode', 'registrations.dh','registrations.zsm', 'registrations.branch', 'registrations.dse',  'ssi_tracks.ssi_track_id')
        ->where('ssi_tracks.track_first_status', '>','0')
        ->whereDate('registrations.delivery', '>=', $request->input('datefrom'))
        ->whereDate('registrations.delivery', '<=', $request->input('dateto'))
        ->distinct('registrations.customerid')
        ->get();
     
            $compsArray = [];


            // Define the Excel spreadsheet headers
        $compsArray[] = ['sl_no', 'Customer ID', 'Customer Name', 'Mobile', 'Chassis No', 'Vehicle Model', 'Invoice Date', 'Delivery Date', 'Dealership','Zone','Branch Code', 'DH','ZSM/SM',  'Branch', 'DSE',   'Status','PSF Date'];
        // return $compsArray;
         foreach ($questions as $question) {
            array_push($compsArray[0], $question->question);
            }
        // Convert each member of the returned collection into an array,
        // and append it to the payments array.

        $array = [];
        $i = 1;
        foreach ($answeredcalls as $call) {
            $newDateFormat3 = date('d-m-Y', strtotime((string)$call->delivery));
            $newDateFormat4 = date('d-m-Y', strtotime((string)$call->invoicedate));
            $call->delivery = $newDateFormat3;
            $call->invoicedate=$newDateFormat4;
            $psf24LastCallDate='';
            if ($call->zsm == '') {
                $sm = Registration::select('sm')->where('customerid', $call->customerid)->first();
                $call->zsm = $sm->sm;
            }
            $ssi = Ssi_track::where('ssi_track_id', $call->ssi_track_id)->first();
            if ($ssi->track_first_status == 1)
                $status = 'Partilly Called';
            if ($ssi->track_first_status == 2)
            {
               $lastcall= Call_track::where([['ssi_track_id', $call->ssi_track_id], ['call_schedule', 1]])->orderBy('call_track_id', 'desc')->first();
               if($lastcall)
               {
                
               
                if ($lastcall->call_responce == 1)
                   $status = 'Call Attended';
               if ($lastcall->call_responce == 2)
                   $status = 'Switched Off';
               if ($lastcall->call_responce == 3)
                   $status = 'Not Responding' ;
               if ($lastcall->call_responce == 4)
                   $status = 'Network Busy';
               if ($lastcall->call_responce == 5)
                   $status = 'Out of Coverage';
                 }  }      
            if ($ssi->track_first_status == 3)
                $status = 'Black listed call';
            if ($ssi->track_first_status == 4)
                $status = 'Call Completed';
            if ($ssi->track_first_status ==5 )
                $status = 'Call Closed';
            $tempArray = json_decode($call, true);
            array_unshift($tempArray, $i);
            $answeredArray = $tempArray;
            array_push($answeredArray,$status);
           $lastcall= Call_track::where([['ssi_track_id', $call->ssi_track_id], ['call_schedule', 1]])->orderBy('call_track_id', 'desc')->first();
           if($lastcall)
           $psf24LastCallDate = date('d-m-Y', strtotime((string)$lastcall->created_at));
                array_push($answeredArray,$psf24LastCallDate );
            
        
            foreach ($questions as $question) {
                $answer = Customer_answer:: join('questions', 'customer_answers.question_id', '=', 'questions.question_id')
                ->select('customer_answers.answer')->where([['customer_answers.ssi_track_id',$call->ssi_track_id ], ['questions.question_schedul', 2],['questions.status', 0],['customer_answers.question_id',$question->question_id]])
                ->orderBy('questions.order_no', 'asc')
                ->first();
                if($answer)
                    array_push($answeredArray, $answer->answer);
                else
                    array_push($answeredArray, " ");
            }
            
            unset($answeredArray['ssi_track_id']);

            $compsArray[] = $answeredArray;

            $i++;
        }

        // Generate and return the spreadsheet
        Excel::create('questionwise report 24 hours', function ($excel) use ($compsArray) {

            // Set the spreadsheet title, creator, and description
            $excel->setTitle('Questionwise');
            $excel->setCreator('Laravel')->setCompany('test, LLC');
            $excel->setDescription('payments file');

            // Build the spreadsheet, passing in the payments array
            $excel->sheet('sheet1', function ($sheet) use ($compsArray) {
                $sheet->fromArray($compsArray, null, 'A1', false, false)
                    ->getStyle('A1')
                    ->getAlignment()
                    ->setWrapText(true);
            });

        })->download('xlsx');
    }

【问题讨论】:

  • $answeredcalls 是否计算 2000 个数据对象或者您在数据库中有 2000 行?
  • 注册表包含 17633 个数据,以及 40 行,但我没有获取所有数据,因为您可以看到我只从注册表中获取特定数据,ssi_tracks 表也包含相同的 17633 数据但 10 行,但如果我拿一个月的数据,它至少接近 1000 个数据@ZoranStankovic
  • 这是相当数量的数据,您首先需要检查是否正确设置了索引,这将提高您的查询速度。另一件事,更重要的是尝试减少查询次数。您每次都在循环数据并进行查询,这非常耗时。您应该考虑编写一个查询,该查询将提取您所有需要的数据,而不是循环并将其推送到数组。此外,您应该避免使用 ORM (Eloquent),因为 ORM 在处理大数据集时并不是特别好。
  • 你能帮我吗,如何索引数据类型的东西@ZoranStankovic
  • 您需要创建迁移并定义要索引的列。我建议您为用于搜索、排序和关系的所有列定义索引,例如:questions.order_no、customer_answers.question_id、questions.question_id。除了那些你应该根据我上面的说明添加所有列的内容。然后运行迁移并等待一段时间让 SQL Server 构建索引,然后再次测量执行时间。我认为如果您想获得最佳性能,编写适当的 SQL 查询将一次提取所有数据至关重要。

标签: mysql laravel laravel-5


【解决方案1】:

感谢@Zoran Stankovic 的想法,创建索引确实提高了速度,所以我不得不在模型中索引数据,参考creating-indexes

导入

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

我在模型上所做的代码示例

注册模式

public function regindex()
    {
        Schema::table('registrations', function (Blueprint $table) {
           $table->index('registration_id');
        });
    }

供客户回答

 public function customerindex()
    {
     Schema::table('customer_answers', function (Blueprint $table) {
      $table->index(['ssi_track_id','question_id']);
  });
 } 

对于 ssi_track 模型

public function ssiindex()
    {
      Schema::table('ssi_tracks', function (Blueprint $table) {
         $table->index(['ssi_track_id','registration_id']);
      });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多