简体   繁体   English

如何从大表子集中选择mysql中的n个随机行?

[英]how to select n random rows in mysql from subset of large table?

I know there is this question that is very similar, but I wanted to add my specific situation and see if the answer is different. 我知道有这个问题 ,这是非常相似的,但我想补充我的具体情况,看看答案是不同的。

I've read that you can simply do ORDER BY RAND() but that that is a bad idea for large tables. 我读过,您可以简单地执行ORDER BY RAND(),但这对大型表来说是个坏主意。 However, I can limit my rows to a max of about 160 with the WHERE clause. 但是,我可以使用WHERE子句将行限制为最多约160个。 So the question is, is the WHERE clause evaluated and processed before the ORDER BY RAND(), thereby making this an acceptable solution? 所以问题是,在ORDER BY RAND()之前是否对WHERE子句进行了评估和处理,从而使其成为可接受的解决方案?

My scenario is I'm implementing a card game. 我的情况是我正在实施纸牌游戏。 I have a deck table and a deck_card table. 我有一个卡座桌和一个卡座桌。 I'm trying to simply draw n random cards from the deck_cards table. 我试图从deck_cards表中简单地绘制n张随机卡。 Each game has a separate deck, so when a player draws n cards I know it is from a particular deck, so the proposed query is 每个游戏都有一个单独的套牌,因此当玩家抓取n张牌时,我知道它来自特定套牌,因此建议的查询为

select id
from deck_cards
where deck_id = ? and draw_pile = true
order by rand()
limit 5

The draw_pile column is for just what it sounds like, I keep track of where the cards are, in the draw_pile or discard_pile. draw_pile列用于显示听起来的样子,我会在draw_pile或discard_pile中跟踪卡片的位置。

Incidentally, I'm doing this in rails and am trying to shuffle the results with Array.sort_by { rand } , but I'm seeing evidence that the rails rand isn't quite random. 顺便说一句,我正在用rails进行此操作,并尝试使用Array.sort_by { rand }来改组结果,但是我看到有证据表明rails rand不是很随机。 So another question is whether it is better to try the random function in the SQL or in rails. 因此,另一个问题是,在SQL或rails中尝试随机函数是否更好。 Currently, I don't have either the ORDER BY RAND or the Limit in the SQL. 当前,我在SQL中没有ORDER BY RAND或Limit。 I'm shuffling the result array in rails and then just popping off n cards from that shuffled array. 我将结果数组拖入轨道,然后从经过改组的数组中弹出n张卡片。 But again, I'm seeing results that indicate that the shuffle isn't really shuffling randomly. 但是,我再次看到的结果表明改组并不是真正随机进行的改组。

The other issue/solution is perhaps table locking. 另一个问题/解决方案可能是表锁定。 This is a multiplayer, realtime game app. 这是一个多人实时游戏应用程序。 Occasionally I'm seeing >1 player drawing the same card. 有时我看到> 1个玩家在绘制同一张牌。 I assume this is from players drawing at the same time and hitting this query and shuffle which are somehow returning overlapping results. 我假设这是来自玩家同时绘制并点击此查询和随机播放的某种方式,它们以某种方式返回了重叠的结果。

"So the question is, is the WHERE clause evaluated and processed before the ORDER BY RAND(), thereby making this an acceptable solution?" “所以问题是,在ORDER BY RAND()之前是否对WHERE子句进行了评估和处理,从而使其成为可接受的解决方案?”

Yes. 是。

The order by is only a cursor that say in what order the data should be retrieved from set. order by只是一个游标,它指示应该从集合中检索数据的顺序。

I think using subquery will work as WHERE processed before the ORDER BY 我认为使用子查询将在ORDER BY之前作为WHERE处理

select id
from deck_cards
where id in (select id
             from deck_cards
             where deck_id = ? and draw_pile = true
          )
order by rand()
limit 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM