mysql - 获取随机行,但不使用 ORDER BY RAND() MariaDB

标签 mysql sql random sql-order-by mariadb

我有一个相当大的 sql 查询,它需要选择随机行,但是因为表很大,所以通过 rand() 排序需要很长时间。

            $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");

对于此特定查询,选择随机行的最佳解决方案是什么?

最佳答案

根据您的详细要求,here 中有几种更快的方法没有一个是“完美的”,但每个都可能“足够好”。

关于mysql - 获取随机行,但不使用 ORDER BY RAND() MariaDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42330867/

相关文章:

MySQL - 如何通过中间文本/数字匹配查询结果

mysql - 优化 mysql 查询 - 避免创建临时表?

java - 将列表列表随机分组为 N 个子集,每个子​​集有 K 个元素

python - 如何在 Cython 中处理任意长度列表作为输入和输出

mysql - 标签数据库架构

mysql - 为MySQL中的每个组选择第一行?

java - hibernate 条件 : hibernate left join without association

sql - 不支持表/列名称中的方括号?

sql - 在 go 中结合 row.Scan 和 rows.Scan 接口(interface)?

arrays - 将一个数组插入到链表中,其中元素的顺序是随机的