简体   繁体   English

更快的方式来填充数据mysql和php

[英]Faster way to populate data mysql and php

Hi I am using Laravel 5 and Laravel Excel http://www.maatwebsite.nl/laravel-excel/docs . 嗨,我正在使用Laravel 5和Laravel Excel http://www.maatwebsite.nl/laravel-excel/docs

I am working in a reporting module that will be exported to excel. 我正在使用将导出到excel的报告模块。 I have three tables. 我有三张桌子。

Forms, FormsResponses and Metrics

Forms
->id
->phone_number
->calldatetime

FormsReponses
->id
->Forms_id
->Metrics_id
->Response

Metrics
->id
->description

So as you can see I have a Form which has a list of metrics and then the user will select responses for each metrics. 因此,您可以看到我有一个表单,其中包含指标列表,然后用户将为每个指标选择响应。 Resposes can be Yes,No,N/A. 可以是“是”,“否”,“不适用”。 And I have this report. 我有这份报告。 Let's say I have 3 Metrics for now which can be increased if we added new Metrics. 假设我现在有3个度量标准,如果添加新的度量标准,可以增加。

Metrics ID|Metrics Description|Phone1|Phone2|Phone3 ...
__________|___________________|______|______|______|___
1         |Sample1            |Yes   |Yes   |Yes 
__________|___________________|______|______|__________
2         |Sample2            |Yes   |No    |No
__________|___________________|______|______|__________
3         |Sample3            |Yes   |No    |No

So what I'm doing is I am showing the Responses of each phone number on each metrics. 所以我正在做的是显示每个指标上每个电话号码的响应。 I have a From Date and To Date. 我有一个From Date和To Date。 This report will be outputed in an excel file which I already have. 此报告将在我已有的excel文件中输出。 So this report expands Vertically (Depends on the number of Metrics) and as well Horizontally depending on the number of phone numbers i will get in my Date Range. 因此,此报告会垂直扩展(取决于指标的数量)以及水平扩展,具体取决于我将在日期范围中获得的电话号码的数量。 I can populte it properly. 我可以正确地使用它。 But it's too slow. 但它太慢了。 Processing takes a lot if time and I see that it is cause in my code that searches for the Responses. 如果时间处理需要花费很多时间,我发现在我的代码中搜索响应是有原因的。 My code: 我的代码:

$query = "SELECT id, phone FROM qcv.forms WHERE calldatetime >= '$from' AND calldatetime <= '$to' ORDER BY id ASC ;";
$phone = DB::connection('mysql')->select($query);

      $metrics = Metric::all();
      $metric_start = 10;
      $start = "D";
      $count = 10;

      foreach ($phone as $key => $value2) // Populate Phone Numbers Horizontally
      {
          $sheet->cell($start.'9', $value2->phone);
          // This will fill the responses for each number
          foreach ($metrics as $key => $value)
          {
              $responses = FormResponses::where('form_id', '=', $value2->id)->where('metrics_id', '=', $value->id)->get();
              $sheet->cell($start.$count, $responses[0]->response);
              // Populate Metrics Vertically
              $sheet->cell('C'.$count, $value->question);
              $sheet->cell('B'.$count, $value->description);
              $sheet->cell('A'.$count, $value->metrics_name);
             $count++;
          }
          $start++;
          $count = 10;
      }

And

$responses = FormResponses::where('form_id', '=', $value2->id)->where('metrics_id', '=', $value->id)->get();

That line takes up the time because for each phone number it has to search the responses for each metrics I have. 该行占用时间,因为对于每个电话号码,它必须搜索我的每个指标的响应。 So imagine if I have 30 Metrics and 150 Phone numbers. 想象一下,如果我有30个指标和150个电话号码。 It will take time. 需要花时间。 Is there a better approach to it? 有没有更好的方法呢?

Isn't it a better approach to use fromArray method , it would seem it's more efficient instead of creating to track it yourself. 使用fromArray 方法不是更好的方法 ,它似乎更有效,而不是创建自己跟踪它。 I used to same strategy in a export script i created for our company. 我在为我们公司创建的导出脚本中使用了相同的策略。

Create the array's you desire , like: 创建您想要的数组 ,例如:

$forms = [
    ['id', 'phone_number', 'calldatetime'], // headers
    // total of 5 dataset items
];

$formsReponsesStartAtRow = $formsStartAtRow + count($forms) + ;
$formsReponses = [
    ['id', 'Forms_id', 'Metrics_id', 'Response'], // headers
    // total of 15 dataset items
];

$metrics = [
    ['id', 'description']
    // total of 100 dataset items
];

When your data is ready calculate the start row from each array. 数据准备就绪后,计算每个数组的起始行。

// leave between each dataset 2 rows
$rowSpacing = 2;
$startCol = 'A';

// start cell A1
$startAtRow = 1;
$forms = [...];
// add $forms to the sheet
$sheet->fromArray($forms, null, $startCol . $startAtRow);

// calculate start row from previous $startAtRow
// start cell is: 5 + 2 = 7 = A7
$startAtRow = $startAtRow + count($forms) + $rowSpacing;
$formsReponses = [...];
// add $formsReponses to the sheet
$sheet->fromArray($formsReponses, null, $startCol . $startAtRow);

// calculate start row from previous $startAtRow
// start cell is: 7 + 15 + 2 = 24 = A24
$startAtRow = $startAtRow + count($formsReponses) + $rowSpacing;
$metrics = [...];
// add $metrics to the sheet
$sheet->fromArray($metrics, null, $startCol . $startAtRow);

If you add a another dataset that one will start on: 24 + 100 + 2 = 126 = A126 如果添加另一个将开始的数据集: 24 + 100 + 2 = 126 = A126

Whenever your dataset grows your excel anticipate on the changes. 每当您的数据集增长时,您的Excel都会预测更改。

Hope this helps you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM