Framework: inRandomOrder is terribly inefficient

Created on 9 Jul 2016  路  8Comments  路  Source: laravel/framework

inRandomOrder is terribly inefficient when database is not small. For example, if a database has 50000 rows it needs to generate 50000 random numbers and sort all rows without an index. The algorithm need to be changed. Skipping random number of rows is far more efficient as is explained here:
http://akinas.com/pages/en/blog/mysql_random_row/

The method was initially introduced in order to get some random rows not ordering them randomly.

Most helpful comment

So as I said, create a PR ;)

All 8 comments

The method explained where?

The inRandomOrder method is on the query builder, so one would expect it to call the random function of the underlying database driver. If you wanted to use a specific, different sort of randomisation I think that's the sort of thing you'd implement in your own project.

If you have something that will work more efficient, without configuration, it would probably be accepted as PR. What do you suggest?

@barryvdh Skipping random number of rows is far more efficient as is explained here:
http://akinas.com/pages/en/blog/mysql_random_row/

Yes, for selecting 1 item. But that doesn't work for multiple rows.

@barryvdh If my memory serve me rightly that method was initially suggested for choosing a single row. However, for selecting multiple rows, several rows can be selected and be UNIONed. Normally the application needs to choose a very tiny proportion of tables rows randomly. And that is yet far more efficient that random sorting.

So as I said, create a PR ;)

@salarmehr any solution you recommend. I am also noticing inRandomOrder() is very slow with large tables.

@vdhicts I have already suggested something in the above converstaion.

Was this page helpful?
0 / 5 - 0 ratings