Mysql random row from large database table faster

May-23-2014 in Mysql, PHP No comments

The question of “HOW TO SELECT RANDOM ROWS IN MYSQL?”

To select mysql random row from large database, you can refer to the following steps:

selecting-random-rows-faster-mysql

Using ORDER BY RAND()

The simplest way of selecting random rows from the MySQL database is to use “ORDER BY RAND()” clause in the query database.

The following code:

 

But there is only one problem with this solution – it is very slow, as RAND() should be executed for each row, assigned and only after this MySQL will be able to select the record. If table is small this approach will work and probably will work best. But what if table contains 100 000+ records? or even more…because mysql needs to create temporary table. So never do ti this way

Using MySQL with PHP code to select random row

The following code:

 

Add Your Comment