开发者

MySQL random record with a bias

开发者 https://www.devze.com 2023-03-24 09:21 出处:网络
I would like to select a random record from a table but with a bias toward higher values in a particular field -- I don\'t want any record to have a 0% chance of getting selected, just less likely to

I would like to select a random record from a table but with a bias toward higher values in a particular field -- I don't want any record to have a 0% chance of getting selected, just less likely to get selected.

From this article, I know that random selects can be slow and you can speed them up: http://wanderr.com/jay/order-by-slow/2008/01/30/

But what about when you are dealing with a few tables with joins and a where statement, and want to use one of the fields as a way to bias the randomness (the higher this field's value, the more likely to get selected)? For example:

SELECT a.id, a.date, a.userid, b.points FROM table_a AS a INNER JOIN table_b AS b ON (a.userid = b.userid) WHERE DATE_SUB(CURDATE(), INTERVAL 60 DAY) <= a.date

How could I turn the above into an efficient but not truly random query 开发者_JAVA技巧that would be biased toward higher values of b.points?


my 2 cents, biased can be carried out like this:

Assuming the Score is between 0, 100.

You randomly choose 5 records that is >75, 3 recording >50, 2 record >25, 1 record >0

Now if you random again from this 11 records, it is biased toward higher score.

To put them in sql, called your joined table "abc"

Select * from (
select * from abc where b.points > 75 order by rand() limit 5
cross join 
select * from abc where b.points > 50 and b.points <75 order by rand() limit 3
cross join 
select * from abc where b.points > 25 and b.points <50 order by rand() limit 2
cross join 
select * from abc where b.points > 0 and b.points <25 order by rand() limit 1
) as result
order by rand() limit 3

Performance wise, I'll have a look at your link and update this anwser.

0

精彩评论

暂无评论...
验证码 换一张
取 消