magento get products from category, order by rand()

后端 未结 3 457
别跟我提以往
别跟我提以往 2020-12-25 14:02

I have the following:

$products = Mage::getModel(\'catalog/product\')
    ->getCollection()
    ->addAttributeToSort(\'id\', \'RAND()\')
    ->addAt         


        
3条回答
  •  死守一世寂寞
    2020-12-25 14:38

    Using ORDER BY RAND() to return a list of items in a random order will require a full table scan and sort. It can negatively affect performance on large number of rows in the table.

    There are several alternative solutions possible of how to optimize this query. Magento provides a native solution for that.

    The orderRand() method of Varien_Db_Select and the database adapter allows to specify a random order and leverage index for ORDER BY. Specify a name of some integer indexed column to be used in the ORDER BY clause, for example:

    $collection->getSelect()->orderRand('main_table.entity_id');
    

    See Varien_Db_Adapter_Pdo_Mysql::orderRand() for implementation details.

提交回复
热议问题