简体   繁体   English

如何在每次执行SQL查询时选择随机唯一记录

[英]how to select random unique records on each execution of the SQL Query

I have a table "masterurls" it has morethan 1 million records. 我有一个表“ masterurls”,它有超过100万条记录。 I want to fetch random records each time the query executed. 我想在每次执行查询时获取随机记录。 It should not have any of the records that were fetched in previous executions. 它不应包含先前执行中获取的任何记录。 I'm already have this query: 我已经有了这个查询:

SELECT m.url FROM masterurls ORDER BY RAND() LIMIT 200

The problem is the above query returns only first 200 hundred records and randomizes it each time. 问题是上述查询仅返回前200条记录,并且每次都将其随机化。

How are you going to know if the url is already accessed before. 您将如何知道之前是否已经访问过该URL。 My best suggestion would be setting a flag to know this in the table. 我最好的建议是在表中设置一个标志来知道这一点。 Add a field like view in the table which will accept two values 1 or 0, 1 for already accessed and 0 for not accessed. 在表中添加一个类似于view的字段,该字段将接受两个值1或0,其中1表示已访问,0表示未访问。 Then you could use 那你可以用

SELECT m.url FROM masterurls m WHERE view='1' ORDER BY RAND() LIMIT 200;

Since you can pass a seed parameter to the RAND() function, you can "paginate" the random results by generating a seed before the first page. 由于您可以将种子参数传递给RAND()函数,因此可以通过在第一页之前生成种子来“分页”随机结果。

Sample code: For the first page (varies by language): 示例代码:对于第一页(因语言而异):

int seed = Math.abs(new Random().nextInt());

SQL query: SQL查询:

SELECT url FROM masterurls ORDER BY RAND({seed}) LIMIT 200;

Store the seed somewhere (for web-based applications you can use a url parameter or session). 将种子存储在某处(对于基于Web的应用程序,您可以使用url参数或会话)。 For the next pages: 对于下一页:

SELECT url FROM masterurls ORDER BY RAND({seed}) LIMIT 200 * {pageNumber}, 200;

Note: Sorting by RAND() is a heavy operation, you might be better off storing a indexed column with the Hash Code of the url, then using a module-based or other random functions. 注意:按RAND()排序是一项繁重的操作,您最好将索引列与url的哈希码一起存储,然后再使用基于模块的函数或其他随机函数。

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

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