How to select a random record from MySQL database without using primary key and order by rand()


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
36 views
There is a problem, there is a database which contains users (users installed the app Vkontakte, to be exact) as a Primary key ispolzuetsa user id in the social network, which can be considered a random number. Need to select from the database one random user. The user base a lot so order by rand() too expensive to use to generate a random number and select the record with the same id also not work, given that id go out of order. How to be in this situation? And at the same time, what if you need a few random users?
by | 36 views

6 Answers

0 like 0 dislike
The first thing that comes to mind: to play with the limit, for example limit <random number>,<sample size>
by
0 like 0 dislike
The best solution is to take the random range, which is the primary key and get the record whose key is greater than or equal to this randomness. Like this:
\r
\r
SELECT * FROM my_table WHERE pk_column >= (SELECT FLOOR( MAX(pk_column) * RAND()) FROM my_table) ORDER BY pk_column LIMIT 1; 

Of course, there are problems in tables, which removed large ranges zapisa, but there are also their decisions, like the introduction of additional cross-cutting field without "holes". Here you can read more here
by
0 like 0 dislike
0 like 0 dislike
add the table view
by
0 like 0 dislike
To renormalizability id in the SET in Redis and use SRANDMEMBER.
by
0 like 0 dislike
get uRAND field in the table and once a day sit there true RAND
then SELECT * FROM table WHERE uRAND
by

Related questions

110,608 questions
257,186 answers
0 comments
26,179 users