电子商务应用程序和数据库查询的复杂功能

问题描述 投票:0回答:0

我正在尝试构建一个电子商务应用程序,但我面临着一个性能问题,比我更好、更有经验的人可能能够为我提供一些想法。

我想要实现的功能: 客户可以选择两种运输方式

  • 送货
  • 皮卡

如果客户选择送货,应用程序会要求他提供邮政编码,并且他只能看到商店中实际送货的产品。

如果客户选择提货,应用程序会要求他选择他想去的商店并从中提货,并且他只能看到该商店的产品。

在我的应用程序中,我设置了以下关系:

  • 产品模型与类别模型多对多关系,反之亦然
  • 商店模型与产品模型多对多关系,反之亦然
  • Zip 模型与 Store 模型属于一种关系
  • Store 模型与 Zip 模型有很多关系

我已经开发了前面提到的功能,我认为我不需要任何帮助。 当客户浏览类别时,问题出现在性能上。

让我解释一下原因。

  • 数据库中存在的产品总数为 14.000。
  • id为5的类别有800个产品。
  • id为25的商店有8000种产品。

这是我的单个类别的控制器:

public function single($slug, $brands = null, $labels = null) {
  $category = Category::where('slug', $slug)->first();
  $cart = new Cart;
  //if user selected shippinh method then $cart->store exists and holds the id of the store that he should see
  if ($cart->store) {
    $storeId = intval($cart->store);
    $store = Store::find($storeId);
    $products = $category->products()->whereHas('stores', function($q) use ($store) {
            $q->where('stores.id', $store->id);
        })->paginate(39);
  }
  //if user has not selected any shipping method yet, he can see all products from this category
  else {
    $products = $category->products()->paginate(39);
  }
  $labels = [];
  $brands = [];
  return view('website.shop.categories.single')->withCategory($category)->withProducts($products)->withLabels($labels)->withBrands($brands);
}

我知道我应该实施缓存。 我实际上正在使用 Redis,并有一个使用 Cache Facade 的重复控制器来缓存上述结果,并且它的工作方式就像一个魅力。 问题是,由于产品每天更新一次,我想在导入命令完成时预先缓存它们。我有一个预热缓存功能来缓存数据,因此我不必等待用户访问结果(以及其余分页页面)来缓存它们。 但这需要很长时间,因为有 42 家不同的商店、14000 种产品和 692 个不同的类别。

我开始认为在缓存实现之前我缺少一些想法或架构,以在我开始缓存数据库结果之前减少性能开销。

请分享您的想法,虽然它工作正常并且缓存速度非常快,但我担心可扩展性。

提前致谢。

php mysql laravel database relationship
© www.soinside.com 2019 - 2024. All rights reserved.