gpt4 book ai didi

mysql - 如何优化 MySQL 的 ORDER BY RAND() 函数?

转载 作者:行者123 更新时间:2023-11-29 19:11:33 25 4
gpt4 key购买 nike

我想优化我的查询,因此我查看 mysql-slow.log

我的大多数慢速查询都包含 ORDER BY RAND()。我找不到真正的解决方案来解决这个问题。有一个可能的解决方案:MySQLPerformanceBlog但我认为这还不够。在优化不佳(或频繁更新、用户管理)的表上,它不起作用,或者我需要运行两个或多个查询才能选择 PHP 生成的随机行。

这个问题有解决办法吗?

一个虚拟示例:

SELECT  accomodation.ac_id,
accomodation.ac_status,
accomodation.ac_name,
accomodation.ac_status,
accomodation.ac_images
FROM accomodation, accomodation_category
WHERE accomodation.ac_status != 'draft'
AND accomodation.ac_category = accomodation_category.acat_id
AND accomodation_category.acat_slug != 'vendeglatohely'
AND ac_images != 'b:0;'
ORDER BY
RAND()
LIMIT 1

最佳答案

试试这个:

SELECT  *
FROM (
SELECT @cnt := COUNT(*) + 1,
@lim := 10
FROM t_random
) vars
STRAIGHT_JOIN
(
SELECT r.*,
@lim := @lim - 1
FROM t_random r
WHERE (@cnt := @cnt - 1)
AND RAND(20090301) < @lim / @cnt
) i

这在 MyISAM 上尤其有效(因为 COUNT(*) 是即时的),但即使在 InnoDB 中,它也是 10比 ORDER BY RAND() 效率高 倍。

这里的主要思想是我们不排序,而是保留两个变量并计算当前步骤中要选择的行的运行概率

有关更多详细信息,请参阅我的博客中的这篇文章:

更新:

如果您只需要选择一个随机记录,请尝试以下操作:

SELECT  aco.*
FROM (
SELECT minid + FLOOR((maxid - minid) * RAND()) AS randid
FROM (
SELECT MAX(ac_id) AS maxid, MIN(ac_id) AS minid
FROM accomodation
) q
) q2
JOIN accomodation aco
ON aco.ac_id =
COALESCE
(
(
SELECT accomodation.ac_id
FROM accomodation
WHERE ac_id > randid
AND ac_status != 'draft'
AND ac_images != 'b:0;'
AND NOT EXISTS
(
SELECT NULL
FROM accomodation_category
WHERE acat_id = ac_category
AND acat_slug = 'vendeglatohely'
)
ORDER BY
ac_id
LIMIT 1
),
(
SELECT accomodation.ac_id
FROM accomodation
WHERE ac_status != 'draft'
AND ac_images != 'b:0;'
AND NOT EXISTS
(
SELECT NULL
FROM accomodation_category
WHERE acat_id = ac_category
AND acat_slug = 'vendeglatohely'
)
ORDER BY
ac_id
LIMIT 1
)
)

这假设您的 ac_id 分布或多或少均匀。

关于mysql - 如何优化 MySQL 的 ORDER BY RAND() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43039723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com