【发布时间】:2017-02-19 18:04:54
【问题描述】:
我有一个很大的 sql 查询,它需要选择随机行,但是由于表很大,rand() 的 order 需要很长时间。
$getdata = $this->db->query("
SELECT DISTINCT property.id,property.unid,property.imported,property.userid,
CONCAT(user.firstname) as username,property.url,
IFNULL(user.thumbpic,'temp/misc/noimage.png') as profilepic,
property.bedrooms,property.beds,type.meta_val as type,property.accommodates,property.price,
IFNULL((select thumbimg from tblpropertyimages where pid=property.id limit 1),'temp/misc/noimage.png') as image,
property.name as propertyname,(select sum(rating) from tblreviews where pid=property.id) as totalrating,
(select count(id) from tblreviews where pid=property.id) as countratings,
location.name as cityname from tblproperty as property join tbluser as user on property.userid=user.id
join tblcommon as type on property.type=type.id
left join tblpropertyamenities as p_amenities on property.id=p_amenities.pid
join tbllocation as location on location.id=property.city
WHERE property.status='Active' and user.status='Active'
$home $q limit $limit offset $start");
对于这个特定的查询,选择随机行的最佳解决方案是什么?
【问题讨论】:
-
你能不能先生成一个随机数列表,然后只选择列表中包含的那些行(例如,可能通过将其连接到 #rownum 或类似的东西)。这样你实际上并没有对任何东西进行排序。 (生成随机数列表在这里:stackoverflow.com/questions/1045138/…)
标签: mysql sql random sql-order-by mariadb