【问题标题】:Query takes to much time to execute laravel查询需要很长时间才能执行 laravel
【发布时间】:2021-12-25 20:39:13
【问题描述】:

我正在上传一个 excel 文件,其中包含用户数据及其产品状态 (0,1)。

我想先将产品数据保存在 Productsale 表中,包括 user_id、product_id、target_month 和 status。

然后我获取所有用户,然后从 productsale 表中获取产品及其状态并对其进行计数并将其结果保存在 Saleproduct 表中。

我在 excel 文件中有 33000 行,在 productsale 中节省了 300k,因为每个用户都有 8 个产品,

这里是excel的SS

这是我的代码

try {
          $path = $request->file('file')->store('upload', ['disk' => 'upload']);
          $value = (new FastExcel())->import($path, function ($line) {
            $user = User::where('code', $line['RVS Code'])->first();
            $store = Store::where('code', $line['Customer Code'])->first();
            $a = array_keys($line);
            $total_number = count($a);
            $n = 4;
            $productsale= 0;
            for ($i=3; $i<$total_number; $i++) {
              $str_arr = preg_split('/(ml )/', $a[$i]);
                $product = Product::where('name', $str_arr[1] ?? null)->where('type', $str_arr[0] . 'ml')->first();
                if (!empty($product)) {
                    $product = ProductSale::updateOrCreate([
                    'user_id' => $user->id,
                    'store_id' => $store->id,
                    'month' => $line['Target Month'],
                    'product_id' => $product->id,
                    'status' => $line[$str_arr[0] . 'ml ' . $str_arr[1]],
                  ]);
            }
         }
          });
          //sales
          $datas = User::all();
          foreach($datas as $user){
            $targets = Target::where('user_id',$user->id)->get();
              foreach($targets as $target){
              $sales =   Sales::where('user_id', $user->id)->where('month',$target->month)->first();
                $products = Product::all();
                foreach ($products as $product) {
                  $totalSale = ProductSale::where('user_id',$user->id)->where('month',$target->month)->where('product_id',$product->id)->sum('status');
                  $sale_product = SalesProduct::updateOrCreate([
                    'product_id' => $product->id,
                    'sales_id' => $sales->id,
                    'sale' => $totalSale,
                ]);
                }
  
              }
          }

          return response()->json(true, 200);
      }

【问题讨论】:

  • 因此,您当前的问题是对于每个用户,您执行的似乎是 4 个查询,其中一个是总和。您可以通过使用eager loading 的大部分关系来缓解这种情况

标签: php mysql laravel eloquent laravel-8


【解决方案1】:

您正在为每个用户的每个目标的每个产品从“糟糕的数据库:(”请求一个单独的查询。

  • 尝试实现产品、销售、用户、目标之间的关系,并在获取用户时预先加载它们
  • 小心在每个单独的循环中执行的每个查询
  • 获取您的最终查询并在您的数据库中使用 EXPLAIN 命令来分析执行代码的后端会发生什么。
  • 获取活动数据库慢查询日志,然后对其进行优化。
  • 将您的数据库表引擎更改为 MyISAM。

您可以做很多其他事情来优化您的查询。

【讨论】:

    【解决方案2】:

    如果运行时间过长(并且可能导致返回错误),则不要同步执行 - 即。而用户正在等待 - 但它是异步的。

    您的控制器应该只关心验证文件,将其保存到存储中,并将成功代码返回给用户,告诉他们文件已上传,很快就会处理。

    然后将有关文件处理的所有代码移动到一个作业中,该作业由控制器调度,该作业在后台运行。理想情况下,这将在队列中(在这种情况下,请查看您的队列设置,因为默认值为 30 秒以使作业在那里完成,这与您可能已经犯规的 PHP 设置一致,因此请准备好允许作业执行时间更长)。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 2016-04-22
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2011-01-17
      • 2012-12-03
      相关资源
      最近更新 更多