Hi,

We have some code to return randomised Django model instances.

Initially, I was using random.randint(), and returning objects by index 
between 0 < max_id.

This was based on the Django docs:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by

specifically, the part:

Note: order_by('?') queries may be expensive and slow, depending on the 
> database backend you’re using.  


This blog post from February 2011 also seems to back that up:

http://www.peterbe.com/plog/getting-random-rows-postgresql-django

However, I just ran the benchmarks on my own box running Django 1.5 and 
Postgres 9.2.4, and the ORDER BY RANDOM() approach was slower, but not 
substantially so (certainly not to the extent hinted at by the blog post).

I'm curious - what are other people's experiences with using ORDERY BY 
RANDOM versus other approaches? 

Has Postgres made some recent changes which makes ORDER BY RANDOM the 
better option now?

Which database backends specifically should we be warned about when saying 
that order_by('?') is expensive and slow?
Cheers,
Victor

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to