简体   繁体   English

MySQL快速选择3个唯一随机ID行

[英]MySQL select 3 UNIQUE random ID rows fast WHERE

I need to be able to select 3 Unique random MySQL ID rows from a table that is progressively growing and growing over time, AND have a WHERE 'status' = 'available'. 我需要能够从随着时间的推移逐渐增长和增长的表中选择3个唯一的随机MySQL ID行,并具有WHERE'status'='available'。 So it only picks the id number if status = available. 因此,仅在status = available时才选择ID号。

There will be no gaps as all data is kept for record keeping compliance, if data is deleted by the user it is not actually deleted just STATUS is marked 'deleted'. 不会有间隙,因为将保留所有数据以确保符合记录要求,如果用户删除了数据,则实际上并没有删除它,只是将STATUS标记为“已删除”。

I've read many posts about the subject but it all seems to boil down to picking just 1 result, and then repeating it 3 times, problem is, it might pick the same ID again, so any solution to my problem will be much appreciated. 我已经读过很多关于该主题的文章,但似乎都归结为仅选择1个结果,然后重复3次,问题是,它可能会再次选择相同的ID,因此,对我的问题的任何解决方案将不胜感激。

Thank you 谢谢

:) :)

I was looking at doing something like this (taken from here ... MySQL select 10 random rows from 600K rows fast ) 我正在考虑做这样的事情(从这里取... MySQL从60万行中快速选择10个随机行

SELECT id
  FROM table AS r1 JOIN
       (SELECT (RAND() *
                     (SELECT MAX(id)
                        FROM table)) AS id2)
        AS r2
 WHERE r1.id >= r2.id2 AND status = 'available'
 ORDER BY r1.id ASC
 LIMIT 1

Which gives me what I need, but it only returns 1 random Unique 'pick', plus IF I limit it to 3, it does not always pick 3, and if it does pick 3 they always seem to be sequential which is not random! 这满足了我的需求,但是它只返回1个随机的唯一“选择”,如果我将其限制为3,则它并不总是选择3,如果选择了3,它们似乎总是顺序的,这不是随机的!

Is there a way to repeat the query, store the ID picked and keep looping / running the query until 3 ID's are picked AT RANDOM that are Different. 有没有一种方法可以重复查询,存储选择的ID并继续循环/运行查询,直到在AT RANDOM中选择了3个不同的ID。 This is my question :) 这是我的问题:)

Did you try: 你试过了吗:

SELECT *
FROM mytable
WHERE status = 'available'
ORDER BY RAND( )
LIMIT 0,3;
SELECT DISTINCT *
FROM table
WHERE status = 'available'
ORDER BY RAND()
LIMIT 3

The DISTINCT clause shall prevent repeated values. DISTINCT子句应防止重复值。 ORDER BY RAND() will randomly organize the result. ORDER BY RAND()将随机组织结果。

See the following; 请参阅以下内容; where it talks about "gaps", you have the equivalent (status=deleted) http://mysql.rjweb.org/doc.php/random#case_auto_increment_with_gaps_1_or_more_rows_returned (and look around in that link for variants) 在谈论“差距”的地方,您有一个等效的(状态=已删除) http://mysql.rjweb.org/doc.php/random#case_auto_increment_with_gaps_1_or_more_rows_returned (并在该链接中查找变体)

  • Requirement: AUTO_INCREMENT, possibly with gaps due to DELETEs, etc 要求:AUTO_INCREMENT,可能由于DELETE等而存在间隙
  • Flaw: Only semi-random (rows do not have an equal chance of being picked), but it does partially compensate for the gaps 瑕疵:仅半随机(行被拾取的机会均等),但确实可以部分弥补差距
  • Flaw: The first and last few rows of the table are less likely to be delivered. 缺陷:表的前几行和后几​​行不太可能被交付。

This gets 50 "consecutive" ids (possibly with gaps), then delivers a random 10 of them. 这将获得50个“连续的” id(可能带有空格),然后随机提供10个。

-- First select is one-time:
SELECT @min := MIN(id),
       @max := MAX(id)
    FROM RandTest;
SELECT a.*
    FROM RandTest a
    JOIN ( SELECT id FROM
            ( SELECT id
                FROM ( SELECT @min + (@max - @min + 1 - 50) *
                         RAND() AS start FROM DUAL ) AS init
                JOIN RandTest y
                WHERE    y.id > init.start
                ORDER BY y.id
                LIMIT 50           -- Inflated to deal with gaps
            ) z ORDER BY RAND()
           LIMIT 10                -- number of rows desired
         ) r ON a.id = r.id;

Yes, it is complex, but yes, it is fast, regardles of the table size. 是的,它很复杂,但是很快,这取决于表的大小。

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

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