【问题标题】:Changing the random product selection mechanism in Mangento更改 Magento 中的随机产品选择机制
【发布时间】:2013-11-27 15:53:50
【问题描述】:

我们的 Magento 安装有一个主页块,它显示来自给定类别的随机选择的产品。它可以工作,但它真的很慢。我将罪魁祸首追踪到以下功能:

protected function _getProductCollection()
{
    if (is_null($this->_productCollection))
    {
        $categoryID = $this->getCategoryId();
        if($categoryID)
        {
            $category = new Mage_Catalog_Model_Category();
            $category->load($categoryID);
            $collection = $category->getProductCollection();
        }
        else
        {
            $collection = Mage::getResourceModel('catalog/product_collection');
        }
        Mage::getModel('catalog/layer')->prepareProductCollection($collection);

        if ($this->getIsRandom())
        {
            $collection->getSelect()->order('rand()'); // REALLY slow, approx 4000ms at 25k products
        }

        $collection->addStoreFilter();
        $productCount = $this->getProductCount() ? $this->getProductCount() : 8;
        $collection->setPage(1, $productCount)
            ->load();

        $this->_productCollection = $collection;
    }
    return $this->_productCollection;
}

具体来说,$collection->getSelect()->order('rand()'); 语句非常很慢,在我们当前的产品数量约为 25k 时,我们的第一个字节的时间增加了大约 4000 毫秒。只需禁用随机性即可将我们的页面加载时间缩短约 3700 毫秒。

我想在应用程序级别执行随机化,而不是使用众所周知的慢 MySQL ORDER BY RAND() 方法。我尝试应用:Randomise & limit Category thumbs on homepage magento中描述的方法,具体改:

$collection->getSelect()->order('rand()');

进入这个(及其变化的排列):

$collection->getSelect()->addIdFilter(array_rand(array_flip($category->getAllChildren(true)),5));

效果是现在前端根本不显示块。我将不胜感激有关如何实施上面链接的解决方案的任何建议,或任何其他有效的方法来随机化上面引用的 $collection 变量。

【问题讨论】:

    标签: php magento zend-framework random


    【解决方案1】:

    这是上述问题的有效解决方案;这使用了应用程序,而不是数据库级别的随机化,并且确实对 Magento 的 time-to-first-byte 产生了显着的改进。

    function _getProductCollection()
    {
    
        if (is_null($this->_productCollection)) {
            $categoryID = $this->getCategoryId();
            if ($categoryID) {
                $category = new Mage_Catalog_Model_Category();
                $category->load($categoryID);
                $collection = $category->getProductCollection();
            } else {
                $collection = Mage::getResourceModel('catalog/product_collection');
            }
    
            Mage::getModel('catalog/layer')->prepareProductCollection($collection);
    
            $maxsize = $collection->getSize();
            $randArray = array();
            while (count($randArray) <= 50) {
                $randArray[] = rand(1, $maxsize);
            }
    
            if ($this->getIsRandom()) {
                $collection->getSelect()->where('product_id IN (?)',$randArray)->order('RAND()');
            }
    
            $collection->addStoreFilter();
            $productCount = $this->getProductCount() ? $this->getProductCount() : 8;
            $collection->setPage(1, $productCount)->load();
    
            $this->_productCollection = $collection;
        }
        return $this->_productCollection;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多