查询 magento 限制 +按 rand() 排序
function getIdModelsSliderJuwels(){
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("attribute_set_id", 27);
$collection->addAttributeToSelect('modellijnen');
// $collection->setRandomOrder();
// $collection->getSelect()->limit( 5 );
return $collection;
}
您好,
我想知道如何设置在 Magento 中运行的查询的限制,因为 $collection->getSelect()->limit( 5 );
不起作用。
另外如何随机选择, $collection->setRandomOrder();
也不起作用。
德克萨斯州。
function getIdModelsSliderJuwels(){
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("attribute_set_id", 27);
$collection->addAttributeToSelect('modellijnen');
// $collection->setRandomOrder();
// $collection->getSelect()->limit( 5 );
return $collection;
}
Hi there,
I'd like to know how to set a limit to your query running in Magento because$collection->getSelect()->limit( 5 );
doesn't work.
Also how to select randomly, $collection->setRandomOrder();
also doesn't work.
txs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
$collection->setPageSize(5)->setCurPage(1);
try to use
$collection->setPageSize(5)->setCurPage(1);
使用 ORDER BY RAND() 以随机顺序返回项目列表将需要全表扫描和排序。它会对表中大量行的性能产生负面影响。
对于如何优化此查询,有几种可能的替代解决方案。 Magento 为此提供了本地解决方案。
Varien_Db_Select
的orderRand()
方法和数据库适配器允许为ORDER BY
指定随机订单和杠杆索引。指定要在 ORDER BY 子句中使用的某些整数索引列的名称,例如:有关实现详细信息,请参阅
Varien_Db_Adapter_Pdo_Mysql::orderRand()
。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 ofVarien_Db_Select
and the database adapter allows to specify a random order and leverage index forORDER BY
. Specify a name of some integer indexed column to be used in theORDER BY
clause, for example:See
Varien_Db_Adapter_Pdo_Mysql::orderRand()
for implementation details.setRandomOrder
不适用于产品集合,仅适用于相关产品。您必须使用以下代码自行添加:同时设置页面大小和页数的快捷方式是:
setRandomOrder
does not work for collections of products, only for related products. You'll have to add it yourself with this code:A shortcut for setting both page size and number at the same time is:
正如clockworkgeek所说,使用
$collection->getSelect()->order(...)
方法来随机化顺序。要将其限制为仅$n
个项目,您还可以使用As clockworkgeek said, use the
$collection->getSelect()->order(...)
method to randomize the order. To limit it to just$n
number of items you can also use