Using order_by('?')
can be very inefficient if you have lots of rows in the table. Moving the randomness to the application layer will probably give significant a performance improvement.
Using order_by('?')
can be very slow as to achieve random ordering the database has to:
Let that sink in: generating random numbers is slow and scanning is not quick. We're doing that for every row in the table just to read one row! If there are a lot of rows then expect a performance impact. In short, databases are not good at random. Applications are though. So consier splitting responsibilities between the database and the application:
objects.count()
This will mean two database reads are performed, but that will be significantly quicker than the database copying the table and ordering all the rows randomly.
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | def get_random_item(): |
Using order_by('?')
can be very inefficient if you have lots of rows in the table. Moving the randomness to the application layer will probably give significant a performance improvement.
+ | from random import randint |
+ | |
- | return MyModel.objects.order_by('?')[0] |
+ | index = randint(0, MyModel.objects.count() -1) |
+ | return model.objects.all()[index] |
2 | + | return MyModel.objects.order_by('?')[0] |