简体   繁体   English

Opencart 3 getProduct 方法导致页面加载缓慢

[英]Opencart 3 getProduct method causes slow page loading

My Opencart 3.0 is running very slow.我的 Opencart 3.0 运行很慢。 In the.network tab in the chrome browser inspector, it records 23.02 seconds for the category page to load.在 chrome 浏览器检查器的 .network 选项卡中,它记录了加载类别页面的 23.02 秒。

When I try to debug it, I could see that the slow loading happens here当我尝试调试它时,我可以看到加载缓慢发生在这里

catalog/model/catalog/product.php - public function getProduct($product_id)

Inside this method, when I comment out this line in the returned array - 'product_id' => $query->row['product_id'] , the loading speed comes to 7s.在这个方法中,当我在返回的数组中注释掉这一行时 - 'product_id' => $query->row['product_id'] ,加载速度达到 7s。

The method getProduct($product_id) is called in this method getProducts($data = array()) .方法getProduct($product_id)在此方法getProducts($data = array())中被调用。 The part where the method is called looks like below.调用该方法的部分如下所示。

$query = $this->db->query($sql);
foreach ($query->rows as $result) {
   $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}

When I comment out this line当我注释掉这一行时

//$product_data[$result['product_id']] = $this->getProduct($result['product_id']);

The loading speed goes to 3.5s.加载速度达到3.5s。

What I could not figure out is why this part of the code takes time to return - 'product_id' => $query->row['product_id']我想不通的是为什么这部分代码需要时间返回 - 'product_id' => $query->row['product_id']

I will appreciate your help.我会感谢你的帮助。

It look like your issues comes from getProduct() and getProducts() methods in the catalog/model/catalog/product.ph p file.看起来您的问题来自catalog/model/catalog/product.ph p 文件中的getProduct()getProducts()方法。 Especially this row:特别是这一行:

'product_id' => $query->row['product_id']

So you could check your DB performance, how much time is taking to complete this query.所以你可以检查你的数据库性能,完成这个查询需要多少时间。

  1. If it takes too much time and your columns are not indexed, you could index them.如果花费太多时间并且您的列没有索引,您可以索引它们。
  1. You could also try to limit the number of rows being retrieved in the query by using a LIMIT clause or a WHERE clause.您还可以尝试使用 LIMIT 子句或 WHERE 子句来限制查询中检索的行数。

  2. You could use Opencart cache system, to reduce time.您可以使用 Opencart 缓存系统来减少时间。

On first sign it looks more like DB related problem, instead of code problem.在第一次登录时,它看起来更像是与数据库相关的问题,而不是代码问题。

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

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