Php 改变Mangento的随机产品选择机制

Php 改变Mangento的随机产品选择机制,php,magento,zend-framework,random,Php,Magento,Zend Framework,Random,我们的Magento安装有一个主页块,其中显示从给定类别中随机选择的产品。它工作正常,但速度确实很慢。我追踪到了罪犯的以下功能: protected function _getProductCollection() { if (is_null($this->_productCollection)) { $categoryID = $this->getCategoryId(); if($categoryID) {

我们的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的情况下,第一个字节的时间增加了约4000ms。简单地禁用随机性可以减少3700毫秒的页面加载时间

我希望在应用程序级别执行随机化,而不是使用以速度慢著称的MySQL
orderbyrand()
方法。我尝试应用中描述的方法,具体更改如下:

$collection->getSelect()->order('rand()');
在此(及其各种排列)中:


其效果是,现在该块根本不显示在前端。如果您能就如何实现上面链接的解决方案提供任何建议,或以任何其他有效方式对上面引用的$collection变量进行随机化,我将不胜感激。

以下是解决上述问题的有效方法;这使用了应用程序,而不是数据库级别的随机化,并且确实大大缩短了Magento的第一个字节的时间

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;

}
函数_getProductCollection()
{
如果(为空($this->\u productCollection)){
$categoryID=$this->getCategoryId();
如果($categoryID){
$category=新的Mage_Catalog_Model_category();
$category->load($categoryID);
$collection=$category->getProductCollection();
}否则{
$collection=Mage::getResourceModel('catalog/product_collection');
}
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$maxsize=$collection->getSize();
$randArray=array();
while(count($randArray)getIsRandom()){
$collection->getSelect()->其中('product_id IN(?)$randArray)->订单('RAND()');
}
$collection->addStoreFilter();
$productCount=$this->getProductCount()?$this->getProductCount():8;
$collection->setPage(1,$productCount)->load();
$this->_productCollection=$collection;
}
返回$this->\u productCollection;
}
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;

}