Prestashop 1.7.6.8 getproducts功能缓慢



我的自定义模块PS 1.7.6.8遇到了一个奇怪的问题。

让我先解释一下:

  • 我从一个现有的模块中构建了一个自定义模块,用于通过一个名为Ktype的唯一ID搜索汽车零件。这非常有效
  • 我使用Prestashop Search控制器来搜索产品,然后返回它们,并使用ProductAssembler和ProductListingAssembler构建产品列表

搜索控制器从我的模块中获取来自Make、Model、Year和Device的ID,所有信息都存储在数据库表中,包括:Product ID’s、ID Make、ID Model、ID Year、ID Device。

选择品牌、型号、年份和设备后,我从id_products构建一个数组,并将其存储到变量中:pSQL($id_productions(

然后,为了展示产品,我使用了这个功能:

public function getProducts($where, $id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
{
# validate module
unset($check_access);
if (!$context) {
$context = Context::getContext();
}
$front = true;
if (!in_array($context->controller->controller_type, array('front', 'modulefront'))) {
$front = false;
}
if ($p < 1) {
$p = 1;
}
$id_supplier = (int) Tools::getValue('id_supplier');

$sql = 'SELECT DISTINCT p.id_product, p.*, product_shop.*, pl.`description`, pl.`description_short`, pl.`available_now`,
pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, m.`name` AS manufacturer_name, cl.`name` AS category_default, product_shop.price AS orderprice
FROM `' . _DB_PREFIX_ . 'category_product` cp
INNER JOIN `' . _DB_PREFIX_ . 'product` p
ON p.`id_product` = cp.`id_product`
' . Shop::addSqlAssociation('product', 'p') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa
ON (p.`id_product` = pa.`id_product`)
' . Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1') . '
LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl
ON (product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('cl') . ')
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl
ON (p.`id_product` = pl.`id_product`
AND pl.`id_lang` = ' . (int) $id_lang . Shop::addSqlRestrictionOnLang('pl') . ')
LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m
ON m.`id_manufacturer` = p.`id_manufacturer`
' . $where . '
'
. (Tools::getValue('filter_cat') ? ' AND cp.id_category='. (int) Tools::getValue('filter_cat') : '');

if (Tools::getValue('order') == 'product.position.asc') {
$orderby = 'cp.position ASC';
}
if (Tools::getValue('order') == 'product.name.asc') {
$orderby = 'pl.name ASC';
}
if (Tools::getValue('order') == 'product.name.desc') {
$orderby = 'pl.name DESC';
}
if (Tools::getValue('order') == 'product.price.asc') {
$orderby = 'product_shop.price ASC';
}
if (Tools::getValue('order') == 'product.price.desc') {
$orderby = 'product_shop.price DESC';
}
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
foreach ($result AS $row) {
if ($row2 = Product::getProductProperties($id_lang, $row)) {
$products[] = $row2;
}
}           
return $products;
}

$where variable=where p.id_product IN(pSQL$id_products(

奇怪的是,当这个SQL查询被调用时:

  1. 我可以通过phpMyAdmin看到,这个查询只需要不到1秒的时间就可以完成
  2. 然后,接下来执行许多其他查询,可能来自产品控制器

在我的pSQL($id_products(变量中,我可以用逗号分隔大约4000个产品id,我可以看到每个产品id都执行了产品控制器查询,并且对于前控制器需要的每个参数(一个查询图像、一个查询数量、一个搜索类别、一个查找add_date等(,导致大量的查询(单个搜索的平均查询量约为150.000(。

我使用的是一个64 GB RAM、16核Intel I7的VPS,所有索引都使用得很好。

此查询大约需要15秒才能完成,这对于网店来说是不可用的。

我已经问过核心开发人员,他们以前是否见过这样的问题,但我从来没有得到任何关于PS支持的答案。

有什么方法可以避免这些多个子查询吗?

它是否来自SQL查询中的WHERE IN子句?由于每个值都是逗号分隔的,因此控制器可能将其理解为单个产品,从而导致所有值都有多个子查询。

如果我使用JOIN而不是WHERE IN子句,它可以工作吗?或者我需要保留WHERE IN子句,以便产品控制器工作?。

如果有人能在这件事上帮我,这会让我发疯的。

谢谢你的帮助!

对于每一行,您都调用Product::getProductProperties,这是一项繁重的功能。

您应该估计是否需要该方法中的数据,以及是否不需要覆盖getProducts并避免Product::getProductProperties

另一方面,4000种产品在一个类别中我认为太多了,你应该细分。

最新更新